Skip to contentSkip to conversation

dma-buf: add dynamic DMA-buf handling v12

PATCH In review Christian König 6 participants 23 messages Jun 25, 2019 – Jun 26, 2019 8 messages not in the archive

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/6 dma-buf: add dynamic DMA-buf handling v12

On the exporter side we add optional explicit pinning callbacks. If those callbacks are implemented the framework no longer caches sg tables and the map/unmap callbacks are always called with the lock of the reservation object held.

On the importer side we add an optional invalidate callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible.

This allows the exporter to provide the mappings without the need to pin the backing store.

v2: don't try to invalidate mappings when the callback is NULL,
lock the reservation obj while using the attachments, add helper to set the callback
v3: move flag for invalidation support into the DMA-buf,
use new attach_info structure to set the callback
v4: use importer_priv field instead of mangling exporter priv.
v5: drop invalidation_supported flag
v6: squash together with pin/unpin changes
v7: pin/unpin takes an attachment now
v8: nuke dma_buf_attachment_(map|unmap)_locked,
everything is now handled backward compatible
v9: always cache when export/importer don't agree on dynamic handling
v10: minimal style cleanup
v11: drop automatically re-entry avoidance
v12: rename callback to move_notify

---
 drivers/dma-buf/dma-buf.c | 179 ++++++++++++++++++++++++++++++++++++--
 include/linux/dma-buf.h   | 108 +++++++++++++++++++++--
 2 files changed, 273 insertions(+), 14 deletions(-)
drivers/dma-buf/dma-buf.c +173 -6
index 6c15deb5d4ad..216f76109f3f 100644
@@ -531,6 +531,9 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
 		return ERR_PTR(-EINVAL);
 	}
 
+	if (WARN_ON(exp_info->ops->cache_sgt_mapping && exp_info->ops->pin))
+		return ERR_PTR(-EINVAL);
+
 	if (!try_module_get(exp_info->owner))
 		return ERR_PTR(-ENOENT);
 
@@ -651,10 +654,12 @@ void dma_buf_put(struct dma_buf *dmabuf)
 EXPORT_SYMBOL_GPL(dma_buf_put);
 
 /**
- * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
+ * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list; optionally,
  * calls attach() of dma_buf_ops to allow device-specific attach functionality
- * @dmabuf:	[in]	buffer to attach device to.
- * @dev:	[in]	device to be attached.
+ * @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
  *
  * Returns struct dma_buf_attachment pointer for this attachment. Attachments
  * must be cleaned up by calling dma_buf_detach().
@@ -668,8 +673,10 @@ EXPORT_SYMBOL_GPL(dma_buf_put);
  * accessible to @dev, and cannot be moved to a more suitable place. This is
  * indicated with the error code -EBUSY.
  */
-struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-					  struct device *dev)
+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 *attach;
 	int ret;
@@ -683,6 +690,8 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
 
 	attach->dev = dev;
 	attach->dmabuf = dmabuf;
+	attach->importer_ops = importer_ops;
+	attach->importer_priv = importer_priv;
 
 	mutex_lock(&dmabuf->lock);
 
@@ -691,16 +700,72 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
 		if (ret)
 			goto err_attach;
 	}
+	reservation_object_lock(dmabuf->resv, NULL);
 	list_add(&attach->node, &dmabuf->attachments);
+	reservation_object_unlock(dmabuf->resv);
 
 	mutex_unlock(&dmabuf->lock);
 
+	/* When either the importer or the exporter can't handle dynamic
+	 * mappings we cache the mapping here to avoid issues with the
+	 * reservation object lock.
+	 */
+	if (dma_buf_attachment_is_dynamic(attach) !=
+	    dma_buf_is_dynamic(dmabuf)) {
+		struct sg_table *sgt;
+
+		if (dma_buf_is_dynamic(attach->dmabuf)) {
+			reservation_object_lock(attach->dmabuf->resv, NULL);
+			ret = dma_buf_pin(attach);
+			if (ret)
+				goto err_unlock;
+		}
+
+		sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL);
+		if (!sgt)
+			sgt = ERR_PTR(-ENOMEM);
+		if (IS_ERR(sgt)) {
+			ret = PTR_ERR(sgt);
+			goto err_unpin;
+		}
+		if (dma_buf_is_dynamic(attach->dmabuf))
+			reservation_object_unlock(attach->dmabuf->resv);
+		attach->sgt = sgt;
+		attach->dir = DMA_BIDIRECTIONAL;
+	}
+
 	return attach;
 
 err_attach:
 	kfree(attach);
 	mutex_unlock(&dmabuf->lock);
 	return ERR_PTR(ret);
+
+err_unpin:
+	if (dma_buf_is_dynamic(attach->dmabuf))
+		dma_buf_unpin(attach);
+
+err_unlock:
+	if (dma_buf_is_dynamic(attach->dmabuf))
+		reservation_object_unlock(attach->dmabuf->resv);
+
+	dma_buf_detach(dmabuf, attach);
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(dma_buf_dynamic_attach);
+
+/**
+ * dma_buf_attach - Wrapper for dma_buf_dynamic_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
+ * mapping.
+ */
+struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
+					  struct device *dev)
+{
+	return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
 }
 EXPORT_SYMBOL_GPL(dma_buf_attach);
 
@@ -717,11 +782,22 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
 	if (WARN_ON(!dmabuf || !attach))
 		return;
 
-	if (attach->sgt)
+	if (attach->sgt) {
+		if (dma_buf_is_dynamic(attach->dmabuf))
+			reservation_object_lock(attach->dmabuf->resv, NULL);
+
 		dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
 
+		if (dma_buf_is_dynamic(attach->dmabuf)) {
+			dma_buf_unpin(attach);
+			reservation_object_unlock(attach->dmabuf->resv);
+		}
+	}
+
 	mutex_lock(&dmabuf->lock);
+	reservation_object_lock(dmabuf->resv, NULL);
 	list_del(&attach->node);
+	reservation_object_unlock(dmabuf->resv);
 	if (dmabuf->ops->detach)
 		dmabuf->ops->detach(dmabuf, attach);
 
@@ -730,6 +806,44 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
 }
 EXPORT_SYMBOL_GPL(dma_buf_detach);
 
+/**
+ * dma_buf_pin - Lock down the DMA-buf
+ *
+ * @attach:	[in]	attachment which should be pinned
+ *
+ * Returns:
+ * 0 on success, negative error code on failure.
+ */
+int dma_buf_pin(struct dma_buf_attachment *attach)
+{
+	struct dma_buf *dmabuf = attach->dmabuf;
+	int ret = 0;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	if (dmabuf->ops->pin)
+		ret = dmabuf->ops->pin(attach);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(dma_buf_pin);
+
+/**
+ * dma_buf_unpin - Remove lock from DMA-buf
+ *
+ * @attach:	[in]	attachment which should be unpinned
+ */
+void dma_buf_unpin(struct dma_buf_attachment *attach)
+{
+	struct dma_buf *dmabuf = attach->dmabuf;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	if (dmabuf->ops->unpin)
+		dmabuf->ops->unpin(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_unpin);
+
 /**
  * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
@@ -749,6 +863,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
 					enum dma_data_direction direction)
 {
 	struct sg_table *sg_table;
+	int r;
 
 	might_sleep();
 
@@ -767,10 +882,29 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
 		return attach->sgt;
 	}
 
+	if (dma_buf_attachment_is_dynamic(attach)) {
+		reservation_object_assert_held(attach->dmabuf->resv);
+
+	} else if (dma_buf_is_dynamic(attach->dmabuf)) {
+		reservation_object_lock(attach->dmabuf->resv, NULL);
+		r = dma_buf_pin(attach);
+		if (r) {
+			reservation_object_unlock(attach->dmabuf->resv);
+			return ERR_PTR(r);
+		}
+	}
+
 	sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
 	if (!sg_table)
 		sg_table = ERR_PTR(-ENOMEM);
 
+	if (!dma_buf_attachment_is_dynamic(attach) &&
+	    dma_buf_is_dynamic(attach->dmabuf)) {
+		if (IS_ERR(sg_table))
+			dma_buf_unpin(attach);
+		reservation_object_unlock(attach->dmabuf->resv);
+	}
+
 	if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
 		attach->sgt = sg_table;
 		attach->dir = direction;
@@ -802,10 +936,41 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
 	if (attach->sgt == sg_table)
 		return;
 
+	if (dma_buf_attachment_is_dynamic(attach))
+		reservation_object_assert_held(attach->dmabuf->resv);
+	else if (dma_buf_is_dynamic(attach->dmabuf))
+		reservation_object_lock(attach->dmabuf->resv, NULL);
+
 	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+
+	if (dma_buf_is_dynamic(attach->dmabuf) &&
+	    !dma_buf_attachment_is_dynamic(attach)) {
+		dma_buf_unpin(attach);
+		reservation_object_unlock(attach->dmabuf->resv);
+	}
 }
 EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
 
+/**
+ * dma_buf_move_notify - notify attachments that DMA-buf is moving
+ *
+ * @dmabuf:	[in]	buffer which is moving
+ *
+ * Informs all attachmenst that they need to destroy and recreated all their
+ * mappings.
+ */
+void dma_buf_move_notify(struct dma_buf *dmabuf)
+{
+	struct dma_buf_attachment *attach;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	list_for_each_entry(attach, &dmabuf->attachments, node)
+		if (attach->importer_ops && attach->importer_ops->move_notify)
+			attach->importer_ops->move_notify(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_move_notify);
+
 /**
  * DOC: cpu access
  *
@@ -1225,10 +1390,12 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 		seq_puts(s, "\tAttached Devices:\n");
 		attach_count = 0;
 
+		reservation_object_lock(buf_obj->resv, NULL);
 		list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
 			seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
 			attach_count++;
 		}
+		reservation_object_unlock(buf_obj->resv);
 
 		seq_printf(s, "Total %d devices attached\n\n",
 				attach_count);
include/linux/dma-buf.h +100 -8

163 lines not on this page · raw message

Signature · 6 lines
2.17.1

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH 2/6 drm/ttm: remove the backing store if no placement is given

Pipeline removal of the BOs backing store when no placement is given during validation.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpu/drm/ttm/ttm_bo.c +12 -0
index c7de667d482a..682a1a035b35 100644
@@ -1240,6 +1240,18 @@ int ttm_bo_validate(struct ttm_buffer_object *bo,
 	uint32_t new_flags;
 
 	reservation_object_assert_held(bo->resv);
+
+	/*
+	 * Remove the backing store if no placement is given.
+	 */
+	if (!placement->num_placement && !placement->num_busy_placement) {
+		ret = ttm_bo_pipeline_gutting(bo);
+		if (ret)
+			return ret;
+
+		return ttm_tt_create(bo, false);
+	}
+
 	/*
 	 * Check whether we need to move buffer.
 	 */
Signature · 6 lines
2.17.1

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

PATCH 3/6 drm/ttm: use the parent resv for ghost objects v2

In reply to Christian König

This way we can even pipeline imported BO evictions.

v2: Limit this to only cases when the parent object uses a separate
reservation object as well. This fixes another OOM problem.

---
 drivers/gpu/drm/ttm/ttm_bo_util.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_util.c +11 -9
index 9f918b992f7e..5992ca821c69 100644
@@ -517,9 +517,11 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
 	kref_init(&fbo->base.kref);
 	fbo->base.destroy = &ttm_transfered_destroy;
 	fbo->base.acc_size = 0;
-	fbo->base.resv = &fbo->base.ttm_resv;
-	reservation_object_init(fbo->base.resv);
-	ret = reservation_object_trylock(fbo->base.resv);
+	if (bo->resv == &bo->ttm_resv)
+		fbo->base.resv = &fbo->base.ttm_resv;
+
+	reservation_object_init(&fbo->base.ttm_resv);
+	ret = reservation_object_trylock(&fbo->base.ttm_resv);
 	WARN_ON(!ret);
 
 	*new_obj = &fbo->base;
@@ -716,7 +718,7 @@ int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
 		if (ret)
 			return ret;
 
-		reservation_object_add_excl_fence(ghost_obj->resv, fence);
+		reservation_object_add_excl_fence(&ghost_obj->ttm_resv, fence);
 
 		/**
 		 * If we're not moving to fixed memory, the TTM object
@@ -729,7 +731,7 @@ int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
 		else
 			bo->ttm = NULL;
 
-		ttm_bo_unreserve(ghost_obj);
+		reservation_object_unlock(&ghost_obj->ttm_resv);
 		ttm_bo_put(ghost_obj);
 	}
 
@@ -772,7 +774,7 @@ int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
 		if (ret)
 			return ret;
 
-		reservation_object_add_excl_fence(ghost_obj->resv, fence);
+		reservation_object_add_excl_fence(&ghost_obj->ttm_resv, fence);
 
 		/**
 		 * If we're not moving to fixed memory, the TTM object
@@ -785,7 +787,7 @@ int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
 		else
 			bo->ttm = NULL;
 
-		ttm_bo_unreserve(ghost_obj);
+		reservation_object_unlock(&ghost_obj->ttm_resv);
 		ttm_bo_put(ghost_obj);
 
 	} else if (from->flags & TTM_MEMTYPE_FLAG_FIXED) {
@@ -841,7 +843,7 @@ int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
 	if (ret)
 		return ret;
 
-	ret = reservation_object_copy_fences(ghost->resv, bo->resv);
+	ret = reservation_object_copy_fences(&ghost->ttm_resv, bo->resv);
 	/* Last resort, wait for the BO to be idle when we are OOM */
 	if (ret)
 		ttm_bo_wait(bo, false, false);
@@ -850,7 +852,7 @@ int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
 	bo->mem.mem_type = TTM_PL_SYSTEM;
 	bo->ttm = NULL;
 
-	ttm_bo_unreserve(ghost);
+	reservation_object_unlock(&ghost->ttm_resv);
 	ttm_bo_put(ghost);
 
 	return 0;
Signature · 6 lines
2.17.1

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

A message that is not in the archive (sent off-list or not yet received): 20190625124654.122364-1-christian.koenig-5C7GfCeVMHo@public.gmane.org

#4 lore raw

PATCH 4/6 drm/amdgpu: use allowed_domains for exported DMA-bufs

Avoid that we ping/pong the buffers when we stop to pin DMA-buf exports by using the allowed domains for exported buffers.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +4 -1
index dc63707e426f..0da512341194 100644
@@ -28,6 +28,7 @@
 #include <linux/file.h>
 #include <linux/pagemap.h>
 #include <linux/sync_file.h>
+#include <linux/dma-buf.h>
 
 #include <drm/amdgpu_drm.h>
 #include <drm/drm_syncobj.h>
@@ -414,7 +415,9 @@ static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
 	/* Don't move this buffer if we have depleted our allowance
 	 * to move it. Don't move anything if the threshold is zero.
 	 */
-	if (p->bytes_moved < p->bytes_moved_threshold) {
+	if (p->bytes_moved < p->bytes_moved_threshold &&
+	    (!bo->gem_base.dma_buf ||
+	    list_empty(&bo->gem_base.dma_buf->attachments))) {
 		if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
 		    (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
 			/* And don't move a CPU_ACCESS_REQUIRED BO to limited
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#5 lore raw

PATCH 5/6 drm/amdgpu: add independent DMA-buf export v6

In reply to a message not in the archive

The caching of SGT's is actually quite harmful and should probably removed altogether when all drivers are audited.

Start by providing a separate DMA-buf export implementation in amdgpu. This is also a prerequisite of unpinned DMA-buf handling.

v2: fix unintended recursion, remove debugging leftovers
v3: split out from unpinned DMA-buf work
v4: rebase on top of new no_sgt_cache flag
v5: fix some warnings by including amdgpu_dma_buf.h
v6: fix locking for non amdgpu exports

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 210 +++++++++++++-------
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.h |   1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c  |   5 +
 4 files changed, 139 insertions(+), 78 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +134 -76
index 4809d4a5d72a..4541eae7aadb 100644
@@ -34,26 +34,11 @@
 #include "amdgpu.h"
 #include "amdgpu_display.h"
 #include "amdgpu_gem.h"
+#include "amdgpu_dma_buf.h"
 #include <drm/amdgpu_drm.h>
 #include <linux/dma-buf.h>
 #include <linux/dma-fence-array.h>
 
-/**
- * amdgpu_gem_prime_get_sg_table - &drm_driver.gem_prime_get_sg_table
- * implementation
- * @obj: GEM buffer object (BO)
- *
- * Returns:
- * A scatter/gather table for the pinned pages of the BO's memory.
- */
-struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj)
-{
-	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
-	int npages = bo->tbo.num_pages;
-
-	return drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages);
-}
-
 /**
  * amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
  * @obj: GEM BO
@@ -179,92 +164,163 @@ __reservation_object_make_exclusive(struct reservation_object *obj)
 }
 
 /**
- * amdgpu_dma_buf_map_attach - &dma_buf_ops.attach implementation
- * @dma_buf: Shared DMA buffer
+ * amdgpu_dma_buf_attach - &dma_buf_ops.attach implementation
+ *
+ * @dmabuf: DMA-buf where we attach to
+ * @attach: attachment to add
+ *
+ * Add the attachment as user to the exported DMA-buf.
+ */
+static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf,
+				 struct dma_buf_attachment *attach)
+{
+	struct drm_gem_object *obj = dmabuf->priv;
+	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
+	int r;
+
+	if (attach->dev->driver == adev->dev->driver)
+		return 0;
+
+	r = amdgpu_bo_reserve(bo, false);
+	if (unlikely(r != 0))
+		return r;
+
+	/*
+	 * We only create shared fences for internal use, but importers
+	 * of the dmabuf rely on exclusive fences for implicitly
+	 * tracking write hazards. As any of the current fences may
+	 * correspond to a write, we need to convert all existing
+	 * fences on the reservation object into a single exclusive
+	 * fence.
+	 */
+	r = __reservation_object_make_exclusive(bo->tbo.resv);
+	if (r)
+		return r;
+
+	bo->prime_shared_count++;
+	amdgpu_bo_unreserve(bo);
+	return 0;
+}
+
+/**
+ * amdgpu_dma_buf_detach - &dma_buf_ops.detach implementation
+ *
+ * @dmabuf: DMA-buf where we remove the attachment from
+ * @attach: the attachment to remove
+ *
+ * Called when an attachment is removed from the DMA-buf.
+ */
+static void amdgpu_dma_buf_detach(struct dma_buf *dmabuf,
+				  struct dma_buf_attachment *attach)
+{
+	struct drm_gem_object *obj = dmabuf->priv;
+	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
+
+	if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
+		bo->prime_shared_count--;
+}
+
+/**
+ * amdgpu_dma_buf_pin - &dma_buf_ops.pin implementation
+ *
+ * @attach: attachment to pin down
+ *
+ * Pin the BO which is backing the DMA-buf so that it can't move any more.
+ */
+static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)
+{
+	struct drm_gem_object *obj = attach->dmabuf->priv;
+	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+
+	/* pin buffer into GTT */
+	return amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+}
+
+/**
+ * amdgpu_dma_buf_unpin - &dma_buf_ops.unpin implementation
+ *
+ * @attach: attachment to unpin
+ *
+ * Unpin a previously pinned BO to make it movable again.
+ */
+static void amdgpu_dma_buf_unpin(struct dma_buf_attachment *attach)
+{
+	struct drm_gem_object *obj = attach->dmabuf->priv;
+	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+
+	amdgpu_bo_unpin(bo);
+}
+
+/**
+ * amdgpu_dma_buf_map_dma_buf - &dma_buf_ops.map_dma_buf implementation
  * @attach: DMA-buf attachment
+ * @dir: DMA direction
  *
  * Makes sure that the shared DMA buffer can be accessed by the target device.
  * For now, simply pins it to the GTT domain, where it should be accessible by
  * all DMA devices.
  *
  * Returns:
- * 0 on success or a negative error code on failure.
+ * sg_table filled with the DMA addresses to use or ERR_PRT with negative error
+ * code.
  */
-static int amdgpu_dma_buf_map_attach(struct dma_buf *dma_buf,
-				     struct dma_buf_attachment *attach)
+static struct sg_table *
+amdgpu_dma_buf_map_dma_buf(struct dma_buf_attachment *attach,
+			   enum dma_data_direction dir)
 {
+	struct dma_buf *dma_buf = attach->dmabuf;
 	struct drm_gem_object *obj = dma_buf->priv;
 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
-	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
+	struct sg_table *sgt;
 	long r;
 
-	r = drm_gem_map_attach(dma_buf, attach);
-	if (r)
-		return r;
+	if (!bo->pin_count) {
+		/* move buffer into GTT */
+		struct ttm_operation_ctx ctx = { false, false };
 
-	r = amdgpu_bo_reserve(bo, false);
-	if (unlikely(r != 0))
-		goto error_detach;
-
-
-	if (attach->dev->driver != adev->dev->driver) {
-		/*
-		 * We only create shared fences for internal use, but importers
-		 * of the dmabuf rely on exclusive fences for implicitly
-		 * tracking write hazards. As any of the current fences may
-		 * correspond to a write, we need to convert all existing
-		 * fences on the reservation object into a single exclusive
-		 * fence.
-		 */
-		r = __reservation_object_make_exclusive(bo->tbo.resv);
+		amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
+		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
 		if (r)
-			goto error_unreserve;
+			return ERR_PTR(r);
 	}
 
-	/* pin buffer into GTT */
-	r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
-	if (r)
-		goto error_unreserve;
+	sgt = drm_prime_pages_to_sg(bo->tbo.ttm->pages, bo->tbo.num_pages);
+	if (IS_ERR(sgt))
+		return sgt;
 
-	if (attach->dev->driver != adev->dev->driver)
-		bo->prime_shared_count++;
+	if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
+			      DMA_ATTR_SKIP_CPU_SYNC))
+		goto error_free;
 
-error_unreserve:
-	amdgpu_bo_unreserve(bo);
+	return sgt;
 
-error_detach:
-	if (r)
-		drm_gem_map_detach(dma_buf, attach);
-	return r;
+error_free:
+	sg_free_table(sgt);
+	kfree(sgt);
+	return ERR_PTR(-ENOMEM);
 }
 
 /**
- * amdgpu_dma_buf_map_detach - &dma_buf_ops.detach implementation
- * @dma_buf: Shared DMA buffer
+ * amdgpu_gem_unmap_dma_buf - &dma_buf_ops.unmap_dma_buf implementation
  * @attach: DMA-buf attachment
+ * @sgt: sg_table to unmap
+ * @dir: DMA direction
  *
  * This is called when a shared DMA buffer no longer needs to be accessible by
  * another device. For now, simply unpins the buffer from GTT.
  */
-static void amdgpu_dma_buf_map_detach(struct dma_buf *dma_buf,
-				      struct dma_buf_attachment *attach)
+static void amdgpu_dma_buf_unmap_dma_buf(struct dma_buf_attachment *attach,
+					 struct sg_table *sgt,
+					 enum dma_data_direction dir)
 {
-	struct drm_gem_object *obj = dma_buf->priv;
-	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
-	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
-	int ret = 0;
-
-	ret = amdgpu_bo_reserve(bo, true);
-	if (unlikely(ret != 0))
-		goto error;
+	if (!sgt)
+		return;
 
-	amdgpu_bo_unpin(bo);
-	if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
-		bo->prime_shared_count--;
-	amdgpu_bo_unreserve(bo);
-
-error:
-	drm_gem_map_detach(dma_buf, attach);
+	dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
+	sg_free_table(sgt);
+	kfree(sgt);
 }
 
 /**
@@ -322,10 +378,12 @@ static int amdgpu_dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
 }
 
 const struct dma_buf_ops amdgpu_dmabuf_ops = {
-	.attach = amdgpu_dma_buf_map_attach,
-	.detach = amdgpu_dma_buf_map_detach,
-	.map_dma_buf = drm_gem_map_dma_buf,
-	.unmap_dma_buf = drm_gem_unmap_dma_buf,
+	.attach = amdgpu_dma_buf_attach,
+	.detach = amdgpu_dma_buf_detach,
+	.pin = amdgpu_dma_buf_pin,
+	.unpin = amdgpu_dma_buf_unpin,
+	.map_dma_buf = amdgpu_dma_buf_map_dma_buf,
+	.unmap_dma_buf = amdgpu_dma_buf_unmap_dma_buf,
 	.release = drm_gem_dmabuf_release,
 	.begin_cpu_access = amdgpu_dma_buf_begin_cpu_access,
 	.mmap = drm_gem_dmabuf_mmap,
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.h +0 -1
index 7f73a4f94204..0affdfa9f22e 100644
@@ -25,7 +25,6 @@
 
 #include <drm/drm_gem.h>
 
-struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj);
 struct drm_gem_object *
 amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
 				 struct dma_buf_attachment *attach,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -1
index 8e1b269351e8..bc28515a79b0 100644
@@ -1334,7 +1334,6 @@ static struct drm_driver kms_driver = {
 	.gem_prime_export = amdgpu_gem_prime_export,
 	.gem_prime_import = amdgpu_gem_prime_import,
 	.gem_prime_res_obj = amdgpu_gem_prime_res_obj,
-	.gem_prime_get_sg_table = amdgpu_gem_prime_get_sg_table,
 	.gem_prime_import_sg_table = amdgpu_gem_prime_import_sg_table,
 	.gem_prime_vmap = amdgpu_gem_prime_vmap,
 	.gem_prime_vunmap = amdgpu_gem_prime_vunmap,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +5 -0
index 16f96f2e3671..1972fbbbb091 100644
@@ -31,6 +31,7 @@
  */
 #include <linux/list.h>
 #include <linux/slab.h>
+#include <linux/dma-buf.h>
 
 #include <drm/amdgpu_drm.h>
 #include <drm/drm_cache.h>
@@ -1194,6 +1195,10 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
 
 	amdgpu_bo_kunmap(abo);
 
+	if (abo->gem_base.dma_buf && !abo->gem_base.import_attach &&
+	    bo->mem.mem_type != TTM_PL_SYSTEM)
+		dma_buf_move_notify(abo->gem_base.dma_buf);
+
 	/* remember the eviction */
 	if (evict)
 		atomic64_inc(&adev->num_evictions);
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#6 lore raw

PATCH 6/6 drm/amdgpu: add independent DMA-buf import v7

In reply to a message not in the archive

Instead of relying on the DRM functions just implement our own import functions. This prepares support for taking care of unpinned DMA-buf.

v2: enable for all exporters, not just amdgpu, fix invalidation
handling, lock reservation object while setting callback
v3: change to new dma_buf attach interface
v4: split out from unpinned DMA-buf work
v5: rebased and cleanup on new DMA-buf interface
v6: squash with invalidation callback change,
stop using _(map|unmap)_locked
v7: drop invalidations when the BO is already in system domain

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 68 ++++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.h |  4 --
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |  1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c  |  6 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c     | 32 ++++++++--
 5 files changed, 86 insertions(+), 25 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +53 -15
index 4541eae7aadb..2630393da35c 100644
@@ -423,31 +423,28 @@ struct dma_buf *amdgpu_gem_prime_export(struct drm_gem_object *gobj,
 }
 
 /**
- * amdgpu_gem_prime_import_sg_table - &drm_driver.gem_prime_import_sg_table
- * implementation
+ * amdgpu_dma_buf_create_obj - create BO for DMA-buf import
+ *
  * @dev: DRM device
- * @attach: DMA-buf attachment
- * @sg: Scatter/gather table
+ * @dma_buf: DMA-buf
  *
- * Imports shared DMA buffer memory exported by another device.
+ * Creates an empty SG BO for DMA-buf import.
  *
  * Returns:
  * A new GEM BO of the given DRM device, representing the memory
  * described by the given DMA-buf attachment and scatter/gather table.
  */
-struct drm_gem_object *
-amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
-				 struct dma_buf_attachment *attach,
-				 struct sg_table *sg)
+static struct drm_gem_object *
+amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
 {
-	struct reservation_object *resv = attach->dmabuf->resv;
+	struct reservation_object *resv = dma_buf->resv;
 	struct amdgpu_device *adev = dev->dev_private;
 	struct amdgpu_bo *bo;
 	struct amdgpu_bo_param bp;
 	int ret;
 
 	memset(&bp, 0, sizeof(bp));
-	bp.size = attach->dmabuf->size;
+	bp.size = dma_buf->size;
 	bp.byte_align = PAGE_SIZE;
 	bp.domain = AMDGPU_GEM_DOMAIN_CPU;
 	bp.flags = 0;
@@ -458,11 +455,9 @@ amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
 	if (ret)
 		goto error;
 
-	bo->tbo.sg = sg;
-	bo->tbo.ttm->sg = sg;
 	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
 	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
-	if (attach->dmabuf->ops != &amdgpu_dmabuf_ops)
+	if (dma_buf->ops != &amdgpu_dmabuf_ops)
 		bo->prime_shared_count = 1;
 
 	ww_mutex_unlock(&resv->lock);
@@ -473,6 +468,35 @@ amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
 	return ERR_PTR(ret);
 }
 
+/**
+ * amdgpu_gem_prime_move_notify - &attach.move_notify implementation
+ *
+ * @attach: the DMA-buf attachment
+ *
+ * Invalidate the DMA-buf attachment, making sure that the we re-create the
+ * mapping before the next use.
+ */
+static void
+amdgpu_gem_prime_move_notify(struct dma_buf_attachment *attach)
+{
+	struct ttm_operation_ctx ctx = { false, false };
+	struct drm_gem_object *obj = attach->importer_priv;
+	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+	struct ttm_placement placement = {};
+	int r;
+
+	if (bo->tbo.mem.mem_type == TTM_PL_SYSTEM)
+		return;
+
+	r = ttm_bo_validate(&bo->tbo, &placement, &ctx);
+	if (r)
+		DRM_ERROR("Failed to invalidate DMA-buf import (%d))\n", r);
+}
+
+static const struct dma_buf_attach_ops amdgpu_dma_buf_attach_ops = {
+	.move_notify = amdgpu_gem_prime_move_notify
+};
+
 /**
  * amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
  * @dev: DRM device
@@ -487,6 +511,7 @@ amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
 struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
 					    struct dma_buf *dma_buf)
 {
+	struct dma_buf_attachment *attach;
 	struct drm_gem_object *obj;
 
 	if (dma_buf->ops == &amdgpu_dmabuf_ops) {
@@ -501,5 +526,18 @@ struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
 		}
 	}
 
-	return drm_gem_prime_import(dev, dma_buf);
+	obj = amdgpu_dma_buf_create_obj(dev, dma_buf);
+	if (IS_ERR(obj))
+		return obj;
+
+	attach = dma_buf_dynamic_attach(dma_buf, dev->dev,
+					&amdgpu_dma_buf_attach_ops, obj);
+	if (IS_ERR(attach)) {
+		drm_gem_object_put(obj);
+		return ERR_CAST(attach);
+	}
+
+	get_dma_buf(dma_buf);
+	obj->import_attach = attach;
+	return obj;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.h +0 -4
index 0affdfa9f22e..6afc470ca182 100644
@@ -25,10 +25,6 @@
 
 #include <drm/drm_gem.h>
 
-struct drm_gem_object *
-amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
-				 struct dma_buf_attachment *attach,
-				 struct sg_table *sg);
 struct dma_buf *amdgpu_gem_prime_export(struct drm_gem_object *gobj,
 					int flags);
 struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -1
index bc28515a79b0..836f23dac04b 100644
@@ -1334,7 +1334,6 @@ static struct drm_driver kms_driver = {
 	.gem_prime_export = amdgpu_gem_prime_export,
 	.gem_prime_import = amdgpu_gem_prime_import,
 	.gem_prime_res_obj = amdgpu_gem_prime_res_obj,
-	.gem_prime_import_sg_table = amdgpu_gem_prime_import_sg_table,
 	.gem_prime_vmap = amdgpu_gem_prime_vmap,
 	.gem_prime_vunmap = amdgpu_gem_prime_vunmap,
 	.gem_prime_mmap = amdgpu_gem_prime_mmap,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +6 -0
index 1972fbbbb091..ddb6a12c4924 100644
@@ -850,6 +850,9 @@ int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
 		return 0;
 	}
 
+	if (bo->gem_base.import_attach)
+		dma_buf_pin(bo->gem_base.import_attach);
+
 	bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
 	/* force to pin into visible video ram */
 	if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
@@ -933,6 +936,9 @@ int amdgpu_bo_unpin(struct amdgpu_bo *bo)
 
 	amdgpu_bo_subtract_pin_size(bo);
 
+	if (bo->gem_base.import_attach)
+		dma_buf_unpin(bo->gem_base.import_attach);
+
 	for (i = 0; i < bo->placement.num_placement; i++) {
 		bo->placements[i].lpfn = 0;
 		bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -5
index d81bebf76310..c89fa58a7914 100644
@@ -39,6 +39,7 @@
 #include <linux/slab.h>
 #include <linux/swap.h>
 #include <linux/swiotlb.h>
+#include <linux/dma-buf.h>
 
 #include <drm/ttm/ttm_bo_api.h>
 #include <drm/ttm/ttm_bo_driver.h>
@@ -711,6 +712,7 @@ static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
  */
 struct amdgpu_ttm_tt {
 	struct ttm_dma_tt	ttm;
+	struct drm_gem_object	*gobj;
 	u64			offset;
 	uint64_t		userptr;
 	struct task_struct	*usertask;
@@ -1198,6 +1200,7 @@ static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
 		return NULL;
 	}
 	gtt->ttm.ttm.func = &amdgpu_backend_func;
+	gtt->gobj = &ttm_to_amdgpu_bo(bo)->gem_base;
 
 	/* allocate space for the uninitialized page entries */
 	if (ttm_sg_tt_init(&gtt->ttm, bo, page_flags)) {
@@ -1218,7 +1221,6 @@ static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm,
 {
 	struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
-	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
 
 	/* user pages are bound by amdgpu_ttm_tt_pin_userptr() */
 	if (gtt && gtt->userptr) {
@@ -1231,7 +1233,19 @@ static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm,
 		return 0;
 	}
 
-	if (slave && ttm->sg) {
+	if (ttm->page_flags & TTM_PAGE_FLAG_SG) {
+		if (!ttm->sg) {
+			struct dma_buf_attachment *attach;
+			struct sg_table *sgt;
+
+			attach = gtt->gobj->import_attach;
+			sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
+			if (IS_ERR(sgt))
+				return PTR_ERR(sgt);
+
+			ttm->sg = sgt;
+		}
+
 		drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
 						 gtt->ttm.dma_address,
 						 ttm->num_pages);
@@ -1258,9 +1272,8 @@ static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm,
  */
 static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
 {
-	struct amdgpu_device *adev;
 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
-	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
+	struct amdgpu_device *adev;
 
 	if (gtt && gtt->userptr) {
 		amdgpu_ttm_tt_set_user_pages(ttm, NULL);
@@ -1269,7 +1282,16 @@ static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
 		return;
 	}
 
-	if (slave)
+	if (ttm->sg && gtt->gobj->import_attach) {
+		struct dma_buf_attachment *attach;
+
+		attach = gtt->gobj->import_attach;
+		dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL);
+		ttm->sg = NULL;
+		return;
+	}
+
+	if (ttm->page_flags & TTM_PAGE_FLAG_SG)
 		return;
 
 	adev = amdgpu_ttm_adev(ttm->bdev);
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#7 lore raw
In reply to a message not in the archive
Quoted 28 lines · Christian König

On the exporter side we add optional explicit pinning callbacks. If those callbacks are implemented the framework no longer caches sg tables and the map/unmap callbacks are always called with the lock of the reservation object held.

On the importer side we add an optional invalidate callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible.

This allows the exporter to provide the mappings without the need to pin the backing store.

v2: don't try to invalidate mappings when the callback is NULL,
lock the reservation obj while using the attachments, add helper to set the callback
v3: move flag for invalidation support into the DMA-buf,
use new attach_info structure to set the callback
v4: use importer_priv field instead of mangling exporter priv.
v5: drop invalidation_supported flag
v6: squash together with pin/unpin changes
v7: pin/unpin takes an attachment now
v8: nuke dma_buf_attachment_(map|unmap)_locked,
everything is now handled backward compatible
v9: always cache when export/importer don't agree on dynamic handling
v10: minimal style cleanup
v11: drop automatically re-entry avoidance
v12: rename callback to move_notify

Quoted 1 line

One thing I've forgotten, just stumbled over ttm_bo->moving. For pinned buffer sharing that's not needed, and I think for dynamic buffer sharing it's also not going to be the primary requirement. But I think there's two reasons we should maybe look into moving that from ttm_bo to resv_obj:

- You sound like you want to use this a lot more, even internally in amdgpu. For that I do think the sepearate dma_fence just to make sure the buffer is accessible will be needed in resv_obj.

- Once we have ->moving I think there's some good chances to extract a bit of the eviction/pipeline bo move boilerplate from ttm, and maybe use it in other drivers. i915 could already make use of this in upstream, since we already pipeline get_pages and clflush of buffers. Ofc once we have vram support, even more useful.

And doing that slight semantic change is much easier once we only have a few dynamic exporters/importers. And since it's a pure opt-in optimization (you can always fall back to the exclusive fence) it should be easy to roll out.

Thoughts about moving ttm_bo->moving to resv_obj? Ofc strictly only as a follow up. Plus maybe with a clearer name :-)

Cheers, Daniel

Quoted 474 lines
---
 drivers/dma-buf/dma-buf.c | 179 ++++++++++++++++++++++++++++++++++++--
 include/linux/dma-buf.h   | 108 +++++++++++++++++++++--
 2 files changed, 273 insertions(+), 14 deletions(-)
drivers/dma-buf/dma-buf.c +173 -6
index 6c15deb5d4ad..216f76109f3f 100644
@@ -531,6 +531,9 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
 		return ERR_PTR(-EINVAL);
 	}
 
+	if (WARN_ON(exp_info->ops->cache_sgt_mapping && exp_info->ops->pin))
+		return ERR_PTR(-EINVAL);
+
 	if (!try_module_get(exp_info->owner))
 		return ERR_PTR(-ENOENT);
 
@@ -651,10 +654,12 @@ void dma_buf_put(struct dma_buf *dmabuf)
 EXPORT_SYMBOL_GPL(dma_buf_put);
 
 /**
- * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
+ * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list; optionally,
  * calls attach() of dma_buf_ops to allow device-specific attach functionality
- * @dmabuf:	[in]	buffer to attach device to.
- * @dev:	[in]	device to be attached.
+ * @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
  *
  * Returns struct dma_buf_attachment pointer for this attachment. Attachments
  * must be cleaned up by calling dma_buf_detach().
@@ -668,8 +673,10 @@ EXPORT_SYMBOL_GPL(dma_buf_put);
  * accessible to @dev, and cannot be moved to a more suitable place. This is
  * indicated with the error code -EBUSY.
  */
-struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-					  struct device *dev)
+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 *attach;
 	int ret;
@@ -683,6 +690,8 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
 
 	attach->dev = dev;
 	attach->dmabuf = dmabuf;
+	attach->importer_ops = importer_ops;
+	attach->importer_priv = importer_priv;
 
 	mutex_lock(&dmabuf->lock);
 
@@ -691,16 +700,72 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
 		if (ret)
 			goto err_attach;
 	}
+	reservation_object_lock(dmabuf->resv, NULL);
 	list_add(&attach->node, &dmabuf->attachments);
+	reservation_object_unlock(dmabuf->resv);
 
 	mutex_unlock(&dmabuf->lock);
 
+	/* When either the importer or the exporter can't handle dynamic
+	 * mappings we cache the mapping here to avoid issues with the
+	 * reservation object lock.
+	 */
+	if (dma_buf_attachment_is_dynamic(attach) !=
+	    dma_buf_is_dynamic(dmabuf)) {
+		struct sg_table *sgt;
+
+		if (dma_buf_is_dynamic(attach->dmabuf)) {
+			reservation_object_lock(attach->dmabuf->resv, NULL);
+			ret = dma_buf_pin(attach);
+			if (ret)
+				goto err_unlock;
+		}
+
+		sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL);
+		if (!sgt)
+			sgt = ERR_PTR(-ENOMEM);
+		if (IS_ERR(sgt)) {
+			ret = PTR_ERR(sgt);
+			goto err_unpin;
+		}
+		if (dma_buf_is_dynamic(attach->dmabuf))
+			reservation_object_unlock(attach->dmabuf->resv);
+		attach->sgt = sgt;
+		attach->dir = DMA_BIDIRECTIONAL;
+	}
+
 	return attach;
 
 err_attach:
 	kfree(attach);
 	mutex_unlock(&dmabuf->lock);
 	return ERR_PTR(ret);
+
+err_unpin:
+	if (dma_buf_is_dynamic(attach->dmabuf))
+		dma_buf_unpin(attach);
+
+err_unlock:
+	if (dma_buf_is_dynamic(attach->dmabuf))
+		reservation_object_unlock(attach->dmabuf->resv);
+
+	dma_buf_detach(dmabuf, attach);
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(dma_buf_dynamic_attach);
+
+/**
+ * dma_buf_attach - Wrapper for dma_buf_dynamic_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
+ * mapping.
+ */
+struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
+					  struct device *dev)
+{
+	return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
 }
 EXPORT_SYMBOL_GPL(dma_buf_attach);
 
@@ -717,11 +782,22 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
 	if (WARN_ON(!dmabuf || !attach))
 		return;
 
-	if (attach->sgt)
+	if (attach->sgt) {
+		if (dma_buf_is_dynamic(attach->dmabuf))
+			reservation_object_lock(attach->dmabuf->resv, NULL);
+
 		dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
 
+		if (dma_buf_is_dynamic(attach->dmabuf)) {
+			dma_buf_unpin(attach);
+			reservation_object_unlock(attach->dmabuf->resv);
+		}
+	}
+
 	mutex_lock(&dmabuf->lock);
+	reservation_object_lock(dmabuf->resv, NULL);
 	list_del(&attach->node);
+	reservation_object_unlock(dmabuf->resv);
 	if (dmabuf->ops->detach)
 		dmabuf->ops->detach(dmabuf, attach);
 
@@ -730,6 +806,44 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
 }
 EXPORT_SYMBOL_GPL(dma_buf_detach);
 
+/**
+ * dma_buf_pin - Lock down the DMA-buf
+ *
+ * @attach:	[in]	attachment which should be pinned
+ *
+ * Returns:
+ * 0 on success, negative error code on failure.
+ */
+int dma_buf_pin(struct dma_buf_attachment *attach)
+{
+	struct dma_buf *dmabuf = attach->dmabuf;
+	int ret = 0;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	if (dmabuf->ops->pin)
+		ret = dmabuf->ops->pin(attach);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(dma_buf_pin);
+
+/**
+ * dma_buf_unpin - Remove lock from DMA-buf
+ *
+ * @attach:	[in]	attachment which should be unpinned
+ */
+void dma_buf_unpin(struct dma_buf_attachment *attach)
+{
+	struct dma_buf *dmabuf = attach->dmabuf;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	if (dmabuf->ops->unpin)
+		dmabuf->ops->unpin(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_unpin);
+
 /**
  * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
@@ -749,6 +863,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
 					enum dma_data_direction direction)
 {
 	struct sg_table *sg_table;
+	int r;
 
 	might_sleep();
 
@@ -767,10 +882,29 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
 		return attach->sgt;
 	}
 
+	if (dma_buf_attachment_is_dynamic(attach)) {
+		reservation_object_assert_held(attach->dmabuf->resv);
+
+	} else if (dma_buf_is_dynamic(attach->dmabuf)) {
+		reservation_object_lock(attach->dmabuf->resv, NULL);
+		r = dma_buf_pin(attach);
+		if (r) {
+			reservation_object_unlock(attach->dmabuf->resv);
+			return ERR_PTR(r);
+		}
+	}
+
 	sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
 	if (!sg_table)
 		sg_table = ERR_PTR(-ENOMEM);
 
+	if (!dma_buf_attachment_is_dynamic(attach) &&
+	    dma_buf_is_dynamic(attach->dmabuf)) {
+		if (IS_ERR(sg_table))
+			dma_buf_unpin(attach);
+		reservation_object_unlock(attach->dmabuf->resv);
+	}
+
 	if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
 		attach->sgt = sg_table;
 		attach->dir = direction;
@@ -802,10 +936,41 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
 	if (attach->sgt == sg_table)
 		return;
 
+	if (dma_buf_attachment_is_dynamic(attach))
+		reservation_object_assert_held(attach->dmabuf->resv);
+	else if (dma_buf_is_dynamic(attach->dmabuf))
+		reservation_object_lock(attach->dmabuf->resv, NULL);
+
 	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+
+	if (dma_buf_is_dynamic(attach->dmabuf) &&
+	    !dma_buf_attachment_is_dynamic(attach)) {
+		dma_buf_unpin(attach);
+		reservation_object_unlock(attach->dmabuf->resv);
+	}
 }
 EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
 
+/**
+ * dma_buf_move_notify - notify attachments that DMA-buf is moving
+ *
+ * @dmabuf:	[in]	buffer which is moving
+ *
+ * Informs all attachmenst that they need to destroy and recreated all their
+ * mappings.
+ */
+void dma_buf_move_notify(struct dma_buf *dmabuf)
+{
+	struct dma_buf_attachment *attach;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	list_for_each_entry(attach, &dmabuf->attachments, node)
+		if (attach->importer_ops && attach->importer_ops->move_notify)
+			attach->importer_ops->move_notify(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_move_notify);
+
 /**
  * DOC: cpu access
  *
@@ -1225,10 +1390,12 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 		seq_puts(s, "\tAttached Devices:\n");
 		attach_count = 0;
 
+		reservation_object_lock(buf_obj->resv, NULL);
 		list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
 			seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
 			attach_count++;
 		}
+		reservation_object_unlock(buf_obj->resv);
 
 		seq_printf(s, "Total %d devices attached\n\n",
 				attach_count);
include/linux/dma-buf.h +100 -8
index 01ad5b942a6f..ccad2fc1f640 100644
@@ -92,14 +92,40 @@ struct dma_buf_ops {
 	 */
 	void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
 
+	/**
+	 * @pin:
+	 *
+	 * This is called by dma_buf_pin and lets the exporter know that the
+	 * DMA-buf can't be moved any more.
+	 *
+	 * This is called with the dmabuf->resv object locked.
+	 *
+	 * This callback is optional.
+	 *
+	 * Returns:
+	 *
+	 * 0 on success, negative error code on failure.
+	 */
+	int (*pin)(struct dma_buf_attachment *attach);
+
+	/**
+	 * @unpin:
+	 *
+	 * This is called by dma_buf_unpin and lets the exporter know that the
+	 * DMA-buf can be moved again.
+	 *
+	 * This is called with the dmabuf->resv object locked.
+	 *
+	 * This callback is optional.
+	 */
+	void (*unpin)(struct dma_buf_attachment *attach);
+
 	/**
 	 * @map_dma_buf:
 	 *
 	 * This is called by dma_buf_map_attachment() and is used to map a
 	 * shared &dma_buf into device address space, and it is mandatory. It
-	 * can only be called if @attach has been called successfully. This
-	 * essentially pins the DMA buffer into place, and it cannot be moved
-	 * any more
+	 * can only be called if @attach has been called successfully.
 	 *
 	 * This call may sleep, e.g. when the backing storage first needs to be
 	 * allocated, or moved to a location suitable for all currently attached
@@ -120,6 +146,9 @@ struct dma_buf_ops {
 	 * any other kind of sharing that the exporter might wish to make
 	 * available to buffer-users.
 	 *
+	 * This is always called with the dmabuf->resv object locked when
+	 * the pin/unpin callbacks are implemented.
+	 *
 	 * Returns:
 	 *
 	 * A &sg_table scatter list of or the backing storage of the DMA buffer,
@@ -137,9 +166,6 @@ struct dma_buf_ops {
 	 *
 	 * This is called by dma_buf_unmap_attachment() and should unmap and
 	 * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
-	 * It should also unpin the backing storage if this is the last mapping
-	 * of the DMA buffer, it the exporter supports backing storage
-	 * migration.
 	 */
 	void (*unmap_dma_buf)(struct dma_buf_attachment *,
 			      struct sg_table *,
@@ -330,6 +356,34 @@ struct dma_buf {
 	} cb_excl, cb_shared;
 };
 
+/**
+ * struct dma_buf_attach_ops - importer operations for an attachment
+ * @move_notify: [optional] notification that the DMA-buf is moving
+ *
+ * Attachment operations implemented by the importer.
+ */
+struct dma_buf_attach_ops {
+	/**
+	 * @move_notify
+	 *
+	 * If this callback is provided the framework can avoid pinning the
+	 * backing store while mappings exists.
+	 *
+	 * This callback is called with the lock of the reservation object
+	 * associated with the dma_buf held and the mapping function must be
+	 * called with this lock held as well. This makes sure that no mapping
+	 * is created concurrently with an ongoing move operation.
+	 *
+	 * Mappings stay valid and are not directly affected by this callback.
+	 * But the DMA-buf can now be in a different physical location, so all
+	 * mappings should be destroyed and re-created as soon as possible.
+	 *
+	 * New mappings can be created after this callback returns, and will
+	 * point to the new location of the DMA-buf.
+	 */
+	void (*move_notify)(struct dma_buf_attachment *attach);
+};
+
 /**
  * struct dma_buf_attachment - holds device-buffer attachment data
  * @dmabuf: buffer for this attachment.
@@ -338,6 +392,8 @@ struct dma_buf {
  * @sgt: cached mapping.
  * @dir: direction of cached mapping.
  * @priv: exporter specific attachment data.
+ * @importer_ops: importer operations for this attachment.
+ * @importer_priv: importer specific attachment data.
  *
  * This structure holds the attachment information between the dma_buf buffer
  * and its user device(s). The list contains one attachment struct per device
@@ -355,6 +411,9 @@ struct dma_buf_attachment {
 	struct sg_table *sgt;
 	enum dma_data_direction dir;
 	void *priv;
+
+	const struct dma_buf_attach_ops *importer_ops;
+	void *importer_priv;
 };
 
 /**
@@ -405,10 +464,42 @@ static inline void get_dma_buf(struct dma_buf *dmabuf)
 	get_file(dmabuf->file);
 }
 
+/**
+ * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings.
+ * @dmabuf: the DMA-buf to check
+ *
+ * Returns true if a DMA-buf exporter wants to create dynamic sg table mappings
+ * for each attachment. False if only a single static sg table should be used.
+ */
+static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
+{
+	return !!dmabuf->ops->pin;
+}
+
+/**
+ * dma_buf_attachment_is_dynamic - check if a DMA-buf attachment uses dynamic
+ * mappinsg
+ * @attach: the DMA-buf attachment to check
+ *
+ * Returns true if a DMA-buf importer wants to use dynamic sg table mappings and
+ * calls the map/unmap functions with the reservation object locked.
+ */
+static inline bool
+dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
+{
+	return attach->importer_ops && attach->importer_ops->move_notify;
+}
+
 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-							struct device *dev);
+					  struct device *dev);
+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);
 void dma_buf_detach(struct dma_buf *dmabuf,
-				struct dma_buf_attachment *dmabuf_attach);
+		    struct dma_buf_attachment *attach);
+int dma_buf_pin(struct dma_buf_attachment *attach);
+void dma_buf_unpin(struct dma_buf_attachment *attach);
 
 struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
 
@@ -420,6 +511,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
 					enum dma_data_direction);
 void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
 				enum dma_data_direction);
+void dma_buf_move_notify(struct dma_buf *dma_buf);
 int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
 			     enum dma_data_direction dir);
 int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
Signature · 6 lines
2.17.1

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 20190625160539.GB12905-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org

#9 lore raw

Am 25.06.19 um 18:05 schrieb Daniel Vetter:

Quoted 29 lines · Christian König
Quoted 28 lines

On the exporter side we add optional explicit pinning callbacks. If those callbacks are implemented the framework no longer caches sg tables and the map/unmap callbacks are always called with the lock of the reservation object held.

On the importer side we add an optional invalidate callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible.

This allows the exporter to provide the mappings without the need to pin the backing store.

v2: don't try to invalidate mappings when the callback is NULL,
lock the reservation obj while using the attachments, add helper to set the callback
v3: move flag for invalidation support into the DMA-buf,
use new attach_info structure to set the callback
v4: use importer_priv field instead of mangling exporter priv.
v5: drop invalidation_supported flag
v6: squash together with pin/unpin changes
v7: pin/unpin takes an attachment now
v8: nuke dma_buf_attachment_(map|unmap)_locked,
everything is now handled backward compatible
v9: always cache when export/importer don't agree on dynamic handling
v10: minimal style cleanup
v11: drop automatically re-entry avoidance
v12: rename callback to move_notify

Quoted 5 lines
Quoted 1 line

One thing I've forgotten, just stumbled over ttm_bo->moving. For pinned buffer sharing that's not needed, and I think for dynamic buffer sharing it's also not going to be the primary requirement. But I think there's two reasons we should maybe look into moving that from ttm_bo to resv_obj:

That is already part of the resv_obj. The difference is that radeon is overwriting the one in the resv_obj during CS while amdgpu isn't.

So for amdgpu we keep an extra copy in ttm_bo->moving to keep the page fault handler from unnecessary waiting for a fence in radeon.

Quoted 10 lines

- You sound like you want to use this a lot more, even internally in amdgpu. For that I do think the sepearate dma_fence just to make sure the buffer is accessible will be needed in resv_obj.

- Once we have ->moving I think there's some good chances to extract a bit of the eviction/pipeline bo move boilerplate from ttm, and maybe use it in other drivers. i915 could already make use of this in upstream, since we already pipeline get_pages and clflush of buffers. Ofc once we have vram support, even more useful.

I actually indeed wanted to add more stuff to the reservation object implementation, like finally cleaning up the distinction of readers/writers.

And cleaning up the fence removal hack we have in the KFD for freed up BOs. That would also allow for getting rid of this in the long term.

Christian.

Quoted 11 lines

And doing that slight semantic change is much easier once we only have a few dynamic exporters/importers. And since it's a pure opt-in optimization (you can always fall back to the exclusive fence) it should be easy to roll out.

Thoughts about moving ttm_bo->moving to resv_obj? Ofc strictly only as a follow up. Plus maybe with a clearer name :-)

Cheers, Daniel

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 819ef4bd-e862-6390-d2e3-60f9d6c9cab4-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#11 lore raw
Quoted 30 lines · Christian König

Am 25.06.19 um 18:05 schrieb Daniel Vetter:

Quoted 29 lines · Christian König
Quoted 28 lines

On the exporter side we add optional explicit pinning callbacks. If those callbacks are implemented the framework no longer caches sg tables and the map/unmap callbacks are always called with the lock of the reservation object held.

On the importer side we add an optional invalidate callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible.

This allows the exporter to provide the mappings without the need to pin the backing store.

v2: don't try to invalidate mappings when the callback is NULL,
lock the reservation obj while using the attachments, add helper to set the callback
v3: move flag for invalidation support into the DMA-buf,
use new attach_info structure to set the callback
v4: use importer_priv field instead of mangling exporter priv.
v5: drop invalidation_supported flag
v6: squash together with pin/unpin changes
v7: pin/unpin takes an attachment now
v8: nuke dma_buf_attachment_(map|unmap)_locked,
everything is now handled backward compatible
v9: always cache when export/importer don't agree on dynamic handling
v10: minimal style cleanup
v11: drop automatically re-entry avoidance
v12: rename callback to move_notify

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

One thing I've forgotten, just stumbled over ttm_bo->moving. For pinned buffer sharing that's not needed, and I think for dynamic buffer sharing it's also not going to be the primary requirement. But I think there's two reasons we should maybe look into moving that from ttm_bo to resv_obj:

That is already part of the resv_obj. The difference is that radeon is overwriting the one in the resv_obj during CS while amdgpu isn't.

I'm confused here: Atm ->moving isn't in resv_obj, there's only one exclusive fence. And yes you need to set that every time you do a move (because a move needs to be pretty exclusive access). But I'm not seeing a separate not_quite_exclusive fence slot for moves.

Quoted 2 lines

So for amdgpu we keep an extra copy in ttm_bo->moving to keep the page fault handler from unnecessary waiting for a fence in radeon.

Yeah that's the main one. The other is in CS (at least for i915) we could run pipeline texture uploads in parallel with other rendering and stuff like that (with multiple engines, which atm is also not there yet). I think that could be somewhat useful for vk drivers.

Anyway, totally not understand what you wanted to tell me here in these two lines.

Quoted 4 lines
Quoted 4 lines

- You sound like you want to use this a lot more, even internally in amdgpu. For that I do think the sepearate dma_fence just to make sure the buffer is accessible will be needed in resv_obj.

Quoted 8 lines
Quoted 5 lines

- Once we have ->moving I think there's some good chances to extract a bit of the eviction/pipeline bo move boilerplate from ttm, and maybe use it in other drivers. i915 could already make use of this in upstream, since we already pipeline get_pages and clflush of buffers. Ofc once we have vram support, even more useful.

I actually indeed wanted to add more stuff to the reservation object implementation, like finally cleaning up the distinction of readers/writers.

Hm, more details? Not ringing a bell ...

Quoted 2 lines

And cleaning up the fence removal hack we have in the KFD for freed up BOs. That would also allow for getting rid of this in the long term.

Hm, what's that for?
-Daniel

Quoted 15 lines

Christian.

Quoted 11 lines

And doing that slight semantic change is much easier once we only have a few dynamic exporters/importers. And since it's a pure opt-in optimization (you can always fall back to the exclusive fence) it should be easy to roll out.

Thoughts about moving ttm_bo->moving to resv_obj? Ofc strictly only as a follow up. Plus maybe with a clearer name :-)

Cheers, Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 20190626081711.GH12905-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org

#13 lore raw

Am 26.06.19 um 10:17 schrieb Daniel Vetter:

Quoted 36 lines · Christian König
Quoted 35 lines

Am 25.06.19 um 18:05 schrieb Daniel Vetter:

Quoted 34 lines · Christian König
Quoted 28 lines

On the exporter side we add optional explicit pinning callbacks. If those callbacks are implemented the framework no longer caches sg tables and the map/unmap callbacks are always called with the lock of the reservation object held.

On the importer side we add an optional invalidate callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible.

This allows the exporter to provide the mappings without the need to pin the backing store.

v2: don't try to invalidate mappings when the callback is NULL,
lock the reservation obj while using the attachments, add helper to set the callback
v3: move flag for invalidation support into the DMA-buf,
use new attach_info structure to set the callback
v4: use importer_priv field instead of mangling exporter priv.
v5: drop invalidation_supported flag
v6: squash together with pin/unpin changes
v7: pin/unpin takes an attachment now
v8: nuke dma_buf_attachment_(map|unmap)_locked,
everything is now handled backward compatible
v9: always cache when export/importer don't agree on dynamic handling
v10: minimal style cleanup
v11: drop automatically re-entry avoidance
v12: rename callback to move_notify

Quoted 1 line

One thing I've forgotten, just stumbled over ttm_bo->moving. For pinned buffer sharing that's not needed, and I think for dynamic buffer sharing it's also not going to be the primary requirement. But I think there's two reasons we should maybe look into moving that from ttm_bo to resv_obj:

Quoted 6 lines
Quoted 2 lines

That is already part of the resv_obj. The difference is that radeon is overwriting the one in the resv_obj during CS while amdgpu isn't.

I'm confused here: Atm ->moving isn't in resv_obj, there's only one exclusive fence. And yes you need to set that every time you do a move (because a move needs to be pretty exclusive access). But I'm not seeing a separate not_quite_exclusive fence slot for moves.

Yeah, but shouldn't that be sufficient? I mean why does somebody else than the exporter needs to know when a BO is moving?

Quoted 9 lines
Quoted 2 lines

So for amdgpu we keep an extra copy in ttm_bo->moving to keep the page fault handler from unnecessary waiting for a fence in radeon.

Yeah that's the main one. The other is in CS (at least for i915) we could run pipeline texture uploads in parallel with other rendering and stuff like that (with multiple engines, which atm is also not there yet). I think that could be somewhat useful for vk drivers.

Anyway, totally not understand what you wanted to tell me here in these two lines.

Sorry it's 33C in my home office here and I mixed up radeon/amdgpu in the sentence above.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

- You sound like you want to use this a lot more, even internally in amdgpu. For that I do think the sepearate dma_fence just to make sure the buffer is accessible will be needed in resv_obj.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

- Once we have ->moving I think there's some good chances to extract a bit of the eviction/pipeline bo move boilerplate from ttm, and maybe use it in other drivers. i915 could already make use of this in upstream, since we already pipeline get_pages and clflush of buffers. Ofc once we have vram support, even more useful.

I actually indeed wanted to add more stuff to the reservation object implementation, like finally cleaning up the distinction of readers/writers.

Hm, more details? Not ringing a bell ...

I'm not yet sure about the details either, so please just wait until I solved that all up for me first.

Quoted 3 lines
Quoted 2 lines

And cleaning up the fence removal hack we have in the KFD for freed up BOs. That would also allow for getting rid of this in the long term.

Hm, what's that for?

When the KFD frees up memory it removes their eviction fence from the reservation object instead of setting it as signaled and adding a new one to all other used reservation objects.

Christian.

Quoted 14 lines

-Daniel

Quoted 12 lines

Christian.

Quoted 10 lines

And doing that slight semantic change is much easier once we only have a few dynamic exporters/importers. And since it's a pure opt-in optimization (you can always fall back to the exclusive fence) it should be easy to roll out.

Thoughts about moving ttm_bo->moving to resv_obj? Ofc strictly only as a follow up. Plus maybe with a clearer name :-)

Cheers, Daniel

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#14 lore raw
Quoted 40 lines · Christian

Am 26.06.19 um 10:17 schrieb Daniel Vetter:

Quoted 38 lines · Christian König
Quoted 37 lines

Am 25.06.19 um 18:05 schrieb Daniel Vetter:

Quoted 29 lines · Christian König
Quoted 28 lines

On the exporter side we add optional explicit pinning callbacks. If those callbacks are implemented the framework no longer caches sg tables and the map/unmap callbacks are always called with the lock of the reservation object held.

On the importer side we add an optional invalidate callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible.

This allows the exporter to provide the mappings without the need to pin the backing store.

v2: don't try to invalidate mappings when the callback is NULL,
lock the reservation obj while using the attachments, add helper to set the callback
v3: move flag for invalidation support into the DMA-buf,
use new attach_info structure to set the callback
v4: use importer_priv field instead of mangling exporter priv.
v5: drop invalidation_supported flag
v6: squash together with pin/unpin changes
v7: pin/unpin takes an attachment now
v8: nuke dma_buf_attachment_(map|unmap)_locked,
everything is now handled backward compatible
v9: always cache when export/importer don't agree on dynamic handling
v10: minimal style cleanup
v11: drop automatically re-entry avoidance
v12: rename callback to move_notify

Quoted 5 lines
Quoted 1 line

One thing I've forgotten, just stumbled over ttm_bo->moving. For pinned buffer sharing that's not needed, and I think for dynamic buffer sharing it's also not going to be the primary requirement. But I think there's two reasons we should maybe look into moving that from ttm_bo to resv_obj:

That is already part of the resv_obj. The difference is that radeon is overwriting the one in the resv_obj during CS while amdgpu isn't.

Quoted 7 lines
Quoted 4 lines

I'm confused here: Atm ->moving isn't in resv_obj, there's only one exclusive fence. And yes you need to set that every time you do a move (because a move needs to be pretty exclusive access). But I'm not seeing a separate not_quite_exclusive fence slot for moves.

Yeah, but shouldn't that be sufficient? I mean why does somebody else than the exporter needs to know when a BO is moving?

I think for buffer sharing there's not much use for this, but it sounded like you want to use ->move_notify also more internally. And in that case, for vk, you want to be able to ignore the implicit fences as much as possible. But you can't ignore the buffer moves ofc. Hence tracking those separate could be useful.

amdgpu seems to be solving this internally by never attaching an exclusive fence for implicit stuff, or something like that, except when it's shared. But in general you need to assume a funky mix of implicit and explicit sync'ed workloads, and for those tracking the moves separately would be good.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

So for amdgpu we keep an extra copy in ttm_bo->moving to keep the page fault handler from unnecessary waiting for a fence in radeon.

Yeah that's the main one. The other is in CS (at least for i915) we could run pipeline texture uploads in parallel with other rendering and stuff like that (with multiple engines, which atm is also not there yet). I think that could be somewhat useful for vk drivers.

Quoted 5 lines
Quoted 2 lines

Anyway, totally not understand what you wanted to tell me here in these two lines.

Sorry it's 33C in my home office here and I mixed up radeon/amdgpu in the sentence above.

Same here, and yeah makes more sense.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

- You sound like you want to use this a lot more, even internally in amdgpu. For that I do think the sepearate dma_fence just to make sure the buffer is accessible will be needed in resv_obj.

- Once we have ->moving I think there's some good chances to extract a bit of the eviction/pipeline bo move boilerplate from ttm, and maybe use it in other drivers. i915 could already make use of this in upstream, since we already pipeline get_pages and clflush of buffers. Ofc once we have vram support, even more useful.

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

I actually indeed wanted to add more stuff to the reservation object implementation, like finally cleaning up the distinction of readers/writers.

Hm, more details? Not ringing a bell ...

I'm not yet sure about the details either, so please just wait until I solved that all up for me first.

Ah is this about amdgpu doing something else for implicit sync than what's supposed to be done, and a bit a mismatch when you deal with shared buffers?

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

And cleaning up the fence removal hack we have in the KFD for freed up BOs. That would also allow for getting rid of this in the long term.

Hm, what's that for?

When the KFD frees up memory it removes their eviction fence from the reservation object instead of setting it as signaled and adding a new one to all other used reservation objects.

Oh so just a fast-path for destryoing memory that's in-flight in some move? -Daniel

Quoted 18 lines

Christian.

Quoted 14 lines

-Daniel

Quoted 12 lines

Christian.

Quoted 10 lines

And doing that slight semantic change is much easier once we only have a few dynamic exporters/importers. And since it's a pure opt-in optimization (you can always fall back to the exclusive fence) it should be easy to roll out.

Thoughts about moving ttm_bo->moving to resv_obj? Ofc strictly only as a follow up. Plus maybe with a clearer name :-)

Cheers, Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): CAKMK7uH9SmCw-pcRvMrf1OL=jYDOJ5WSR8U8hOK+Amm6bjhnkg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#16 lore raw

[SNIP]

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

I'm confused here: Atm ->moving isn't in resv_obj, there's only one

exclusive fence. And yes you need to set that every time you do a move (because a move needs to be pretty exclusive access). But I'm not seeing a separate not_quite_exclusive fence slot for moves.

Yeah, but shouldn't that be sufficient? I mean why does somebody else than the exporter needs to know when a BO is moving?

I think for buffer sharing there's not much use for this, but it sounded like you want to use ->move_notify also more internally. And in that case, for vk, you want to be able to ignore the implicit fences as much as possible. But you can't ignore the buffer moves ofc. Hence tracking those separate could be useful.

Yeah, but for this case I can still rely on using ttm_bo->moving. So no need to actually change that.

Quoted 5 lines

amdgpu seems to be solving this internally by never attaching an exclusive fence for implicit stuff, or something like that, except when it's shared. But in general you need to assume a funky mix of implicit and explicit sync'ed workloads, and for those tracking the moves separately would be good.

Actually we have an "owner" for each fence which is basically a "void*" pointer.

If we see that a command submission is coming from the same "owner" we just avoid synchronization at all.

For buffer moves the owner is simply NULL (or some other special value), and so we always sync to those.

[SNIP]

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

- You sound like you want to use this a lot more, even internally in amdgpu. For that I do think the sepearate dma_fence just to make sure the buffer is accessible will be needed in resv_obj.

- Once we have ->moving I think there's some good chances to extract a bit of the eviction/pipeline bo move boilerplate from ttm, and maybe use it in other drivers. i915 could already make use of this in upstream, since we already pipeline get_pages and clflush of buffers. Ofc once we have vram support, even more useful.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

I actually indeed wanted to add more stuff to the reservation object implementation, like finally cleaning up the distinction of readers/writers.

Hm, more details? Not ringing a bell ...

I'm not yet sure about the details either, so please just wait until I solved that all up for me first.

Ah is this about amdgpu doing something else for implicit sync than what's supposed to be done, and a bit a mismatch when you deal with shared buffers?

Yes, exactly.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

And cleaning up the fence removal hack we have in the KFD for freed up BOs. That would also allow for getting rid of this in the long term.

Hm, what's that for?

When the KFD frees up memory it removes their eviction fence from the reservation object instead of setting it as signaled and adding a new one to all other used reservation objects.

Oh so just a fast-path for destryoing memory that's in-flight in some move?

Yes exactly that again.

Christian.
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 24916a7e-e0b5-d2ed-5a7a-0d816690a063-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#18 lore raw
Quoted 8 lines · Christian König

[SNIP]

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

I'm confused here: Atm ->moving isn't in resv_obj, there's only one

exclusive fence. And yes you need to set that every time you do a move (because a move needs to be pretty exclusive access). But I'm not seeing a separate not_quite_exclusive fence slot for moves.

Yeah, but shouldn't that be sufficient? I mean why does somebody else than the exporter needs to know when a BO is moving?

Quoted 8 lines
Quoted 5 lines

I think for buffer sharing there's not much use for this, but it sounded like you want to use ->move_notify also more internally. And in that case, for vk, you want to be able to ignore the implicit fences as much as possible. But you can't ignore the buffer moves ofc. Hence tracking those separate could be useful.

Yeah, but for this case I can still rely on using ttm_bo->moving. So no need to actually change that.

Hm maybe wasn't clear what I had in mind. Roughly this:

include/drm/ttm/ttm_bo_api.h +0 -2
index 435d02f719a8..33bb4eabe2eb 100644
@@ -214,8 +214,6 @@ struct ttm_buffer_object {
         * Members protected by a bo reservation.
         */
 
-       struct dma_fence *moving;
-
        struct drm_vma_offset_node vma_node;
 
        unsigned priority;
include/linux/reservation.h +2 -0
index 644a22dbe53b..1c8a8bd077a2 100644
@@ -74,6 +74,8 @@ struct reservation_object {
        seqcount_t seq;
 
        struct dma_fence __rcu *fence_excl;
+
+       struct dma_fence _rcu *moving;
        struct reservation_object_list __rcu *fence;
 };
 

Plus all the sorted fiddling. With the idea that we'd extract a bunch of these over. Essentially instead of ttm, amdgpu and i915 all having different ways to track the bo move fences, standardize on one.

Quoted 6 lines
Quoted 5 lines

amdgpu seems to be solving this internally by never attaching an exclusive fence for implicit stuff, or something like that, except when it's shared. But in general you need to assume a funky mix of implicit and explicit sync'ed workloads, and for those tracking the moves separately would be good.

Quoted 8 lines

Actually we have an "owner" for each fence which is basically a "void*" pointer.

If we see that a command submission is coming from the same "owner" we just avoid synchronization at all.

For buffer moves the owner is simply NULL (or some other special value), and so we always sync to those.

Hm I guess I'll wait for you to untangel that then. -Daniel

Quoted 32 lines

[SNIP]

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

- You sound like you want to use this a lot more, even internally in amdgpu. For that I do think the sepearate dma_fence just to make sure the buffer is accessible will be needed in resv_obj.

- Once we have ->moving I think there's some good chances to extract a bit of the eviction/pipeline bo move boilerplate from ttm, and maybe use it in other drivers. i915 could already make use of this in upstream, since we already pipeline get_pages and clflush of buffers. Ofc once we have vram support, even more useful.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

I actually indeed wanted to add more stuff to the reservation object implementation, like finally cleaning up the distinction of readers/writers.

Hm, more details? Not ringing a bell ...

I'm not yet sure about the details either, so please just wait until I solved that all up for me first.

Ah is this about amdgpu doing something else for implicit sync than what's supposed to be done, and a bit a mismatch when you deal with shared buffers?

Yes, exactly.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

And cleaning up the fence removal hack we have in the KFD for freed up BOs. That would also allow for getting rid of this in the long term.

Hm, what's that for?

When the KFD frees up memory it removes their eviction fence from the reservation object instead of setting it as signaled and adding a new one to all other used reservation objects.

Oh so just a fast-path for destryoing memory that's in-flight in some move?

Yes exactly that again.

Christian.

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#19 lore raw

✗ Fi.CI.CHECKPATCH: warning for series starting with [1/6] dma-buf: add dynamic DMA-buf handling v12

In reply to Christian König
== Series Details ==

== Series Details ==

Series: series starting with [1/6] dma-buf: add dynamic DMA-buf handling v12
URL : https://patchwork.freedesktop.org/series/62703/
State : warning

== Summary ==

$ dim checkpatch origin/drm-tip
5e67f863406a dma-buf: add dynamic DMA-buf handling v12
-:11: WARNING:COMMIT_LOG_LONG_LINE: Possible unwrapped commit description (prefer a maximum 75 chars per line)
#11: 
map/unmap callbacks are always called with the lock of the reservation object

-:500: WARNING:NO_AUTHOR_SIGN_OFF: Missing Signed-off-by: line by nominal patch author 'Christian König <[email protected]>'

total: 0 errors, 2 warnings, 0 checks, 435 lines checked
643c70cde405 drm/ttm: remove the backing store if no placement is given
-:36: WARNING:NO_AUTHOR_SIGN_OFF: Missing Signed-off-by: line by nominal patch author 'Christian König <[email protected]>'

total: 0 errors, 1 warnings, 0 checks, 18 lines checked
bda6a595237a drm/ttm: use the parent resv for ghost objects v2
-:88: WARNING:NO_AUTHOR_SIGN_OFF: Missing Signed-off-by: line by nominal patch author 'Christian König <[email protected]>'

total: 0 errors, 1 warnings, 0 checks, 62 lines checked
45c4422ca84c drm/amdgpu: use allowed_domains for exported DMA-bufs
-:36: WARNING:NO_AUTHOR_SIGN_OFF: Missing Signed-off-by: line by nominal patch author 'Christian König <[email protected]>'

total: 0 errors, 1 warnings, 0 checks, 17 lines checked
e49369d3f0ff drm/amdgpu: add independent DMA-buf export v6
-:338: WARNING:NO_AUTHOR_SIGN_OFF: Missing Signed-off-by: line by nominal patch author 'Christian König <[email protected]>'

total: 0 errors, 1 warnings, 0 checks, 293 lines checked
60e6bdd2934e drm/amdgpu: add independent DMA-buf import v7
-:280: WARNING:NO_AUTHOR_SIGN_OFF: Missing Signed-off-by: line by nominal patch author 'Christian König <[email protected]>'

total: 0 errors, 1 warnings, 0 checks, 222 lines checked

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#20 lore raw

✗ Fi.CI.SPARSE: warning for series starting with [1/6] dma-buf: add dynamic DMA-buf handling v12

In reply to Christian König
== Series Details ==

== Series Details ==

Series: series starting with [1/6] dma-buf: add dynamic DMA-buf handling v12
URL : https://patchwork.freedesktop.org/series/62703/
State : warning

== Summary ==

$ dim sparse origin/drm-tip
Sparse version: v0.5.2
Commit: dma-buf: add dynamic DMA-buf handling v12
Okay!

Commit: drm/ttm: remove the backing store if no placement is given Okay!

Commit: drm/ttm: use the parent resv for ghost objects v2 Okay!

Commit: drm/amdgpu: use allowed_domains for exported DMA-bufs Okay!

Commit: drm/amdgpu: add independent DMA-buf export v6
-drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c:89:5: warning: symbol 'amdgpu_gem_prime_mmap' was not declared. Should it be static? -drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c:333:27: warning: symbol 'amdgpu_gem_prime_res_obj' was not declared. Should it be static? -O:drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c:324:26: warning: symbol 'amdgpu_dmabuf_ops' was not declared. Should it be static? -drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c:406:16: warning: symbol 'amdgpu_gem_prime_export' was not declared. Should it be static? -drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c:438:23: warning: symbol 'amdgpu_gem_prime_import_sg_table' was not declared. Should it be static? -drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c:487:23: warning: symbol 'amdgpu_gem_prime_import' was not declared. Should it be static? -O:drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c:49:17: warning: symbol 'amdgpu_gem_prime_get_sg_table' was not declared. Should it be static? -drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c:51:6: warning: symbol 'amdgpu_gem_prime_vmap' was not declared. Should it be static? -drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c:71:6: warning: symbol 'amdgpu_gem_prime_vunmap' was not declared. Should it be static?

Commit: drm/amdgpu: add independent DMA-buf import v7 Okay!

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#21 lore raw

✓ Fi.CI.BAT: success for series starting with [1/6] dma-buf: add dynamic DMA-buf handling v12

In reply to Christian König
== Series Details ==

== Series Details ==

Series: series starting with [1/6] dma-buf: add dynamic DMA-buf handling v12
URL : https://patchwork.freedesktop.org/series/62703/
State : success

== Summary ==

CI Bug Log - changes from CI_DRM_6342 -> Patchwork_13414
====================================================

Summary
-------

**SUCCESS**

No regressions found.

External URL: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/

Known issues
------------

Here are the changes found in Patchwork_13414 that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@debugfs_test@read_all_entries:
- fi-ilk-650: [PASS][1] -> [DMESG-WARN][2] ([fdo#106387])
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/fi-ilk-650/igt@debugfs_test@read_all_entries.html
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/fi-ilk-650/igt@debugfs_test@read_all_entries.html

* igt@gem_ctx_create@basic-files:
- fi-icl-u2: [PASS][3] -> [INCOMPLETE][4] ([fdo#107713] / [fdo#109100])
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/fi-icl-u2/igt@[email protected]
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/fi-icl-u2/igt@[email protected]

* igt@gem_mmap_gtt@basic-wc:
- fi-icl-u3: [PASS][5] -> [DMESG-WARN][6] ([fdo#107724])
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/fi-icl-u3/igt@[email protected]
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/fi-icl-u3/igt@[email protected]

* igt@i915_module_load@reload:
- fi-blb-e6850: [PASS][7] -> [INCOMPLETE][8] ([fdo#107718])
[7]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/fi-blb-e6850/igt@[email protected]
[8]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/fi-blb-e6850/igt@[email protected]

* igt@kms_busy@basic-flip-a:
- fi-kbl-7567u: [PASS][9] -> [SKIP][10] ([fdo#109271] / [fdo#109278]) +2 similar issues
[9]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/fi-kbl-7567u/igt@[email protected]
[10]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/fi-kbl-7567u/igt@[email protected]

#### Possible fixes ####

* igt@i915_selftest@live_blt:
- fi-skl-iommu: [INCOMPLETE][11] ([fdo#108602]) -> [PASS][12]
[11]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/fi-skl-iommu/igt@i915_selftest@live_blt.html
[12]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/fi-skl-iommu/igt@i915_selftest@live_blt.html

* igt@i915_selftest@live_sanitycheck:
- fi-icl-u3: [DMESG-WARN][13] ([fdo#107724]) -> [PASS][14] +1 similar issue
[13]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/fi-icl-u3/igt@i915_selftest@live_sanitycheck.html
[14]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/fi-icl-u3/igt@i915_selftest@live_sanitycheck.html

[fdo#106387]: https://bugs.freedesktop.org/show_bug.cgi?id=106387
[fdo#107713]: https://bugs.freedesktop.org/show_bug.cgi?id=107713
[fdo#107718]: https://bugs.freedesktop.org/show_bug.cgi?id=107718
[fdo#107724]: https://bugs.freedesktop.org/show_bug.cgi?id=107724
[fdo#108602]: https://bugs.freedesktop.org/show_bug.cgi?id=108602
[fdo#109100]: https://bugs.freedesktop.org/show_bug.cgi?id=109100
[fdo#109271]: https://bugs.freedesktop.org/show_bug.cgi?id=109271
[fdo#109278]: https://bugs.freedesktop.org/show_bug.cgi?id=109278

Participating hosts (53 -> 42)
------------------------------

Missing (11): fi-ilk-m540 fi-hsw-4200u fi-byt-squawks fi-bsw-cyan fi-bwr-2160 fi-kbl-7500u fi-icl-y fi-bdw-samus fi-icl-guc fi-byt-clapper fi-skl-6600u

Build changes
-------------

* Linux: CI_DRM_6342 -> Patchwork_13414

CI_DRM_6342: 6eef272b254b34200129af8f2ec1e4cfe1ca6bff @ git://anongit.freedesktop.org/gfx-ci/linux
IGT_5067: 5eafa33dbdb1d3c190ac5060161c45152e9a298e @ git://anongit.freedesktop.org/xorg/app/intel-gpu-tools
Patchwork_13414: 60e6bdd2934e7d4c6f17e4a5684854401d9be812 @ git://anongit.freedesktop.org/gfx-ci/linux

== Linux commits ==

60e6bdd2934e drm/amdgpu: add independent DMA-buf import v7 e49369d3f0ff drm/amdgpu: add independent DMA-buf export v6 45c4422ca84c drm/amdgpu: use allowed_domains for exported DMA-bufs bda6a595237a drm/ttm: use the parent resv for ghost objects v2 643c70cde405 drm/ttm: remove the backing store if no placement is given 5e67f863406a dma-buf: add dynamic DMA-buf handling v12

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#22 lore raw
In reply to Christian König

Hi Christian,

Quoted 167 lines · Christian König

On the exporter side we add optional explicit pinning callbacks. If those callbacks are implemented the framework no longer caches sg tables and the map/unmap callbacks are always called with the lock of the reservation object held.

On the importer side we add an optional invalidate callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible.

This allows the exporter to provide the mappings without the need to pin the backing store.

v2: don't try to invalidate mappings when the callback is NULL,
lock the reservation obj while using the attachments, add helper to set the callback
v3: move flag for invalidation support into the DMA-buf,
use new attach_info structure to set the callback
v4: use importer_priv field instead of mangling exporter priv.
v5: drop invalidation_supported flag
v6: squash together with pin/unpin changes
v7: pin/unpin takes an attachment now
v8: nuke dma_buf_attachment_(map|unmap)_locked,
everything is now handled backward compatible
v9: always cache when export/importer don't agree on dynamic handling
v10: minimal style cleanup
v11: drop automatically re-entry avoidance
v12: rename callback to move_notify

---
 drivers/dma-buf/dma-buf.c | 179 ++++++++++++++++++++++++++++++++++++--
 include/linux/dma-buf.h   | 108 +++++++++++++++++++++--
 2 files changed, 273 insertions(+), 14 deletions(-)
drivers/dma-buf/dma-buf.c +73 -6
index 6c15deb5d4ad..216f76109f3f 100644
@@ -531,6 +531,9 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
 		return ERR_PTR(-EINVAL);
 	}
 
+	if (WARN_ON(exp_info->ops->cache_sgt_mapping && exp_info->ops->pin))
+		return ERR_PTR(-EINVAL);
+
 	if (!try_module_get(exp_info->owner))
 		return ERR_PTR(-ENOENT);
 
@@ -651,10 +654,12 @@ void dma_buf_put(struct dma_buf *dmabuf)
 EXPORT_SYMBOL_GPL(dma_buf_put);
 
 /**
- * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
+ * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list; optionally,
  * calls attach() of dma_buf_ops to allow device-specific attach functionality
- * @dmabuf:	[in]	buffer to attach device to.
- * @dev:	[in]	device to be attached.
+ * @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
  *
  * Returns struct dma_buf_attachment pointer for this attachment. Attachments
  * must be cleaned up by calling dma_buf_detach().
@@ -668,8 +673,10 @@ EXPORT_SYMBOL_GPL(dma_buf_put);
  * accessible to @dev, and cannot be moved to a more suitable place. This is
  * indicated with the error code -EBUSY.
  */
-struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-					  struct device *dev)
+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 *attach;
 	int ret;
@@ -683,6 +690,8 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
 
 	attach->dev = dev;
 	attach->dmabuf = dmabuf;
+	attach->importer_ops = importer_ops;
+	attach->importer_priv = importer_priv;
 
 	mutex_lock(&dmabuf->lock);
 
@@ -691,16 +700,72 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
 		if (ret)
 			goto err_attach;
 	}
+	reservation_object_lock(dmabuf->resv, NULL);
 	list_add(&attach->node, &dmabuf->attachments);
+	reservation_object_unlock(dmabuf->resv);
 
 	mutex_unlock(&dmabuf->lock);
 
+	/* When either the importer or the exporter can't handle dynamic
+	 * mappings we cache the mapping here to avoid issues with the
+	 * reservation object lock.
+	 */
+	if (dma_buf_attachment_is_dynamic(attach) !=
+	    dma_buf_is_dynamic(dmabuf)) {
+		struct sg_table *sgt;
+
+		if (dma_buf_is_dynamic(attach->dmabuf)) {
+			reservation_object_lock(attach->dmabuf->resv, NULL);
+			ret = dma_buf_pin(attach);
+			if (ret)
+				goto err_unlock;
+		}
+
+		sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL);
+		if (!sgt)
+			sgt = ERR_PTR(-ENOMEM);
+		if (IS_ERR(sgt)) {
+			ret = PTR_ERR(sgt);
+			goto err_unpin;
+		}
+		if (dma_buf_is_dynamic(attach->dmabuf))
+			reservation_object_unlock(attach->dmabuf->resv);
+		attach->sgt = sgt;
+		attach->dir = DMA_BIDIRECTIONAL;
+	}
+
 	return attach;
 
 err_attach:
 	kfree(attach);
 	mutex_unlock(&dmabuf->lock);
 	return ERR_PTR(ret);
+
+err_unpin:
+	if (dma_buf_is_dynamic(attach->dmabuf))
+		dma_buf_unpin(attach);
+
+err_unlock:
+	if (dma_buf_is_dynamic(attach->dmabuf))
+		reservation_object_unlock(attach->dmabuf->resv);
+
+	dma_buf_detach(dmabuf, attach);
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(dma_buf_dynamic_attach);
+
+/**
+ * dma_buf_attach - Wrapper for dma_buf_dynamic_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
+ * mapping.
+ */
+struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
+					  struct device *dev)
+{
+	return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
 }
 EXPORT_SYMBOL_GPL(dma_buf_attach);
 
@@ -717,11 +782,22 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
 	if (WARN_ON(!dmabuf || !attach))
 		return;
 
-	if (attach->sgt)
+	if (attach->sgt) {
+		if (dma_buf_is_dynamic(attach->dmabuf))
+			reservation_object_lock(attach->dmabuf->resv, NULL);

drivers/dma-buf/dma-buf.c void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) · line 797

Quoted 10 lines
drivers/dma-buf/dma-buf.c +7 -0
+
 		dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
 
+		if (dma_buf_is_dynamic(attach->dmabuf)) {
+			dma_buf_unpin(attach);
+			reservation_object_unlock(attach->dmabuf->resv);
+		}
+	}
+
 	mutex_lock(&dmabuf->lock);

Time to ditch dmabuf->lock in favour of the reservation obj? We have a fallback resv_obj in struct dma_buf already, so this is never null, and I think would clean up the code a bit.

Quoted 64 lines
drivers/dma-buf/dma-buf.c +42 -0
+	reservation_object_lock(dmabuf->resv, NULL);
 	list_del(&attach->node);
+	reservation_object_unlock(dmabuf->resv);
 	if (dmabuf->ops->detach)
 		dmabuf->ops->detach(dmabuf, attach);
 
@@ -730,6 +806,44 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
 }
 EXPORT_SYMBOL_GPL(dma_buf_detach);
 
+/**
+ * dma_buf_pin - Lock down the DMA-buf
+ *
+ * @attach:	[in]	attachment which should be pinned
+ *
+ * Returns:
+ * 0 on success, negative error code on failure.
+ */
+int dma_buf_pin(struct dma_buf_attachment *attach)
+{
+	struct dma_buf *dmabuf = attach->dmabuf;
+	int ret = 0;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	if (dmabuf->ops->pin)
+		ret = dmabuf->ops->pin(attach);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(dma_buf_pin);
+
+/**
+ * dma_buf_unpin - Remove lock from DMA-buf
+ *
+ * @attach:	[in]	attachment which should be unpinned
+ */
+void dma_buf_unpin(struct dma_buf_attachment *attach)
+{
+	struct dma_buf *dmabuf = attach->dmabuf;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	if (dmabuf->ops->unpin)
+		dmabuf->ops->unpin(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_unpin);
+
 /**
  * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
@@ -749,6 +863,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
 					enum dma_data_direction direction)
 {
 	struct sg_table *sg_table;
+	int r;
 
 	might_sleep();
 
@@ -767,10 +882,29 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
 		return attach->sgt;
 	}
 
+	if (dma_buf_attachment_is_dynamic(attach)) {

drivers/dma-buf/dma-buf.c struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, · line 895

Quoted 10 lines
drivers/dma-buf/dma-buf.c +10 -0
+		reservation_object_assert_held(attach->dmabuf->resv);
+
+	} else if (dma_buf_is_dynamic(attach->dmabuf)) {
+		reservation_object_lock(attach->dmabuf->resv, NULL);
+		r = dma_buf_pin(attach);
+		if (r) {
+			reservation_object_unlock(attach->dmabuf->resv);
+			return ERR_PTR(r);
+		}
+	}

With this design (because we can't cache at attach time unconditionally) we inflict the reservation lock on all importers. So needs:

	} else {
		/* neither importer nor exporter are dynamice */
		might_lock(dmabuf->resv->ww_mutex);
	}

Same for dma_buf_unmap_attachment.

I also expect that this will blow up in intel-gfx-ci :-/ I did look at previous intel-gfx-ci runs of your series, and the last one that did pass was the one that still had the caching in _attach():

Geez I was sooooooooo locking forward to slapping an r-b on this and volunteering myself to polish the kerneldoc (which isn't great, but also pre-existing condition plus on my todo list anyway) :-(

Quoted 58 lines
drivers/dma-buf/dma-buf.c +39 -0
+
 	sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
 	if (!sg_table)
 		sg_table = ERR_PTR(-ENOMEM);
 
+	if (!dma_buf_attachment_is_dynamic(attach) &&
+	    dma_buf_is_dynamic(attach->dmabuf)) {
+		if (IS_ERR(sg_table))
+			dma_buf_unpin(attach);
+		reservation_object_unlock(attach->dmabuf->resv);
+	}
+
 	if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
 		attach->sgt = sg_table;
 		attach->dir = direction;
@@ -802,10 +936,41 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
 	if (attach->sgt == sg_table)
 		return;
 
+	if (dma_buf_attachment_is_dynamic(attach))
+		reservation_object_assert_held(attach->dmabuf->resv);
+	else if (dma_buf_is_dynamic(attach->dmabuf))
+		reservation_object_lock(attach->dmabuf->resv, NULL);
+
 	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+
+	if (dma_buf_is_dynamic(attach->dmabuf) &&
+	    !dma_buf_attachment_is_dynamic(attach)) {
+		dma_buf_unpin(attach);
+		reservation_object_unlock(attach->dmabuf->resv);
+	}
 }
 EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
 
+/**
+ * dma_buf_move_notify - notify attachments that DMA-buf is moving
+ *
+ * @dmabuf:	[in]	buffer which is moving
+ *
+ * Informs all attachmenst that they need to destroy and recreated all their
+ * mappings.
+ */
+void dma_buf_move_notify(struct dma_buf *dmabuf)
+{
+	struct dma_buf_attachment *attach;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	list_for_each_entry(attach, &dmabuf->attachments, node)
+		if (attach->importer_ops && attach->importer_ops->move_notify)
+			attach->importer_ops->move_notify(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_move_notify);
+
 /**
  * DOC: cpu access
  *

drivers/dma-buf/dma-buf.c static int dma_buf_debug_show(struct seq_file *s, void *unused) · line 1398

Quoted 9 lines
drivers/dma-buf/dma-buf.c +2 -0
@@ -1225,10 +1390,12 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 		seq_puts(s, "\tAttached Devices:\n");
 		attach_count = 0;
 
+		reservation_object_lock(buf_obj->resv, NULL);
 		list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
 			seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
 			attach_count++;
 		}
+		reservation_object_unlock(buf_obj->resv);

Yeah definitely time to retire dmabuf->lock I think.

Cheers, Daniel

Quoted 185 lines
drivers/dma-buf/dma-buf.c +0 -0
 		seq_printf(s, "Total %d devices attached\n\n",
 				attach_count);
include/linux/dma-buf.h +100 -8
index 01ad5b942a6f..ccad2fc1f640 100644
@@ -92,14 +92,40 @@ struct dma_buf_ops {
 	 */
 	void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
 
+	/**
+	 * @pin:
+	 *
+	 * This is called by dma_buf_pin and lets the exporter know that the
+	 * DMA-buf can't be moved any more.
+	 *
+	 * This is called with the dmabuf->resv object locked.
+	 *
+	 * This callback is optional.
+	 *
+	 * Returns:
+	 *
+	 * 0 on success, negative error code on failure.
+	 */
+	int (*pin)(struct dma_buf_attachment *attach);
+
+	/**
+	 * @unpin:
+	 *
+	 * This is called by dma_buf_unpin and lets the exporter know that the
+	 * DMA-buf can be moved again.
+	 *
+	 * This is called with the dmabuf->resv object locked.
+	 *
+	 * This callback is optional.
+	 */
+	void (*unpin)(struct dma_buf_attachment *attach);
+
 	/**
 	 * @map_dma_buf:
 	 *
 	 * This is called by dma_buf_map_attachment() and is used to map a
 	 * shared &dma_buf into device address space, and it is mandatory. It
-	 * can only be called if @attach has been called successfully. This
-	 * essentially pins the DMA buffer into place, and it cannot be moved
-	 * any more
+	 * can only be called if @attach has been called successfully.
 	 *
 	 * This call may sleep, e.g. when the backing storage first needs to be
 	 * allocated, or moved to a location suitable for all currently attached
@@ -120,6 +146,9 @@ struct dma_buf_ops {
 	 * any other kind of sharing that the exporter might wish to make
 	 * available to buffer-users.
 	 *
+	 * This is always called with the dmabuf->resv object locked when
+	 * the pin/unpin callbacks are implemented.
+	 *
 	 * Returns:
 	 *
 	 * A &sg_table scatter list of or the backing storage of the DMA buffer,
@@ -137,9 +166,6 @@ struct dma_buf_ops {
 	 *
 	 * This is called by dma_buf_unmap_attachment() and should unmap and
 	 * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
-	 * It should also unpin the backing storage if this is the last mapping
-	 * of the DMA buffer, it the exporter supports backing storage
-	 * migration.
 	 */
 	void (*unmap_dma_buf)(struct dma_buf_attachment *,
 			      struct sg_table *,
@@ -330,6 +356,34 @@ struct dma_buf {
 	} cb_excl, cb_shared;
 };
 
+/**
+ * struct dma_buf_attach_ops - importer operations for an attachment
+ * @move_notify: [optional] notification that the DMA-buf is moving
+ *
+ * Attachment operations implemented by the importer.
+ */
+struct dma_buf_attach_ops {
+	/**
+	 * @move_notify
+	 *
+	 * If this callback is provided the framework can avoid pinning the
+	 * backing store while mappings exists.
+	 *
+	 * This callback is called with the lock of the reservation object
+	 * associated with the dma_buf held and the mapping function must be
+	 * called with this lock held as well. This makes sure that no mapping
+	 * is created concurrently with an ongoing move operation.
+	 *
+	 * Mappings stay valid and are not directly affected by this callback.
+	 * But the DMA-buf can now be in a different physical location, so all
+	 * mappings should be destroyed and re-created as soon as possible.
+	 *
+	 * New mappings can be created after this callback returns, and will
+	 * point to the new location of the DMA-buf.
+	 */
+	void (*move_notify)(struct dma_buf_attachment *attach);
+};
+
 /**
  * struct dma_buf_attachment - holds device-buffer attachment data
  * @dmabuf: buffer for this attachment.
@@ -338,6 +392,8 @@ struct dma_buf {
  * @sgt: cached mapping.
  * @dir: direction of cached mapping.
  * @priv: exporter specific attachment data.
+ * @importer_ops: importer operations for this attachment.
+ * @importer_priv: importer specific attachment data.
  *
  * This structure holds the attachment information between the dma_buf buffer
  * and its user device(s). The list contains one attachment struct per device
@@ -355,6 +411,9 @@ struct dma_buf_attachment {
 	struct sg_table *sgt;
 	enum dma_data_direction dir;
 	void *priv;
+
+	const struct dma_buf_attach_ops *importer_ops;
+	void *importer_priv;
 };
 
 /**
@@ -405,10 +464,42 @@ static inline void get_dma_buf(struct dma_buf *dmabuf)
 	get_file(dmabuf->file);
 }
 
+/**
+ * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings.
+ * @dmabuf: the DMA-buf to check
+ *
+ * Returns true if a DMA-buf exporter wants to create dynamic sg table mappings
+ * for each attachment. False if only a single static sg table should be used.
+ */
+static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
+{
+	return !!dmabuf->ops->pin;
+}
+
+/**
+ * dma_buf_attachment_is_dynamic - check if a DMA-buf attachment uses dynamic
+ * mappinsg
+ * @attach: the DMA-buf attachment to check
+ *
+ * Returns true if a DMA-buf importer wants to use dynamic sg table mappings and
+ * calls the map/unmap functions with the reservation object locked.
+ */
+static inline bool
+dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
+{
+	return attach->importer_ops && attach->importer_ops->move_notify;
+}
+
 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-							struct device *dev);
+					  struct device *dev);
+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);
 void dma_buf_detach(struct dma_buf *dmabuf,
-				struct dma_buf_attachment *dmabuf_attach);
+		    struct dma_buf_attachment *attach);
+int dma_buf_pin(struct dma_buf_attachment *attach);
+void dma_buf_unpin(struct dma_buf_attachment *attach);
 
 struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
 
@@ -420,6 +511,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
 					enum dma_data_direction);
 void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
 				enum dma_data_direction);
+void dma_buf_move_notify(struct dma_buf *dma_buf);
 int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
 			     enum dma_data_direction dir);
 int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
Signature · 6 lines
2.17.1

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

A message that is not in the archive (sent off-list or not yet received): 20190625143548.GX12905-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org

#24 lore raw

Am 25.06.19 um 16:35 schrieb Daniel Vetter:

Quoted 173 lines

Hi Christian,

Quoted 170 lines · Christian König

On the exporter side we add optional explicit pinning callbacks. If those callbacks are implemented the framework no longer caches sg tables and the map/unmap callbacks are always called with the lock of the reservation object held.

On the importer side we add an optional invalidate callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible.

This allows the exporter to provide the mappings without the need to pin the backing store.

v2: don't try to invalidate mappings when the callback is NULL,
lock the reservation obj while using the attachments, add helper to set the callback
v3: move flag for invalidation support into the DMA-buf,
use new attach_info structure to set the callback
v4: use importer_priv field instead of mangling exporter priv.
v5: drop invalidation_supported flag
v6: squash together with pin/unpin changes
v7: pin/unpin takes an attachment now
v8: nuke dma_buf_attachment_(map|unmap)_locked,
everything is now handled backward compatible
v9: always cache when export/importer don't agree on dynamic handling
v10: minimal style cleanup
v11: drop automatically re-entry avoidance
v12: rename callback to move_notify

---
  drivers/dma-buf/dma-buf.c | 179 ++++++++++++++++++++++++++++++++++++--
  include/linux/dma-buf.h   | 108 +++++++++++++++++++++--
  2 files changed, 273 insertions(+), 14 deletions(-)
drivers/dma-buf/dma-buf.c +74 -6
index 6c15deb5d4ad..216f76109f3f 100644
@@ -531,6 +531,9 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
  		return ERR_PTR(-EINVAL);
  	}
 
+	if (WARN_ON(exp_info->ops->cache_sgt_mapping && exp_info->ops->pin))
+		return ERR_PTR(-EINVAL);
+
  	if (!try_module_get(exp_info->owner))
  		return ERR_PTR(-ENOENT);
 
@@ -651,10 +654,12 @@ void dma_buf_put(struct dma_buf *dmabuf)
  EXPORT_SYMBOL_GPL(dma_buf_put);
 
  /**
- * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
+ * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list; optionally,
   * calls attach() of dma_buf_ops to allow device-specific attach functionality
- * @dmabuf:	[in]	buffer to attach device to.
- * @dev:	[in]	device to be attached.
+ * @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
   *
   * Returns struct dma_buf_attachment pointer for this attachment. Attachments
   * must be cleaned up by calling dma_buf_detach().
@@ -668,8 +673,10 @@ EXPORT_SYMBOL_GPL(dma_buf_put);
   * accessible to @dev, and cannot be moved to a more suitable place. This is
   * indicated with the error code -EBUSY.
   */
-struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-					  struct device *dev)
+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 *attach;
  	int ret;
@@ -683,6 +690,8 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
 
  	attach->dev = dev;
  	attach->dmabuf = dmabuf;
+	attach->importer_ops = importer_ops;
+	attach->importer_priv = importer_priv;
 
  	mutex_lock(&dmabuf->lock);
 
@@ -691,16 +700,72 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  		if (ret)
  			goto err_attach;
  	}
+	reservation_object_lock(dmabuf->resv, NULL);
  	list_add(&attach->node, &dmabuf->attachments);
+	reservation_object_unlock(dmabuf->resv);
 
  	mutex_unlock(&dmabuf->lock);
 
+	/* When either the importer or the exporter can't handle dynamic
+	 * mappings we cache the mapping here to avoid issues with the
+	 * reservation object lock.
+	 */
+	if (dma_buf_attachment_is_dynamic(attach) !=
+	    dma_buf_is_dynamic(dmabuf)) {
+		struct sg_table *sgt;
+
+		if (dma_buf_is_dynamic(attach->dmabuf)) {
+			reservation_object_lock(attach->dmabuf->resv, NULL);
+			ret = dma_buf_pin(attach);
+			if (ret)
+				goto err_unlock;
+		}
+
+		sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL);
+		if (!sgt)
+			sgt = ERR_PTR(-ENOMEM);
+		if (IS_ERR(sgt)) {
+			ret = PTR_ERR(sgt);
+			goto err_unpin;
+		}
+		if (dma_buf_is_dynamic(attach->dmabuf))
+			reservation_object_unlock(attach->dmabuf->resv);
+		attach->sgt = sgt;
+		attach->dir = DMA_BIDIRECTIONAL;
+	}
+
  	return attach;
 
  err_attach:
  	kfree(attach);
  	mutex_unlock(&dmabuf->lock);
  	return ERR_PTR(ret);
+
+err_unpin:
+	if (dma_buf_is_dynamic(attach->dmabuf))
+		dma_buf_unpin(attach);
+
+err_unlock:
+	if (dma_buf_is_dynamic(attach->dmabuf))
+		reservation_object_unlock(attach->dmabuf->resv);
+
+	dma_buf_detach(dmabuf, attach);
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(dma_buf_dynamic_attach);
+
+/**
+ * dma_buf_attach - Wrapper for dma_buf_dynamic_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
+ * mapping.
+ */
+struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
+					  struct device *dev)
+{
+	return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
  }
  EXPORT_SYMBOL_GPL(dma_buf_attach);
 
@@ -717,11 +782,22 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  	if (WARN_ON(!dmabuf || !attach))
  		return;
 
-	if (attach->sgt)
+	if (attach->sgt) {
+		if (dma_buf_is_dynamic(attach->dmabuf))
+			reservation_object_lock(attach->dmabuf->resv, NULL);
+
  		dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
 
Quoted 10 lines

drivers/dma-buf/dma-buf.c void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) · line 797

Quoted 7 lines
drivers/dma-buf/dma-buf.c +6 -0
+		if (dma_buf_is_dynamic(attach->dmabuf)) {
+			dma_buf_unpin(attach);
+			reservation_object_unlock(attach->dmabuf->resv);
+		}
+	}
+
  	mutex_lock(&dmabuf->lock);

Time to ditch dmabuf->lock in favour of the reservation obj? We have a fallback resv_obj in struct dma_buf already, so this is never null, and I think would clean up the code a bit.

Yeah, thought about that as well. But then decided against it for now.

Key point is that exporters currently doesn't care about dmabuf->lock, but they do care about the reservation lock.

So we will probably have a bunch of cases where we have to fix up exporters because they will try to grab the reservation lock as well.

On the other hand we maybe not need a lock at all here if we just can live with multiple attach/detach callbacks running in parallel.

Quoted 79 lines
Quoted 66 lines
drivers/dma-buf/dma-buf.c +42 -0
+	reservation_object_lock(dmabuf->resv, NULL);
  	list_del(&attach->node);
+	reservation_object_unlock(dmabuf->resv);
  	if (dmabuf->ops->detach)
  		dmabuf->ops->detach(dmabuf, attach);
 
@@ -730,6 +806,44 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  }
  EXPORT_SYMBOL_GPL(dma_buf_detach);
 
+/**
+ * dma_buf_pin - Lock down the DMA-buf
+ *
+ * @attach:	[in]	attachment which should be pinned
+ *
+ * Returns:
+ * 0 on success, negative error code on failure.
+ */
+int dma_buf_pin(struct dma_buf_attachment *attach)
+{
+	struct dma_buf *dmabuf = attach->dmabuf;
+	int ret = 0;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	if (dmabuf->ops->pin)
+		ret = dmabuf->ops->pin(attach);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(dma_buf_pin);
+
+/**
+ * dma_buf_unpin - Remove lock from DMA-buf
+ *
+ * @attach:	[in]	attachment which should be unpinned
+ */
+void dma_buf_unpin(struct dma_buf_attachment *attach)
+{
+	struct dma_buf *dmabuf = attach->dmabuf;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	if (dmabuf->ops->unpin)
+		dmabuf->ops->unpin(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_unpin);
+
  /**
   * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
   * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
@@ -749,6 +863,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  					enum dma_data_direction direction)
  {
  	struct sg_table *sg_table;
+	int r;
 
  	might_sleep();
 
@@ -767,10 +882,29 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  		return attach->sgt;
  	}
 
+	if (dma_buf_attachment_is_dynamic(attach)) {

drivers/dma-buf/dma-buf.c struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, · line 895

Quoted 10 lines
drivers/dma-buf/dma-buf.c +10 -0
+		reservation_object_assert_held(attach->dmabuf->resv);
+
+	} else if (dma_buf_is_dynamic(attach->dmabuf)) {
+		reservation_object_lock(attach->dmabuf->resv, NULL);
+		r = dma_buf_pin(attach);
+		if (r) {
+			reservation_object_unlock(attach->dmabuf->resv);
+			return ERR_PTR(r);
+		}
+	}

With this design (because we can't cache at attach time unconditionally) we inflict the reservation lock on all importers. So needs:

Quoted 6 lines
	} else {
		/* neither importer nor exporter are dynamice */
		might_lock(dmabuf->resv->ww_mutex);
	}

Same for dma_buf_unmap_attachment.

Good idea.

Quoted 4 lines

I also expect that this will blow up in intel-gfx-ci :-/ I did look at previous intel-gfx-ci runs of your series, and the last one that did pass was the one that still had the caching in _attach():

Well as far as I can see the current one passes as well. Actually just recently found a typo because of this.

Quoted 4 lines

Geez I was sooooooooo locking forward to slapping an r-b on this and volunteering myself to polish the kerneldoc (which isn't great, but also pre-existing condition plus on my todo list anyway) :-(

Going to add the might_lock and send again, let's see what intel-gfx-ci has say to this.

Christian.

Quoted 253 lines
Quoted 59 lines
drivers/dma-buf/dma-buf.c +39 -0
+
  	sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
  	if (!sg_table)
  		sg_table = ERR_PTR(-ENOMEM);
 
+	if (!dma_buf_attachment_is_dynamic(attach) &&
+	    dma_buf_is_dynamic(attach->dmabuf)) {
+		if (IS_ERR(sg_table))
+			dma_buf_unpin(attach);
+		reservation_object_unlock(attach->dmabuf->resv);
+	}
+
  	if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
  		attach->sgt = sg_table;
  		attach->dir = direction;
@@ -802,10 +936,41 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
  	if (attach->sgt == sg_table)
  		return;
 
+	if (dma_buf_attachment_is_dynamic(attach))
+		reservation_object_assert_held(attach->dmabuf->resv);
+	else if (dma_buf_is_dynamic(attach->dmabuf))
+		reservation_object_lock(attach->dmabuf->resv, NULL);
+
  	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+
+	if (dma_buf_is_dynamic(attach->dmabuf) &&
+	    !dma_buf_attachment_is_dynamic(attach)) {
+		dma_buf_unpin(attach);
+		reservation_object_unlock(attach->dmabuf->resv);
+	}
  }
  EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
 
+/**
+ * dma_buf_move_notify - notify attachments that DMA-buf is moving
+ *
+ * @dmabuf:	[in]	buffer which is moving
+ *
+ * Informs all attachmenst that they need to destroy and recreated all their
+ * mappings.
+ */
+void dma_buf_move_notify(struct dma_buf *dmabuf)
+{
+	struct dma_buf_attachment *attach;
+
+	reservation_object_assert_held(dmabuf->resv);
+
+	list_for_each_entry(attach, &dmabuf->attachments, node)
+		if (attach->importer_ops && attach->importer_ops->move_notify)
+			attach->importer_ops->move_notify(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_move_notify);
+
  /**
   * DOC: cpu access
   *

drivers/dma-buf/dma-buf.c static int dma_buf_debug_show(struct seq_file *s, void *unused) · line 1398

Quoted 9 lines
drivers/dma-buf/dma-buf.c +2 -0
@@ -1225,10 +1390,12 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
  		seq_puts(s, "\tAttached Devices:\n");
  		attach_count = 0;
 
+		reservation_object_lock(buf_obj->resv, NULL);
  		list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
  			seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
  			attach_count++;
  		}
+		reservation_object_unlock(buf_obj->resv);

Yeah definitely time to retire dmabuf->lock I think.

Cheers, Daniel

Quoted 181 lines
drivers/dma-buf/dma-buf.c +0 -0
  		seq_printf(s, "Total %d devices attached\n\n",
  				attach_count);
include/linux/dma-buf.h +100 -8
index 01ad5b942a6f..ccad2fc1f640 100644
@@ -92,14 +92,40 @@ struct dma_buf_ops {
  	 */
  	void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
 
+	/**
+	 * @pin:
+	 *
+	 * This is called by dma_buf_pin and lets the exporter know that the
+	 * DMA-buf can't be moved any more.
+	 *
+	 * This is called with the dmabuf->resv object locked.
+	 *
+	 * This callback is optional.
+	 *
+	 * Returns:
+	 *
+	 * 0 on success, negative error code on failure.
+	 */
+	int (*pin)(struct dma_buf_attachment *attach);
+
+	/**
+	 * @unpin:
+	 *
+	 * This is called by dma_buf_unpin and lets the exporter know that the
+	 * DMA-buf can be moved again.
+	 *
+	 * This is called with the dmabuf->resv object locked.
+	 *
+	 * This callback is optional.
+	 */
+	void (*unpin)(struct dma_buf_attachment *attach);
+
  	/**
  	 * @map_dma_buf:
  	 *
  	 * This is called by dma_buf_map_attachment() and is used to map a
  	 * shared &dma_buf into device address space, and it is mandatory. It
-	 * can only be called if @attach has been called successfully. This
-	 * essentially pins the DMA buffer into place, and it cannot be moved
-	 * any more
+	 * can only be called if @attach has been called successfully.
  	 *
  	 * This call may sleep, e.g. when the backing storage first needs to be
  	 * allocated, or moved to a location suitable for all currently attached
@@ -120,6 +146,9 @@ struct dma_buf_ops {
  	 * any other kind of sharing that the exporter might wish to make
  	 * available to buffer-users.
  	 *
+	 * This is always called with the dmabuf->resv object locked when
+	 * the pin/unpin callbacks are implemented.
+	 *
  	 * Returns:
  	 *
  	 * A &sg_table scatter list of or the backing storage of the DMA buffer,
@@ -137,9 +166,6 @@ struct dma_buf_ops {
  	 *
  	 * This is called by dma_buf_unmap_attachment() and should unmap and
  	 * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
-	 * It should also unpin the backing storage if this is the last mapping
-	 * of the DMA buffer, it the exporter supports backing storage
-	 * migration.
  	 */
  	void (*unmap_dma_buf)(struct dma_buf_attachment *,
  			      struct sg_table *,
@@ -330,6 +356,34 @@ struct dma_buf {
  	} cb_excl, cb_shared;
  };
 
+/**
+ * struct dma_buf_attach_ops - importer operations for an attachment
+ * @move_notify: [optional] notification that the DMA-buf is moving
+ *
+ * Attachment operations implemented by the importer.
+ */
+struct dma_buf_attach_ops {
+	/**
+	 * @move_notify
+	 *
+	 * If this callback is provided the framework can avoid pinning the
+	 * backing store while mappings exists.
+	 *
+	 * This callback is called with the lock of the reservation object
+	 * associated with the dma_buf held and the mapping function must be
+	 * called with this lock held as well. This makes sure that no mapping
+	 * is created concurrently with an ongoing move operation.
+	 *
+	 * Mappings stay valid and are not directly affected by this callback.
+	 * But the DMA-buf can now be in a different physical location, so all
+	 * mappings should be destroyed and re-created as soon as possible.
+	 *
+	 * New mappings can be created after this callback returns, and will
+	 * point to the new location of the DMA-buf.
+	 */
+	void (*move_notify)(struct dma_buf_attachment *attach);
+};
+
  /**
   * struct dma_buf_attachment - holds device-buffer attachment data
   * @dmabuf: buffer for this attachment.
@@ -338,6 +392,8 @@ struct dma_buf {
   * @sgt: cached mapping.
   * @dir: direction of cached mapping.
   * @priv: exporter specific attachment data.
+ * @importer_ops: importer operations for this attachment.
+ * @importer_priv: importer specific attachment data.
   *
   * This structure holds the attachment information between the dma_buf buffer
   * and its user device(s). The list contains one attachment struct per device
@@ -355,6 +411,9 @@ struct dma_buf_attachment {
  	struct sg_table *sgt;
  	enum dma_data_direction dir;
  	void *priv;
+
+	const struct dma_buf_attach_ops *importer_ops;
+	void *importer_priv;
  };
 
  /**
@@ -405,10 +464,42 @@ static inline void get_dma_buf(struct dma_buf *dmabuf)
  	get_file(dmabuf->file);
  }
 
+/**
+ * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings.
+ * @dmabuf: the DMA-buf to check
+ *
+ * Returns true if a DMA-buf exporter wants to create dynamic sg table mappings
+ * for each attachment. False if only a single static sg table should be used.
+ */
+static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
+{
+	return !!dmabuf->ops->pin;
+}
+
+/**
+ * dma_buf_attachment_is_dynamic - check if a DMA-buf attachment uses dynamic
+ * mappinsg
+ * @attach: the DMA-buf attachment to check
+ *
+ * Returns true if a DMA-buf importer wants to use dynamic sg table mappings and
+ * calls the map/unmap functions with the reservation object locked.
+ */
+static inline bool
+dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
+{
+	return attach->importer_ops && attach->importer_ops->move_notify;
+}
+
  struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-							struct device *dev);
+					  struct device *dev);
+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);
  void dma_buf_detach(struct dma_buf *dmabuf,
-				struct dma_buf_attachment *dmabuf_attach);
+		    struct dma_buf_attachment *attach);
+int dma_buf_pin(struct dma_buf_attachment *attach);
+void dma_buf_unpin(struct dma_buf_attachment *attach);
 
  struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
 
@@ -420,6 +511,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
  					enum dma_data_direction);
  void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
  				enum dma_data_direction);
+void dma_buf_move_notify(struct dma_buf *dma_buf);
  int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
  			     enum dma_data_direction dir);
  int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
Signature · 1 line
2.17.1
Quoted 4 lines
Quoted 4 lines

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#25 lore raw
Quoted 188 lines · Christian König

Am 25.06.19 um 16:35 schrieb Daniel Vetter:

Quoted 173 lines

Hi Christian,

Quoted 170 lines · Christian König

On the exporter side we add optional explicit pinning callbacks. If those callbacks are implemented the framework no longer caches sg tables and the map/unmap callbacks are always called with the lock of the reservation object held.

On the importer side we add an optional invalidate callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible.

This allows the exporter to provide the mappings without the need to pin the backing store.

v2: don't try to invalidate mappings when the callback is NULL,
lock the reservation obj while using the attachments, add helper to set the callback
v3: move flag for invalidation support into the DMA-buf,
use new attach_info structure to set the callback
v4: use importer_priv field instead of mangling exporter priv.
v5: drop invalidation_supported flag
v6: squash together with pin/unpin changes
v7: pin/unpin takes an attachment now
v8: nuke dma_buf_attachment_(map|unmap)_locked,
everything is now handled backward compatible
v9: always cache when export/importer don't agree on dynamic handling
v10: minimal style cleanup
v11: drop automatically re-entry avoidance
v12: rename callback to move_notify

---
  drivers/dma-buf/dma-buf.c | 179 ++++++++++++++++++++++++++++++++++++--
  include/linux/dma-buf.h   | 108 +++++++++++++++++++++--
  2 files changed, 273 insertions(+), 14 deletions(-)
drivers/dma-buf/dma-buf.c +74 -6
index 6c15deb5d4ad..216f76109f3f 100644
@@ -531,6 +531,9 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
             return ERR_PTR(-EINVAL);
     }
 
+    if (WARN_ON(exp_info->ops->cache_sgt_mapping && exp_info->ops->pin))
+            return ERR_PTR(-EINVAL);
+
     if (!try_module_get(exp_info->owner))
             return ERR_PTR(-ENOENT);
 
@@ -651,10 +654,12 @@ void dma_buf_put(struct dma_buf *dmabuf)
  EXPORT_SYMBOL_GPL(dma_buf_put);
 
  /**
- * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
+ * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list; optionally,
   * calls attach() of dma_buf_ops to allow device-specific attach functionality
- * @dmabuf: [in]    buffer to attach device to.
- * @dev:    [in]    device to be attached.
+ * @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
   *
   * Returns struct dma_buf_attachment pointer for this attachment. Attachments
   * must be cleaned up by calling dma_buf_detach().
@@ -668,8 +673,10 @@ EXPORT_SYMBOL_GPL(dma_buf_put);
   * accessible to @dev, and cannot be moved to a more suitable place. This is
   * indicated with the error code -EBUSY.
   */
-struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-                                      struct device *dev)
+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 *attach;
     int ret;
@@ -683,6 +690,8 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
 
     attach->dev = dev;
     attach->dmabuf = dmabuf;
+    attach->importer_ops = importer_ops;
+    attach->importer_priv = importer_priv;
 
     mutex_lock(&dmabuf->lock);
 
@@ -691,16 +700,72 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
             if (ret)
                     goto err_attach;
     }
+    reservation_object_lock(dmabuf->resv, NULL);
     list_add(&attach->node, &dmabuf->attachments);
+    reservation_object_unlock(dmabuf->resv);
 
     mutex_unlock(&dmabuf->lock);
 
+    /* When either the importer or the exporter can't handle dynamic
+     * mappings we cache the mapping here to avoid issues with the
+     * reservation object lock.
+     */
+    if (dma_buf_attachment_is_dynamic(attach) !=
+        dma_buf_is_dynamic(dmabuf)) {
+            struct sg_table *sgt;
+
+            if (dma_buf_is_dynamic(attach->dmabuf)) {
+                    reservation_object_lock(attach->dmabuf->resv, NULL);
+                    ret = dma_buf_pin(attach);
+                    if (ret)
+                            goto err_unlock;
+            }
+
+            sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL);
+            if (!sgt)
+                    sgt = ERR_PTR(-ENOMEM);
+            if (IS_ERR(sgt)) {
+                    ret = PTR_ERR(sgt);
+                    goto err_unpin;
+            }
+            if (dma_buf_is_dynamic(attach->dmabuf))
+                    reservation_object_unlock(attach->dmabuf->resv);
+            attach->sgt = sgt;
+            attach->dir = DMA_BIDIRECTIONAL;
+    }
+
     return attach;
 
  err_attach:
     kfree(attach);
     mutex_unlock(&dmabuf->lock);
     return ERR_PTR(ret);
+
+err_unpin:
+    if (dma_buf_is_dynamic(attach->dmabuf))
+            dma_buf_unpin(attach);
+
+err_unlock:
+    if (dma_buf_is_dynamic(attach->dmabuf))
+            reservation_object_unlock(attach->dmabuf->resv);
+
+    dma_buf_detach(dmabuf, attach);
+    return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(dma_buf_dynamic_attach);
+
+/**
+ * dma_buf_attach - Wrapper for dma_buf_dynamic_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
+ * mapping.
+ */
+struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
+                                      struct device *dev)
+{
+    return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
  }
  EXPORT_SYMBOL_GPL(dma_buf_attach);
 
@@ -717,11 +782,22 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
     if (WARN_ON(!dmabuf || !attach))
             return;
 
-    if (attach->sgt)
+    if (attach->sgt) {
+            if (dma_buf_is_dynamic(attach->dmabuf))
+                    reservation_object_lock(attach->dmabuf->resv, NULL);
+
             dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
 
Quoted 10 lines

drivers/dma-buf/dma-buf.c void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) · line 797

Quoted 7 lines
drivers/dma-buf/dma-buf.c +6 -0
+            if (dma_buf_is_dynamic(attach->dmabuf)) {
+                    dma_buf_unpin(attach);
+                    reservation_object_unlock(attach->dmabuf->resv);
+            }
+    }
+
     mutex_lock(&dmabuf->lock);

Time to ditch dmabuf->lock in favour of the reservation obj? We have a fallback resv_obj in struct dma_buf already, so this is never null, and I think would clean up the code a bit.

Yeah, thought about that as well. But then decided against it for now.

Quoted 8 lines

Key point is that exporters currently doesn't care about dmabuf->lock, but they do care about the reservation lock.

So we will probably have a bunch of cases where we have to fix up exporters because they will try to grab the reservation lock as well.

On the other hand we maybe not need a lock at all here if we just can live with multiple attach/detach callbacks running in parallel.

Well looking through the code I thought that all the places you grab dmabuf->resv we also grab dmabuf->lock, so seemed fully redundant. But I didn't check completely.

Quoted 90 lines
Quoted 80 lines
Quoted 67 lines
drivers/dma-buf/dma-buf.c +42 -0
+    reservation_object_lock(dmabuf->resv, NULL);
     list_del(&attach->node);
+    reservation_object_unlock(dmabuf->resv);
     if (dmabuf->ops->detach)
             dmabuf->ops->detach(dmabuf, attach);
 
@@ -730,6 +806,44 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  }
  EXPORT_SYMBOL_GPL(dma_buf_detach);
 
+/**
+ * dma_buf_pin - Lock down the DMA-buf
+ *
+ * @attach: [in]    attachment which should be pinned
+ *
+ * Returns:
+ * 0 on success, negative error code on failure.
+ */
+int dma_buf_pin(struct dma_buf_attachment *attach)
+{
+    struct dma_buf *dmabuf = attach->dmabuf;
+    int ret = 0;
+
+    reservation_object_assert_held(dmabuf->resv);
+
+    if (dmabuf->ops->pin)
+            ret = dmabuf->ops->pin(attach);
+
+    return ret;
+}
+EXPORT_SYMBOL_GPL(dma_buf_pin);
+
+/**
+ * dma_buf_unpin - Remove lock from DMA-buf
+ *
+ * @attach: [in]    attachment which should be unpinned
+ */
+void dma_buf_unpin(struct dma_buf_attachment *attach)
+{
+    struct dma_buf *dmabuf = attach->dmabuf;
+
+    reservation_object_assert_held(dmabuf->resv);
+
+    if (dmabuf->ops->unpin)
+            dmabuf->ops->unpin(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_unpin);
+
  /**
   * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
   * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
@@ -749,6 +863,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
                                     enum dma_data_direction direction)
  {
     struct sg_table *sg_table;
+    int r;
 
     might_sleep();
 
@@ -767,10 +882,29 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
             return attach->sgt;
     }
 
+    if (dma_buf_attachment_is_dynamic(attach)) {

drivers/dma-buf/dma-buf.c struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, · line 895

Quoted 10 lines
drivers/dma-buf/dma-buf.c +10 -0
+            reservation_object_assert_held(attach->dmabuf->resv);
+
+    } else if (dma_buf_is_dynamic(attach->dmabuf)) {
+            reservation_object_lock(attach->dmabuf->resv, NULL);
+            r = dma_buf_pin(attach);
+            if (r) {
+                    reservation_object_unlock(attach->dmabuf->resv);
+                    return ERR_PTR(r);
+            }
+    }

With this design (because we can't cache at attach time unconditionally) we inflict the reservation lock on all importers. So needs:

Quoted 6 lines
      } else {
              /* neither importer nor exporter are dynamice */
              might_lock(dmabuf->resv->ww_mutex);
      }

Same for dma_buf_unmap_attachment.

Good idea.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

I also expect that this will blow up in intel-gfx-ci :-/ I did look at previous intel-gfx-ci runs of your series, and the last one that did pass was the one that still had the caching in _attach():

Well as far as I can see the current one passes as well. Actually just recently found a typo because of this.

Hm indeed the amd/i915 prime tests are still in BAT and that passed. I'm surprised. Maybe Chris Wilson moved along quite a lot with our locking rework and i915 isn't the bad one out anymore.

Quoted 6 lines
Quoted 3 lines

Geez I was sooooooooo locking forward to slapping an r-b on this and volunteering myself to polish the kerneldoc (which isn't great, but also pre-existing condition plus on my todo list anyway) :-(

Going to add the might_lock and send again, let's see what intel-gfx-ci has say to this.

Yeah that should at least increase the chances we're seeing something. Ofc intel-gfx-ci doesn't test everything :-)
-Daniel

Quoted 261 lines

Christian.

Quoted 257 lines
Quoted 59 lines
drivers/dma-buf/dma-buf.c +39 -0
+
     sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
     if (!sg_table)
             sg_table = ERR_PTR(-ENOMEM);
 
+    if (!dma_buf_attachment_is_dynamic(attach) &&
+        dma_buf_is_dynamic(attach->dmabuf)) {
+            if (IS_ERR(sg_table))
+                    dma_buf_unpin(attach);
+            reservation_object_unlock(attach->dmabuf->resv);
+    }
+
     if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
             attach->sgt = sg_table;
             attach->dir = direction;
@@ -802,10 +936,41 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
     if (attach->sgt == sg_table)
             return;
 
+    if (dma_buf_attachment_is_dynamic(attach))
+            reservation_object_assert_held(attach->dmabuf->resv);
+    else if (dma_buf_is_dynamic(attach->dmabuf))
+            reservation_object_lock(attach->dmabuf->resv, NULL);
+
     attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+
+    if (dma_buf_is_dynamic(attach->dmabuf) &&
+        !dma_buf_attachment_is_dynamic(attach)) {
+            dma_buf_unpin(attach);
+            reservation_object_unlock(attach->dmabuf->resv);
+    }
  }
  EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
 
+/**
+ * dma_buf_move_notify - notify attachments that DMA-buf is moving
+ *
+ * @dmabuf: [in]    buffer which is moving
+ *
+ * Informs all attachmenst that they need to destroy and recreated all their
+ * mappings.
+ */
+void dma_buf_move_notify(struct dma_buf *dmabuf)
+{
+    struct dma_buf_attachment *attach;
+
+    reservation_object_assert_held(dmabuf->resv);
+
+    list_for_each_entry(attach, &dmabuf->attachments, node)
+            if (attach->importer_ops && attach->importer_ops->move_notify)
+                    attach->importer_ops->move_notify(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_move_notify);
+
  /**
   * DOC: cpu access
   *

drivers/dma-buf/dma-buf.c static int dma_buf_debug_show(struct seq_file *s, void *unused) · line 1398

Quoted 9 lines
drivers/dma-buf/dma-buf.c +2 -0
@@ -1225,10 +1390,12 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
             seq_puts(s, "\tAttached Devices:\n");
             attach_count = 0;
 
+            reservation_object_lock(buf_obj->resv, NULL);
             list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
                     seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
                     attach_count++;
             }
+            reservation_object_unlock(buf_obj->resv);

Yeah definitely time to retire dmabuf->lock I think.

Cheers, Daniel

Quoted 185 lines
drivers/dma-buf/dma-buf.c +0 -0
             seq_printf(s, "Total %d devices attached\n\n",
                             attach_count);
include/linux/dma-buf.h +100 -9
index 01ad5b942a6f..ccad2fc1f640 100644
@@ -92,14 +92,40 @@ struct dma_buf_ops {
      */
     void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
 
+    /**
+     * @pin:
+     *
+     * This is called by dma_buf_pin and lets the exporter know that the
+     * DMA-buf can't be moved any more.
+     *
+     * This is called with the dmabuf->resv object locked.
+     *
+     * This callback is optional.
+     *
+     * Returns:
+     *
+     * 0 on success, negative error code on failure.
+     */
+    int (*pin)(struct dma_buf_attachment *attach);
+
+    /**
+     * @unpin:
+     *
+     * This is called by dma_buf_unpin and lets the exporter know that the
+     * DMA-buf can be moved again.
+     *
+     * This is called with the dmabuf->resv object locked.
+     *
+     * This callback is optional.
+     */
+    void (*unpin)(struct dma_buf_attachment *attach);
+
     /**
      * @map_dma_buf:
      *
      * This is called by dma_buf_map_attachment() and is used to map a
      * shared &dma_buf into device address space, and it is mandatory. It
-     * can only be called if @attach has been called successfully. This
-     * essentially pins the DMA buffer into place, and it cannot be moved
-     * any more
+     * can only be called if @attach has been called successfully.
      *
      * This call may sleep, e.g. when the backing storage first needs to be
      * allocated, or moved to a location suitable for all currently attached
@@ -120,6 +146,9 @@ struct dma_buf_ops {
      * any other kind of sharing that the exporter might wish to make
      * available to buffer-users.
      *
+     * This is always called with the dmabuf->resv object locked when
+     * the pin/unpin callbacks are implemented.
+     *
      * Returns:
      *
      * A &sg_table scatter list of or the backing storage of the DMA buffer,
@@ -137,9 +166,6 @@ struct dma_buf_ops {
      *
      * This is called by dma_buf_unmap_attachment() and should unmap and
      * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
-     * It should also unpin the backing storage if this is the last mapping
-     * of the DMA buffer, it the exporter supports backing storage
-     * migration.
      */
     void (*unmap_dma_buf)(struct dma_buf_attachment *,
                           struct sg_table *,
@@ -330,6 +356,34 @@ struct dma_buf {
     } cb_excl, cb_shared;
  };
 
+/**
+ * struct dma_buf_attach_ops - importer operations for an attachment
+ * @move_notify: [optional] notification that the DMA-buf is moving
+ *
+ * Attachment operations implemented by the importer.
+ */
+struct dma_buf_attach_ops {
+    /**
+     * @move_notify
+     *
+     * If this callback is provided the framework can avoid pinning the
+     * backing store while mappings exists.
+     *
+     * This callback is called with the lock of the reservation object
+     * associated with the dma_buf held and the mapping function must be
+     * called with this lock held as well. This makes sure that no mapping
+     * is created concurrently with an ongoing move operation.
+     *
+     * Mappings stay valid and are not directly affected by this callback.
+     * But the DMA-buf can now be in a different physical location, so all
+     * mappings should be destroyed and re-created as soon as possible.
+     *
+     * New mappings can be created after this callback returns, and will
+     * point to the new location of the DMA-buf.
+     */
+    void (*move_notify)(struct dma_buf_attachment *attach);
+};
+
  /**
   * struct dma_buf_attachment - holds device-buffer attachment data
   * @dmabuf: buffer for this attachment.
@@ -338,6 +392,8 @@ struct dma_buf {
   * @sgt: cached mapping.
   * @dir: direction of cached mapping.
   * @priv: exporter specific attachment data.
+ * @importer_ops: importer operations for this attachment.
+ * @importer_priv: importer specific attachment data.
   *
   * This structure holds the attachment information between the dma_buf buffer
   * and its user device(s). The list contains one attachment struct per device
@@ -355,6 +411,9 @@ struct dma_buf_attachment {
     struct sg_table *sgt;
     enum dma_data_direction dir;
     void *priv;
+
+    const struct dma_buf_attach_ops *importer_ops;
+    void *importer_priv;
  };
 
  /**
@@ -405,10 +464,42 @@ static inline void get_dma_buf(struct dma_buf *dmabuf)
     get_file(dmabuf->file);
  }
 
+/**
+ * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings.
+ * @dmabuf: the DMA-buf to check
+ *
+ * Returns true if a DMA-buf exporter wants to create dynamic sg table mappings
+ * for each attachment. False if only a single static sg table should be used.
+ */
+static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
+{
+    return !!dmabuf->ops->pin;
+}
+
+/**
+ * dma_buf_attachment_is_dynamic - check if a DMA-buf attachment uses dynamic
+ * mappinsg
+ * @attach: the DMA-buf attachment to check
+ *
+ * Returns true if a DMA-buf importer wants to use dynamic sg table mappings and
+ * calls the map/unmap functions with the reservation object locked.
+ */
+static inline bool
+dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
+{
+    return attach->importer_ops && attach->importer_ops->move_notify;
+}
+
  struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-                                                    struct device *dev);
+                                      struct device *dev);
+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);
  void dma_buf_detach(struct dma_buf *dmabuf,
-                            struct dma_buf_attachment *dmabuf_attach);
+                struct dma_buf_attachment *attach);
+int dma_buf_pin(struct dma_buf_attachment *attach);
+void dma_buf_unpin(struct dma_buf_attachment *attach);
 
  struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
 
@@ -420,6 +511,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
                                     enum dma_data_direction);
  void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
                             enum dma_data_direction);
+void dma_buf_move_notify(struct dma_buf *dma_buf);
  int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
                          enum dma_data_direction dir);
  int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
--

2.17.1

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#26 lore raw

Am 25.06.19 um 17:07 schrieb Daniel Vetter:

Quoted 191 lines
Quoted 189 lines · Christian König

Am 25.06.19 um 16:35 schrieb Daniel Vetter:

Quoted 173 lines

Hi Christian,

Quoted 170 lines · Christian König

On the exporter side we add optional explicit pinning callbacks. If those callbacks are implemented the framework no longer caches sg tables and the map/unmap callbacks are always called with the lock of the reservation object held.

On the importer side we add an optional invalidate callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible.

This allows the exporter to provide the mappings without the need to pin the backing store.

v2: don't try to invalidate mappings when the callback is NULL,
lock the reservation obj while using the attachments, add helper to set the callback
v3: move flag for invalidation support into the DMA-buf,
use new attach_info structure to set the callback
v4: use importer_priv field instead of mangling exporter priv.
v5: drop invalidation_supported flag
v6: squash together with pin/unpin changes
v7: pin/unpin takes an attachment now
v8: nuke dma_buf_attachment_(map|unmap)_locked,
everything is now handled backward compatible
v9: always cache when export/importer don't agree on dynamic handling
v10: minimal style cleanup
v11: drop automatically re-entry avoidance
v12: rename callback to move_notify

---
   drivers/dma-buf/dma-buf.c | 179 ++++++++++++++++++++++++++++++++++++--
   include/linux/dma-buf.h   | 108 +++++++++++++++++++++--
   2 files changed, 273 insertions(+), 14 deletions(-)
drivers/dma-buf/dma-buf.c +74 -6
index 6c15deb5d4ad..216f76109f3f 100644
@@ -531,6 +531,9 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
              return ERR_PTR(-EINVAL);
      }
 
+    if (WARN_ON(exp_info->ops->cache_sgt_mapping && exp_info->ops->pin))
+            return ERR_PTR(-EINVAL);
+
      if (!try_module_get(exp_info->owner))
              return ERR_PTR(-ENOENT);
 
@@ -651,10 +654,12 @@ void dma_buf_put(struct dma_buf *dmabuf)
   EXPORT_SYMBOL_GPL(dma_buf_put);
 
   /**
- * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
+ * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list; optionally,
    * calls attach() of dma_buf_ops to allow device-specific attach functionality
- * @dmabuf: [in]    buffer to attach device to.
- * @dev:    [in]    device to be attached.
+ * @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
    *
    * Returns struct dma_buf_attachment pointer for this attachment. Attachments
    * must be cleaned up by calling dma_buf_detach().
@@ -668,8 +673,10 @@ EXPORT_SYMBOL_GPL(dma_buf_put);
    * accessible to @dev, and cannot be moved to a more suitable place. This is
    * indicated with the error code -EBUSY.
    */
-struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-                                      struct device *dev)
+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 *attach;
      int ret;
@@ -683,6 +690,8 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
 
      attach->dev = dev;
      attach->dmabuf = dmabuf;
+    attach->importer_ops = importer_ops;
+    attach->importer_priv = importer_priv;
 
      mutex_lock(&dmabuf->lock);
 
@@ -691,16 +700,72 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
              if (ret)
                      goto err_attach;
      }
+    reservation_object_lock(dmabuf->resv, NULL);
      list_add(&attach->node, &dmabuf->attachments);
+    reservation_object_unlock(dmabuf->resv);
 
      mutex_unlock(&dmabuf->lock);
 
+    /* When either the importer or the exporter can't handle dynamic
+     * mappings we cache the mapping here to avoid issues with the
+     * reservation object lock.
+     */
+    if (dma_buf_attachment_is_dynamic(attach) !=
+        dma_buf_is_dynamic(dmabuf)) {
+            struct sg_table *sgt;
+
+            if (dma_buf_is_dynamic(attach->dmabuf)) {
+                    reservation_object_lock(attach->dmabuf->resv, NULL);
+                    ret = dma_buf_pin(attach);
+                    if (ret)
+                            goto err_unlock;
+            }
+
+            sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL);
+            if (!sgt)
+                    sgt = ERR_PTR(-ENOMEM);
+            if (IS_ERR(sgt)) {
+                    ret = PTR_ERR(sgt);
+                    goto err_unpin;
+            }
+            if (dma_buf_is_dynamic(attach->dmabuf))
+                    reservation_object_unlock(attach->dmabuf->resv);
+            attach->sgt = sgt;
+            attach->dir = DMA_BIDIRECTIONAL;
+    }
+
      return attach;
 
   err_attach:
      kfree(attach);
      mutex_unlock(&dmabuf->lock);
      return ERR_PTR(ret);
+
+err_unpin:
+    if (dma_buf_is_dynamic(attach->dmabuf))
+            dma_buf_unpin(attach);
+
+err_unlock:
+    if (dma_buf_is_dynamic(attach->dmabuf))
+            reservation_object_unlock(attach->dmabuf->resv);
+
+    dma_buf_detach(dmabuf, attach);
+    return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(dma_buf_dynamic_attach);
+
+/**
+ * dma_buf_attach - Wrapper for dma_buf_dynamic_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
+ * mapping.
+ */
+struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
+                                      struct device *dev)
+{
+    return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
   }
   EXPORT_SYMBOL_GPL(dma_buf_attach);
 
@@ -717,11 +782,22 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
      if (WARN_ON(!dmabuf || !attach))
              return;
 
-    if (attach->sgt)
+    if (attach->sgt) {
+            if (dma_buf_is_dynamic(attach->dmabuf))
+                    reservation_object_lock(attach->dmabuf->resv, NULL);
+
              dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
 
Quoted 10 lines

drivers/dma-buf/dma-buf.c void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) · line 797

Quoted 7 lines
drivers/dma-buf/dma-buf.c +6 -0
+            if (dma_buf_is_dynamic(attach->dmabuf)) {
+                    dma_buf_unpin(attach);
+                    reservation_object_unlock(attach->dmabuf->resv);
+            }
+    }
+
      mutex_lock(&dmabuf->lock);

Time to ditch dmabuf->lock in favour of the reservation obj? We have a fallback resv_obj in struct dma_buf already, so this is never null, and I think would clean up the code a bit.

Yeah, thought about that as well. But then decided against it for now.

Key point is that exporters currently doesn't care about dmabuf->lock, but they do care about the reservation lock.

Quoted 8 lines
Quoted 5 lines

So we will probably have a bunch of cases where we have to fix up exporters because they will try to grab the reservation lock as well.

On the other hand we maybe not need a lock at all here if we just can live with multiple attach/detach callbacks running in parallel.

Well looking through the code I thought that all the places you grab dmabuf->resv we also grab dmabuf->lock, so seemed fully redundant. But I didn't check completely.

The difference is that I didn't call attach/detach with the lock held yet.

And we could actually drop locking the reservation object while manipulating the list of attachments since we no longer use that workaround.

Going to fix that up tomorrow as well,
Christian.

Quoted 358 lines
Quoted 84 lines
Quoted 84 lines
Quoted 66 lines
drivers/dma-buf/dma-buf.c +42 -0
+    reservation_object_lock(dmabuf->resv, NULL);
      list_del(&attach->node);
+    reservation_object_unlock(dmabuf->resv);
      if (dmabuf->ops->detach)
              dmabuf->ops->detach(dmabuf, attach);
 
@@ -730,6 +806,44 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
   }
   EXPORT_SYMBOL_GPL(dma_buf_detach);
 
+/**
+ * dma_buf_pin - Lock down the DMA-buf
+ *
+ * @attach: [in]    attachment which should be pinned
+ *
+ * Returns:
+ * 0 on success, negative error code on failure.
+ */
+int dma_buf_pin(struct dma_buf_attachment *attach)
+{
+    struct dma_buf *dmabuf = attach->dmabuf;
+    int ret = 0;
+
+    reservation_object_assert_held(dmabuf->resv);
+
+    if (dmabuf->ops->pin)
+            ret = dmabuf->ops->pin(attach);
+
+    return ret;
+}
+EXPORT_SYMBOL_GPL(dma_buf_pin);
+
+/**
+ * dma_buf_unpin - Remove lock from DMA-buf
+ *
+ * @attach: [in]    attachment which should be unpinned
+ */
+void dma_buf_unpin(struct dma_buf_attachment *attach)
+{
+    struct dma_buf *dmabuf = attach->dmabuf;
+
+    reservation_object_assert_held(dmabuf->resv);
+
+    if (dmabuf->ops->unpin)
+            dmabuf->ops->unpin(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_unpin);
+
   /**
    * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
    * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
@@ -749,6 +863,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
                                      enum dma_data_direction direction)
   {
      struct sg_table *sg_table;
+    int r;
 
      might_sleep();
 
@@ -767,10 +882,29 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
              return attach->sgt;
      }
 
+    if (dma_buf_attachment_is_dynamic(attach)) {

drivers/dma-buf/dma-buf.c struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, · line 895

Quoted 10 lines
drivers/dma-buf/dma-buf.c +10 -0
+            reservation_object_assert_held(attach->dmabuf->resv);
+
+    } else if (dma_buf_is_dynamic(attach->dmabuf)) {
+            reservation_object_lock(attach->dmabuf->resv, NULL);
+            r = dma_buf_pin(attach);
+            if (r) {
+                    reservation_object_unlock(attach->dmabuf->resv);
+                    return ERR_PTR(r);
+            }
+    }

With this design (because we can't cache at attach time unconditionally) we inflict the reservation lock on all importers. So needs:

       } else {
               /* neither importer nor exporter are dynamice */
               might_lock(dmabuf->resv->ww_mutex);
       }
Quoted 8 lines
Quoted 1 line

Same for dma_buf_unmap_attachment.

Good idea.

Quoted 3 lines

I also expect that this will blow up in intel-gfx-ci :-/ I did look at previous intel-gfx-ci runs of your series, and the last one that did pass was the one that still had the caching in _attach():

Well as far as I can see the current one passes as well. Actually just recently found a typo because of this.

Hm indeed the amd/i915 prime tests are still in BAT and that passed. I'm surprised. Maybe Chris Wilson moved along quite a lot with our locking rework and i915 isn't the bad one out anymore.

Quoted 5 lines
Quoted 3 lines

Geez I was sooooooooo locking forward to slapping an r-b on this and volunteering myself to polish the kerneldoc (which isn't great, but also pre-existing condition plus on my todo list anyway) :-(

Going to add the might_lock and send again, let's see what intel-gfx-ci has say to this.

Yeah that should at least increase the chances we're seeing something. Ofc intel-gfx-ci doesn't test everything :-)
-Daniel

Quoted 253 lines

Christian.

Quoted 251 lines
Quoted 58 lines
drivers/dma-buf/dma-buf.c +39 -0
+
      sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
      if (!sg_table)
              sg_table = ERR_PTR(-ENOMEM);
 
+    if (!dma_buf_attachment_is_dynamic(attach) &&
+        dma_buf_is_dynamic(attach->dmabuf)) {
+            if (IS_ERR(sg_table))
+                    dma_buf_unpin(attach);
+            reservation_object_unlock(attach->dmabuf->resv);
+    }
+
      if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
              attach->sgt = sg_table;
              attach->dir = direction;
@@ -802,10 +936,41 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
      if (attach->sgt == sg_table)
              return;
 
+    if (dma_buf_attachment_is_dynamic(attach))
+            reservation_object_assert_held(attach->dmabuf->resv);
+    else if (dma_buf_is_dynamic(attach->dmabuf))
+            reservation_object_lock(attach->dmabuf->resv, NULL);
+
      attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+
+    if (dma_buf_is_dynamic(attach->dmabuf) &&
+        !dma_buf_attachment_is_dynamic(attach)) {
+            dma_buf_unpin(attach);
+            reservation_object_unlock(attach->dmabuf->resv);
+    }
   }
   EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
 
+/**
+ * dma_buf_move_notify - notify attachments that DMA-buf is moving
+ *
+ * @dmabuf: [in]    buffer which is moving
+ *
+ * Informs all attachmenst that they need to destroy and recreated all their
+ * mappings.
+ */
+void dma_buf_move_notify(struct dma_buf *dmabuf)
+{
+    struct dma_buf_attachment *attach;
+
+    reservation_object_assert_held(dmabuf->resv);
+
+    list_for_each_entry(attach, &dmabuf->attachments, node)
+            if (attach->importer_ops && attach->importer_ops->move_notify)
+                    attach->importer_ops->move_notify(attach);
+}
+EXPORT_SYMBOL_GPL(dma_buf_move_notify);
+
   /**
    * DOC: cpu access
    *

drivers/dma-buf/dma-buf.c static int dma_buf_debug_show(struct seq_file *s, void *unused) · line 1398

Quoted 9 lines
drivers/dma-buf/dma-buf.c +2 -0
@@ -1225,10 +1390,12 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
              seq_puts(s, "\tAttached Devices:\n");
              attach_count = 0;
 
+            reservation_object_lock(buf_obj->resv, NULL);
              list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
                      seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
                      attach_count++;
              }
+            reservation_object_unlock(buf_obj->resv);

Yeah definitely time to retire dmabuf->lock I think.

Cheers, Daniel

Quoted 180 lines
drivers/dma-buf/dma-buf.c +0 -0
              seq_printf(s, "Total %d devices attached\n\n",
                              attach_count);
include/linux/dma-buf.h +100 -9
index 01ad5b942a6f..ccad2fc1f640 100644
@@ -92,14 +92,40 @@ struct dma_buf_ops {
       */
      void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
 
+    /**
+     * @pin:
+     *
+     * This is called by dma_buf_pin and lets the exporter know that the
+     * DMA-buf can't be moved any more.
+     *
+     * This is called with the dmabuf->resv object locked.
+     *
+     * This callback is optional.
+     *
+     * Returns:
+     *
+     * 0 on success, negative error code on failure.
+     */
+    int (*pin)(struct dma_buf_attachment *attach);
+
+    /**
+     * @unpin:
+     *
+     * This is called by dma_buf_unpin and lets the exporter know that the
+     * DMA-buf can be moved again.
+     *
+     * This is called with the dmabuf->resv object locked.
+     *
+     * This callback is optional.
+     */
+    void (*unpin)(struct dma_buf_attachment *attach);
+
      /**
       * @map_dma_buf:
       *
       * This is called by dma_buf_map_attachment() and is used to map a
       * shared &dma_buf into device address space, and it is mandatory. It
-     * can only be called if @attach has been called successfully. This
-     * essentially pins the DMA buffer into place, and it cannot be moved
-     * any more
+     * can only be called if @attach has been called successfully.
       *
       * This call may sleep, e.g. when the backing storage first needs to be
       * allocated, or moved to a location suitable for all currently attached
@@ -120,6 +146,9 @@ struct dma_buf_ops {
       * any other kind of sharing that the exporter might wish to make
       * available to buffer-users.
       *
+     * This is always called with the dmabuf->resv object locked when
+     * the pin/unpin callbacks are implemented.
+     *
       * Returns:
       *
       * A &sg_table scatter list of or the backing storage of the DMA buffer,
@@ -137,9 +166,6 @@ struct dma_buf_ops {
       *
       * This is called by dma_buf_unmap_attachment() and should unmap and
       * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
-     * It should also unpin the backing storage if this is the last mapping
-     * of the DMA buffer, it the exporter supports backing storage
-     * migration.
       */
      void (*unmap_dma_buf)(struct dma_buf_attachment *,
                            struct sg_table *,
@@ -330,6 +356,34 @@ struct dma_buf {
      } cb_excl, cb_shared;
   };
 
+/**
+ * struct dma_buf_attach_ops - importer operations for an attachment
+ * @move_notify: [optional] notification that the DMA-buf is moving
+ *
+ * Attachment operations implemented by the importer.
+ */
+struct dma_buf_attach_ops {
+    /**
+     * @move_notify
+     *
+     * If this callback is provided the framework can avoid pinning the
+     * backing store while mappings exists.
+     *
+     * This callback is called with the lock of the reservation object
+     * associated with the dma_buf held and the mapping function must be
+     * called with this lock held as well. This makes sure that no mapping
+     * is created concurrently with an ongoing move operation.
+     *
+     * Mappings stay valid and are not directly affected by this callback.
+     * But the DMA-buf can now be in a different physical location, so all
+     * mappings should be destroyed and re-created as soon as possible.
+     *
+     * New mappings can be created after this callback returns, and will
+     * point to the new location of the DMA-buf.
+     */
+    void (*move_notify)(struct dma_buf_attachment *attach);
+};
+
   /**
    * struct dma_buf_attachment - holds device-buffer attachment data
    * @dmabuf: buffer for this attachment.
@@ -338,6 +392,8 @@ struct dma_buf {
    * @sgt: cached mapping.
    * @dir: direction of cached mapping.
    * @priv: exporter specific attachment data.
+ * @importer_ops: importer operations for this attachment.
+ * @importer_priv: importer specific attachment data.
    *
    * This structure holds the attachment information between the dma_buf buffer
    * and its user device(s). The list contains one attachment struct per device
@@ -355,6 +411,9 @@ struct dma_buf_attachment {
      struct sg_table *sgt;
      enum dma_data_direction dir;
      void *priv;
+
+    const struct dma_buf_attach_ops *importer_ops;
+    void *importer_priv;
   };
 
   /**
@@ -405,10 +464,42 @@ static inline void get_dma_buf(struct dma_buf *dmabuf)
      get_file(dmabuf->file);
   }
 
+/**
+ * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings.
+ * @dmabuf: the DMA-buf to check
+ *
+ * Returns true if a DMA-buf exporter wants to create dynamic sg table mappings
+ * for each attachment. False if only a single static sg table should be used.
+ */
+static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
+{
+    return !!dmabuf->ops->pin;
+}
+
+/**
+ * dma_buf_attachment_is_dynamic - check if a DMA-buf attachment uses dynamic
+ * mappinsg
+ * @attach: the DMA-buf attachment to check
+ *
+ * Returns true if a DMA-buf importer wants to use dynamic sg table mappings and
+ * calls the map/unmap functions with the reservation object locked.
+ */
+static inline bool
+dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
+{
+    return attach->importer_ops && attach->importer_ops->move_notify;
+}
+
   struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
-                                                    struct device *dev);
+                                      struct device *dev);
+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);
   void dma_buf_detach(struct dma_buf *dmabuf,
-                            struct dma_buf_attachment *dmabuf_attach);
+                struct dma_buf_attachment *attach);
+int dma_buf_pin(struct dma_buf_attachment *attach);
+void dma_buf_unpin(struct dma_buf_attachment *attach);
 
   struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
 
@@ -420,6 +511,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
                                      enum dma_data_direction);
   void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
                              enum dma_data_direction);
+void dma_buf_move_notify(struct dma_buf *dma_buf);
   int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
                           enum dma_data_direction dir);
   int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
--

2.17.1

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

A message that is not in the archive (sent off-list or not yet received): 6fdde041-89f8-ff12-d237-3e280f0af21c-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#28 lore raw
Quoted 34 lines · Christian König

Am 25.06.19 um 17:07 schrieb Daniel Vetter:

Quoted 24 lines
Quoted 22 lines · Christian König
Quoted 7 lines
Quoted 7 lines
(quoted diff) +4 -1
-    if (attach->sgt)
+    if (attach->sgt) {
+            if (dma_buf_is_dynamic(attach->dmabuf))
+                    reservation_object_lock(attach->dmabuf->resv, NULL);
+
              dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +6 -0
+            if (dma_buf_is_dynamic(attach->dmabuf)) {
+                    dma_buf_unpin(attach);
+                    reservation_object_unlock(attach->dmabuf->resv);
+            }
+    }
+
      mutex_lock(&dmabuf->lock);

Time to ditch dmabuf->lock in favour of the reservation obj? We have a fallback resv_obj in struct dma_buf already, so this is never null, and I think would clean up the code a bit.

Yeah, thought about that as well. But then decided against it for now.

Key point is that exporters currently doesn't care about dmabuf->lock, but they do care about the reservation lock.

Quoted 8 lines
Quoted 5 lines

So we will probably have a bunch of cases where we have to fix up exporters because they will try to grab the reservation lock as well.

On the other hand we maybe not need a lock at all here if we just can live with multiple attach/detach callbacks running in parallel.

Well looking through the code I thought that all the places you grab dmabuf->resv we also grab dmabuf->lock, so seemed fully redundant. But I didn't check completely.

Quoted 6 lines

The difference is that I didn't call attach/detach with the lock held yet.

And we could actually drop locking the reservation object while manipulating the list of attachments since we no longer use that workaround.

Going to fix that up tomorrow as well,

You rely on the reservation lock for walking the list when calling move_notify. So that lock is definitely needed there. If you'd put it in there then lockdep would splat about locking inversions.

In general dmabuf->lock not being a ww_mutex kinda gets in the way and just needless complicates the hierarchy. Atm its even outside of the reservation lock, so anywhere where we expect callers to hold the reservation lock already we can't take it.
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#29 lore raw

✓ Fi.CI.IGT: success for series starting with [1/6] dma-buf: add dynamic DMA-buf handling v12

In reply to Christian König
== Series Details ==

== Series Details ==

Series: series starting with [1/6] dma-buf: add dynamic DMA-buf handling v12
URL : https://patchwork.freedesktop.org/series/62703/
State : success

== Summary ==

CI Bug Log - changes from CI_DRM_6342_full -> Patchwork_13414_full
====================================================

Summary
-------

**SUCCESS**

No regressions found.

Known issues
------------

Here are the changes found in Patchwork_13414_full that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@gem_eio@wait-wedge-1us:
- shard-skl: [PASS][1] -> [DMESG-WARN][2] ([fdo#110913 ])
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl8/igt@[email protected]
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl5/igt@[email protected]

* igt@gem_exec_balancer@smoke:
- shard-iclb: [PASS][3] -> [SKIP][4] ([fdo#110854])
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb1/igt@[email protected]
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb5/igt@[email protected]

* igt@gem_exec_reloc@basic-write-read-active:
- shard-hsw: [PASS][5] -> [DMESG-WARN][6] ([fdo#110789] / [fdo#110913 ]) +5 similar issues
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-hsw5/igt@[email protected]
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-hsw5/igt@[email protected]

* igt@gem_partial_pwrite_pread@writes-after-reads-display:
- shard-kbl: [PASS][7] -> [DMESG-WARN][8] ([fdo#110913 ]) +5 similar issues
[7]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-kbl6/igt@[email protected]
[8]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-kbl6/igt@[email protected]

* igt@gem_partial_pwrite_pread@writes-after-reads-snoop:
- shard-glk: [PASS][9] -> [DMESG-WARN][10] ([fdo#110913 ]) +5 similar issues
[9]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-glk7/igt@[email protected]
[10]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-glk6/igt@[email protected]
- shard-apl: [PASS][11] -> [DMESG-WARN][12] ([fdo#110913 ]) +1 similar issue
[11]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-apl7/igt@[email protected]
[12]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-apl1/igt@[email protected]

* igt@gem_softpin@noreloc-s3:
- shard-apl: [PASS][13] -> [DMESG-WARN][14] ([fdo#108566]) +1 similar issue
[13]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-apl6/igt@[email protected]
[14]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-apl4/igt@[email protected]

* igt@gem_userptr_blits@map-fixed-invalidate-overlap-busy:
- shard-iclb: [PASS][15] -> [DMESG-WARN][16] ([fdo#110913 ]) +7 similar issues
[15]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb6/igt@[email protected]
[16]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb4/igt@[email protected]

* igt@gem_userptr_blits@sync-unmap-cycles:
- shard-snb: [PASS][17] -> [DMESG-WARN][18] ([fdo#110913 ])
[17]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-snb6/igt@[email protected]
[18]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-snb4/igt@[email protected]
- shard-hsw: [PASS][19] -> [DMESG-WARN][20] ([fdo#110913 ])
[19]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-hsw5/igt@[email protected]
[20]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-hsw8/igt@[email protected]

* igt@i915_pm_rpm@i2c:
- shard-hsw: [PASS][21] -> [FAIL][22] ([fdo#104097])
[21]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-hsw2/igt@[email protected]
[22]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-hsw1/igt@[email protected]

* igt@kms_cursor_crc@pipe-a-cursor-suspend:
- shard-skl: [PASS][23] -> [INCOMPLETE][24] ([fdo#110741])
[23]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl1/igt@[email protected]
[24]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl1/igt@[email protected]

* igt@kms_cursor_crc@pipe-b-cursor-128x42-onscreen:
- shard-kbl: [PASS][25] -> [FAIL][26] ([fdo#103232])
[25]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-kbl4/igt@[email protected]
[26]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-kbl6/igt@[email protected]

* igt@kms_flip@flip-vs-expired-vblank:
- shard-skl: [PASS][27] -> [FAIL][28] ([fdo#105363])
[27]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl8/igt@[email protected]
[28]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl7/igt@[email protected]

* igt@kms_frontbuffer_tracking@fbc-1p-primscrn-shrfb-pgflip-blt:
- shard-iclb: [PASS][29] -> [FAIL][30] ([fdo#103167])
[29]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb1/igt@[email protected]
[30]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb8/igt@[email protected]

* igt@kms_frontbuffer_tracking@fbc-2p-primscrn-spr-indfb-draw-mmap-cpu:
- shard-hsw: [PASS][31] -> [SKIP][32] ([fdo#109271]) +13 similar issues
[31]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-hsw2/igt@kms_frontbuffer_tracking@fbc-2p-primscrn-spr-indfb-draw-mmap-cpu.html
[32]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-hsw1/igt@kms_frontbuffer_tracking@fbc-2p-primscrn-spr-indfb-draw-mmap-cpu.html

* igt@kms_frontbuffer_tracking@fbcpsr-1p-primscrn-cur-indfb-draw-render:
- shard-skl: [PASS][33] -> [FAIL][34] ([fdo#108040])
[33]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl2/igt@kms_frontbuffer_tracking@fbcpsr-1p-primscrn-cur-indfb-draw-render.html
[34]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl10/igt@kms_frontbuffer_tracking@fbcpsr-1p-primscrn-cur-indfb-draw-render.html

* igt@kms_frontbuffer_tracking@fbcpsr-rgb101010-draw-mmap-cpu:
- shard-skl: [PASS][35] -> [FAIL][36] ([fdo#103167] / [fdo#110379])
[35]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl7/igt@[email protected]
[36]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl5/igt@[email protected]

* igt@kms_frontbuffer_tracking@psr-1p-primscrn-cur-indfb-draw-mmap-wc:
- shard-skl: [PASS][37] -> [FAIL][38] ([fdo#103167]) +2 similar issues
[37]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl1/igt@kms_frontbuffer_tracking@psr-1p-primscrn-cur-indfb-draw-mmap-wc.html
[38]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl6/igt@kms_frontbuffer_tracking@psr-1p-primscrn-cur-indfb-draw-mmap-wc.html

* igt@kms_plane_lowres@pipe-a-tiling-y:
- shard-iclb: [PASS][39] -> [FAIL][40] ([fdo#103166])
[39]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb6/igt@[email protected]
[40]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb4/igt@[email protected]

* igt@kms_psr@psr2_primary_mmap_cpu:
- shard-iclb: [PASS][41] -> [SKIP][42] ([fdo#109441]) +3 similar issues
[41]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb2/igt@kms_psr@psr2_primary_mmap_cpu.html
[42]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb6/igt@kms_psr@psr2_primary_mmap_cpu.html

* igt@kms_setmode@basic:
- shard-kbl: [PASS][43] -> [FAIL][44] ([fdo#99912])
[43]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-kbl1/igt@[email protected]
[44]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-kbl3/igt@[email protected]

* igt@kms_vblank@pipe-a-query-idle-hang:
- shard-snb: [PASS][45] -> [DMESG-WARN][46] ([fdo#110789] / [fdo#110913 ]) +4 similar issues
[45]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-snb7/igt@[email protected]
[46]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-snb5/igt@[email protected]

#### Possible fixes ####

* igt@gem_eio@in-flight-contexts-1us:
- shard-iclb: [DMESG-WARN][47] ([fdo#110913 ]) -> [PASS][48] +9 similar issues
[47]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb7/igt@[email protected]
[48]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb2/igt@[email protected]

* igt@gem_eio@wait-wedge-10ms:
- shard-kbl: [DMESG-WARN][49] ([fdo#110913 ]) -> [PASS][50] +5 similar issues
[49]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-kbl1/igt@[email protected]
[50]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-kbl6/igt@[email protected]
- shard-skl: [DMESG-WARN][51] ([fdo#110913 ]) -> [PASS][52]
[51]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl5/igt@[email protected]
[52]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl9/igt@[email protected]

* igt@gem_exec_schedule@wide-bsd2:
- shard-kbl: [INCOMPLETE][53] ([fdo#103665]) -> [PASS][54]
[53]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-kbl7/igt@[email protected]
[54]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-kbl3/igt@[email protected]

* igt@gem_partial_pwrite_pread@writes-after-reads-uncached:
- shard-hsw: [DMESG-WARN][55] ([fdo#110789] / [fdo#110913 ]) -> [PASS][56] +5 similar issues
[55]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-hsw2/igt@[email protected]
[56]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-hsw2/igt@[email protected]

* igt@gem_persistent_relocs@forked-interruptible-faulting-reloc-thrashing:
- shard-apl: [DMESG-WARN][57] ([fdo#110913 ]) -> [PASS][58] +5 similar issues
[57]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-apl8/igt@gem_persistent_relocs@forked-interruptible-faulting-reloc-thrashing.html
[58]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-apl7/igt@gem_persistent_relocs@forked-interruptible-faulting-reloc-thrashing.html

* igt@gem_softpin@evict-active-interruptible:
- shard-glk: [DMESG-WARN][59] ([fdo#110913 ]) -> [PASS][60] +5 similar issues
[59]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-glk8/igt@[email protected]
[60]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-glk1/igt@[email protected]

* igt@gem_softpin@noreloc-s3:
- shard-iclb: [INCOMPLETE][61] ([fdo#107713] / [fdo#109100]) -> [PASS][62] +1 similar issue
[61]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb3/igt@[email protected]
[62]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb1/igt@[email protected]
- shard-skl: [INCOMPLETE][63] ([fdo#104108]) -> [PASS][64]
[63]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl3/igt@[email protected]
[64]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl3/igt@[email protected]

* igt@gem_softpin@softpin:
- shard-snb: [DMESG-WARN][65] ([fdo#110789] / [fdo#110913 ]) -> [PASS][66] +2 similar issues
[65]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-snb1/igt@[email protected]
[66]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-snb1/igt@[email protected]

* igt@gem_tiled_swapping@non-threaded:
- shard-hsw: [FAIL][67] ([fdo#108686]) -> [PASS][68]
[67]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-hsw1/igt@[email protected]
[68]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-hsw2/igt@[email protected]

* igt@gem_userptr_blits@map-fixed-invalidate-overlap-busy-gup:
- shard-snb: [DMESG-WARN][69] ([fdo#110913 ]) -> [PASS][70] +1 similar issue
[69]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-snb5/igt@[email protected]
[70]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-snb7/igt@[email protected]

* igt@i915_pm_rc6_residency@rc6-accuracy:
- shard-snb: [SKIP][71] ([fdo#109271]) -> [PASS][72]
[71]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-snb2/igt@[email protected]
[72]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-snb1/igt@[email protected]

* igt@kms_cursor_crc@pipe-a-cursor-64x21-offscreen:
- shard-iclb: [INCOMPLETE][73] ([fdo#107713]) -> [PASS][74] +1 similar issue
[73]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb1/igt@[email protected]
[74]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb7/igt@[email protected]

* igt@kms_flip@2x-flip-vs-fences-interruptible:
- shard-hsw: [SKIP][75] ([fdo#109271]) -> [PASS][76] +11 similar issues
[75]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-hsw1/igt@[email protected]
[76]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-hsw2/igt@[email protected]

* igt@kms_flip@flip-vs-expired-vblank-interruptible:
- shard-skl: [FAIL][77] ([fdo#105363]) -> [PASS][78]
[77]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl7/igt@[email protected]
[78]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl7/igt@[email protected]

* igt@kms_flip@flip-vs-suspend-interruptible:
- shard-apl: [DMESG-WARN][79] ([fdo#108566]) -> [PASS][80]
[79]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-apl4/igt@[email protected]
[80]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-apl5/igt@[email protected]

* igt@kms_frontbuffer_tracking@fbc-1p-primscrn-cur-indfb-move:
- shard-iclb: [FAIL][81] ([fdo#103167]) -> [PASS][82] +2 similar issues
[81]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb2/igt@[email protected]
[82]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb3/igt@[email protected]

* igt@kms_frontbuffer_tracking@psr-1p-primscrn-spr-indfb-draw-mmap-wc:
- shard-skl: [FAIL][83] ([fdo#103167]) -> [PASS][84] +3 similar issues
[83]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl5/igt@kms_frontbuffer_tracking@psr-1p-primscrn-spr-indfb-draw-mmap-wc.html
[84]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl5/igt@kms_frontbuffer_tracking@psr-1p-primscrn-spr-indfb-draw-mmap-wc.html

* igt@kms_plane_lowres@pipe-a-tiling-x:
- shard-iclb: [FAIL][85] ([fdo#103166]) -> [PASS][86]
[85]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb5/igt@[email protected]
[86]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb2/igt@[email protected]

* igt@kms_psr@no_drrs:
- shard-iclb: [FAIL][87] ([fdo#108341]) -> [PASS][88]
[87]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb1/igt@kms_psr@no_drrs.html
[88]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb3/igt@kms_psr@no_drrs.html

* igt@kms_psr@psr2_primary_blt:
- shard-iclb: [SKIP][89] ([fdo#109441]) -> [PASS][90]
[89]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-iclb8/igt@kms_psr@psr2_primary_blt.html
[90]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-iclb2/igt@kms_psr@psr2_primary_blt.html

* igt@kms_setmode@basic:
- shard-skl: [FAIL][91] ([fdo#99912]) -> [PASS][92]
[91]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-skl1/igt@[email protected]
[92]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-skl1/igt@[email protected]

* igt@kms_sysfs_edid_timing:
- shard-hsw: [FAIL][93] ([fdo#100047]) -> [PASS][94]
[93]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-hsw1/igt@kms_sysfs_edid_timing.html
[94]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-hsw2/igt@kms_sysfs_edid_timing.html

* igt@kms_vblank@pipe-b-wait-forked-busy-hang:
- shard-hsw: [DMESG-WARN][95] ([fdo#110913 ]) -> [PASS][96]
[95]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-hsw5/igt@[email protected]
[96]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-hsw2/igt@[email protected]

* igt@perf_pmu@rc6:
- shard-kbl: [SKIP][97] ([fdo#109271]) -> [PASS][98]
[97]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-kbl2/igt@[email protected]
[98]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-kbl1/igt@[email protected]

#### Warnings ####

* igt@gem_eio@in-flight-suspend:
- shard-glk: [FAIL][99] ([fdo#110667]) -> [DMESG-WARN][100] ([fdo#110913 ])
[99]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6342/shard-glk8/igt@[email protected]
[100]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/shard-glk4/igt@[email protected]

[fdo#100047]: https://bugs.freedesktop.org/show_bug.cgi?id=100047
[fdo#103166]: https://bugs.freedesktop.org/show_bug.cgi?id=103166
[fdo#103167]: https://bugs.freedesktop.org/show_bug.cgi?id=103167
[fdo#103232]: https://bugs.freedesktop.org/show_bug.cgi?id=103232
[fdo#103665]: https://bugs.freedesktop.org/show_bug.cgi?id=103665
[fdo#104097]: https://bugs.freedesktop.org/show_bug.cgi?id=104097
[fdo#104108]: https://bugs.freedesktop.org/show_bug.cgi?id=104108
[fdo#105363]: https://bugs.freedesktop.org/show_bug.cgi?id=105363
[fdo#107713]: https://bugs.freedesktop.org/show_bug.cgi?id=107713
[fdo#108040]: https://bugs.freedesktop.org/show_bug.cgi?id=108040
[fdo#108341]: https://bugs.freedesktop.org/show_bug.cgi?id=108341
[fdo#108566]: https://bugs.freedesktop.org/show_bug.cgi?id=108566
[fdo#108686]: https://bugs.freedesktop.org/show_bug.cgi?id=108686
[fdo#109100]: https://bugs.freedesktop.org/show_bug.cgi?id=109100
[fdo#109271]: https://bugs.freedesktop.org/show_bug.cgi?id=109271
[fdo#109441]: https://bugs.freedesktop.org/show_bug.cgi?id=109441
[fdo#110379]: https://bugs.freedesktop.org/show_bug.cgi?id=110379
[fdo#110667]: https://bugs.freedesktop.org/show_bug.cgi?id=110667
[fdo#110741]: https://bugs.freedesktop.o

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_13414/
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#30 lore raw

✗ Fi.CI.BAT: failure for series starting with [1/6] dma-buf: add dynamic DMA-buf handling v12 (rev2)

In reply to Christian König
== Series Details ==

== Series Details ==

Series: series starting with [1/6] dma-buf: add dynamic DMA-buf handling v12 (rev2)
URL : https://patchwork.freedesktop.org/series/62703/
State : failure

== Summary ==

Applying: dma-buf: add dynamic DMA-buf handling v12
error: patch failed: include/drm/ttm/ttm_bo_api.h:214
error: include/drm/ttm/ttm_bo_api.h: patch does not apply
error: patch failed: include/linux/reservation.h:74
error: include/linux/reservation.h: patch does not apply
error: Did you hand edit your patch?
It does not apply to blobs recorded in its index.
hint: Use 'git am --show-current-patch' to see the failed patch Using index info to reconstruct a base tree...
Patch failed at 0001 dma-buf: add dynamic DMA-buf handling v12 When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx