Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 7.3 361/742] drm/xe/dma-buf: Validate BOs on export to charge the exporter
       [not found] <20261204100635.034361255@linuxfoundation.org>
@ 2026-12-04 10:03 ` Greg Kroah-Hartman
  2026-12-04 10:03 ` [PATCH 7.3 365/742] drm/xe: Dont abort the shrinker walk on -EBUSY Greg Kroah-Hartman
  1 sibling, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2026-12-04 10:03 UTC (permalink / raw)
  To: stable
  Cc: Greg Kroah-Hartman, patches, Matthew Brost, Matthew Auld,
	Dave Airlie, Rodrigo Vivi, intel-xe, Thomas Hellström

7.3-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Thomas Hellström <thomas.hellstrom@linux.intel.com>

commit 22e05ee854e1bdacebcfa36ecd3f33ec80cf897a upstream.

xe_gem_prime_export() calls ttm_bo_setup_export() to make sure the
exporter is charged for the memory backing an exported BO. However,
that only populates a TTM_PL_SYSTEM tt. A BO that has been evicted, or
that has deferred backing, will instead get its memory allocated when
the importer maps it. For p2p imports this happens through
xe_bo_validate() in xe_dma_buf_map(), so the importer, not the
exporter, is charged for the allocation.

Call xe_bo_validate() at export time instead, so that the BO is moved
into its preferred placement and the exporter is charged.

Do the purgeable checks and the validation in the same
xe_validation_guard() transaction, to avoid locking the BO twice.

The commit message and code was drafted using LLM.

Link: https://patchwork.freedesktop.org/patch/747480/?series=172524&rev=1
Fixes: 91494dee1091 ("xe: populate buffers before exporting them.")
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Dave Airlie <airlied@redhat.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: intel-xe@lists.freedesktop.org
Cc: stable@vger.kernel.org # v6.18+
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Link: https://patch.msgid.link/20261111100929.2228-1-thomas.hellstrom@linux.intel.com
Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
(cherry picked from commit 451162b9f6570d19da1c5944aa0a0e3e8f17773d)
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/gpu/drm/xe/xe_dma_buf.c |   80 ++++++++++++++++++++--------------------
 1 file changed, 41 insertions(+), 39 deletions(-)

--- a/drivers/gpu/drm/xe/xe_dma_buf.c
+++ b/drivers/gpu/drm/xe/xe_dma_buf.c
@@ -222,60 +222,62 @@ static const struct dma_buf_ops xe_dmabu
 struct dma_buf *xe_gem_prime_export(struct drm_gem_object *obj, int flags)
 {
 	struct xe_bo *bo = gem_to_xe_bo(obj);
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
 	struct dma_buf *buf;
-	struct ttm_operation_ctx ctx = {
-		.interruptible = true,
-		.no_wait_gpu = true,
-		/* We opt to avoid OOM on system pages allocations */
-		.gfp_retry_mayfail = true,
-		.allow_res_evict = false,
-	};
-	int ret;
+	int ret = 0;
 
 	if (bo->vm)
 		return ERR_PTR(-EPERM);
 
-	/*
-	 * Reject exporting purgeable BOs. DONTNEED BOs can be purged
-	 * at any time, making the exported dma-buf unusable. Purged BOs
-	 * have no backing store and are permanently invalid.
-	 */
-	ret = xe_bo_lock(bo, true);
-	if (ret)
-		return ERR_PTR(ret);
-
-	if (xe_bo_madv_is_dontneed(bo)) {
-		ret = -EBUSY;
-		goto out_unlock;
-	}
+	xe_validation_guard(&ctx, &xe_bo_device(bo)->val, &exec,
+			    (struct xe_val_flags) {.interruptible = true}, ret) {
+		ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
+		drm_exec_retry_on_contention(&exec);
+		if (ret)
+			break;
+
+		/*
+		 * Reject exporting purgeable BOs. DONTNEED BOs can be purged
+		 * at any time, making the exported dma-buf unusable. Purged BOs
+		 * have no backing store and are permanently invalid.
+		 */
+		if (xe_bo_madv_is_dontneed(bo)) {
+			ret = -EBUSY;
+			break;
+		}
+
+		if (xe_bo_is_purged(bo)) {
+			ret = -EINVAL;
+			break;
+		}
+
+		/*
+		 * Make sure the BO is backed in its preferred placement so that
+		 * the exporter, rather than the importer, is charged for the
+		 * memory allocation.
+		 */
+		ret = xe_bo_validate(bo, NULL, false, &exec);
+		drm_exec_retry_on_contention(&exec);
+		xe_validation_retry_on_oom(&ctx, &ret);
+		if (ret)
+			break;
 
-	if (xe_bo_is_purged(bo)) {
-		ret = -EINVAL;
-		goto out_unlock;
+		xe_bo_willneed_get_locked(bo);
 	}
-
-	xe_bo_willneed_get_locked(bo);
-	xe_bo_unlock(bo);
-
-	ret = ttm_bo_setup_export(&bo->ttm, &ctx);
 	if (ret)
-		goto out_put;
+		return ERR_PTR(ret);
 
 	buf = drm_gem_prime_export(obj, flags);
 	if (IS_ERR(buf)) {
-		ret = PTR_ERR(buf);
-		goto out_put;
+		xe_bo_lock(bo, false);
+		xe_bo_willneed_put_locked(bo);
+		xe_bo_unlock(bo);
+		return buf;
 	}
 
 	buf->ops = &xe_dmabuf_ops;
 	return buf;
-
-out_put:
-	xe_bo_lock(bo, false);
-	xe_bo_willneed_put_locked(bo);
-out_unlock:
-	xe_bo_unlock(bo);
-	return ERR_PTR(ret);
 }
 
 static struct drm_gem_object *



^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 7.3 365/742] drm/xe: Dont abort the shrinker walk on -EBUSY
       [not found] <20261204100635.034361255@linuxfoundation.org>
  2026-12-04 10:03 ` [PATCH 7.3 361/742] drm/xe/dma-buf: Validate BOs on export to charge the exporter Greg Kroah-Hartman
@ 2026-12-04 10:03 ` Greg Kroah-Hartman
  1 sibling, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2026-12-04 10:03 UTC (permalink / raw)
  To: stable
  Cc: Greg Kroah-Hartman, patches, Christian König, Matthew Brost,
	Rodrigo Vivi, intel-xe, Thomas Hellström, Dave Airlie

7.3-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Thomas Hellström <thomas.hellstrom@linux.intel.com>

commit 9de430b6fbbdfeee1a5eec33edf53f52c13c869b upstream.

xe_bo_shrink() returns -EBUSY for bos that can't currently be shrunk,
for example imported dma-bufs, bos whose vm is currently validating,
bos that are still busy after the wait timeout, or bos whose backup
fails. The shrinker LRU walk treats any negative return value as
fatal and aborts, which also skips the remaining shrinker passes.

As a result, a single such bo, like an imported dma-buf on the
XE_PL_TT LRU, prevents reclaim of all bos behind it on the LRU.

Skip bos for which xe_bo_shrink() returns -EBUSY and continue the
walk, like the TTM LRU walk does.

v4:
- Patch introduced.

Fixes: 00c8efc3180f ("drm/xe: Add a shrinker for xe bos")
Cc: Christian König <christian.koenig@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: intel-xe@lists.freedesktop.org
Cc: <stable@vger.kernel.org> # v6.15+
Assisted-by: LLM
Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Reviewed-by: Matthew Brost <matthew.brost@intel.com>
Link: https://patch.msgid.link/20261019095655.3501-2-thomas.hellstrom@linux.intel.com
(cherry picked from commit 846f333b3e0b48065f96bee74ffa0b9d390ed05f)
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/gpu/drm/xe/xe_shrinker.c |    3 +++
 1 file changed, 3 insertions(+)

--- a/drivers/gpu/drm/xe/xe_shrinker.c
+++ b/drivers/gpu/drm/xe/xe_shrinker.c
@@ -113,6 +113,9 @@ static int __xe_shrinker_walk(struct xe_
 				continue;
 
 			lret = xe_bo_shrink(ctx, ttm_bo, flags, scanned);
+			/* Like the TTM LRU walk, skip bos we can't shrink now. */
+			if (lret == -EBUSY)
+				continue;
 			if (lret < 0) {
 				ret = lret;
 				goto out;



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04 10:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261204100635.034361255@linuxfoundation.org>
2026-12-04 10:03 ` [PATCH 7.3 361/742] drm/xe/dma-buf: Validate BOs on export to charge the exporter Greg Kroah-Hartman
2026-12-04 10:03 ` [PATCH 7.3 365/742] drm/xe: Dont abort the shrinker walk on -EBUSY Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox