Skip to contentSkip to conversation

Driver-managed exhaustive eviction

PATCH In review Thomas Hellström 5 participants 66 messages Aug 13, 2025 – Aug 22, 2025 Page 2 of 2

Subsystems (4)
lore
Share & tools
lore
author maintainer
#55 lore raw

Re: [PATCH 14/15] drm/xe: Convert xe_bo_create_pin_map() for exhaustive eviction

In reply to Matthew Brost
Quoted 1,088 lines · Matthew Brost
Quoted 1,087 lines · Thomas Hellström

Introduce an xe_bo_create_pin_map_novm() function that does not take the drm_exec paramenter to simplify the conversion of many callsites.
For the rest, ensure that the same drm_exec context that was used for locking the vm is passed down to validation.

Use xe_validation_guard() where appropriate.

---
 drivers/gpu/drm/xe/display/intel_fbdev_fb.c   |  18 +-- drivers/gpu/drm/xe/display/xe_dsb_buffer.c    |  10 +- drivers/gpu/drm/xe/display/xe_fb_pin.c        |  39 +++--- drivers/gpu/drm/xe/display/xe_hdcp_gsc.c      |   8 +- drivers/gpu/drm/xe/tests/xe_migrate.c         |   9 +- drivers/gpu/drm/xe/xe_bo.c                    |  53 +++++++- drivers/gpu/drm/xe/xe_bo.h                    |   6 +- drivers/gpu/drm/xe/xe_gsc.c                   |   8 +- drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c    |  24 ++-- drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c |  24 ++-- drivers/gpu/drm/xe/xe_guc_engine_activity.c   |  13 +- drivers/gpu/drm/xe/xe_lmtt.c                  |  12 +- drivers/gpu/drm/xe/xe_lrc.c                   |   7 +- drivers/gpu/drm/xe/xe_migrate.c               |  20 ++-

 drivers/gpu/drm/xe/xe_oa.c                    |   6 +-
 drivers/gpu/drm/xe/xe_pt.c                    |  10 +-
 drivers/gpu/drm/xe/xe_pt.h                    |   3 +-
 drivers/gpu/drm/xe/xe_pxp_submit.c            |  34 +++--
 drivers/gpu/drm/xe/xe_vm.c                    | 119 ++++++++++----
----
 19 files changed, 252 insertions(+), 171 deletions(-)
drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -0
index d96ba2b51065..8ea9a472113c 100644
@@ -42,11 +42,11 @@ struct intel_framebuffer
 *intel_fbdev_fb_alloc(struct drm_fb_helper *helper,
  	obj = ERR_PTR(-ENODEV);


  if (!IS_DGFX(xe) && !XE_GT_WA(xe_root_mmio_gt(xe), 22019338487_display)) {

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -1
-		obj = xe_bo_create_pin_map(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -2
-					   NULL, size,
-					   ttm_bo_type_kernel,

XE_BO_FLAG_SCANOUT |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -2
-					   XE_BO_FLAG_STOLEN |
-					   XE_BO_FLAG_GGTT);
+		obj = xe_bo_create_pin_map_novm(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +2 -0
+						size,
+						ttm_bo_type_kernel

, XE_BO_FLAG_SCANOUT |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -0
+						XE_BO_FLAG_STOLEN

|

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -0
+						XE_BO_FLAG_GGTT,

false);
  if (!IS_ERR(obj))
  drm_info(&xe->drm, "Allocated fbdev into stolen\n");
  else

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -0
@@ -54,10 +54,10 @@ struct intel_framebuffer
 *intel_fbdev_fb_alloc(struct drm_fb_helper *helper,
  	}


  if (IS_ERR(obj)) {

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -1
-		obj = xe_bo_create_pin_map(xe,

xe_device_get_root_tile(xe), NULL, size,

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -1
-					   ttm_bo_type_kernel,

XE_BO_FLAG_SCANOUT |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -1
-					  

XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -1
-					   XE_BO_FLAG_GGTT);
+		obj = xe_bo_create_pin_map_novm(xe,

xe_device_get_root_tile(xe), size,

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -0
+						ttm_bo_type_kernel

, XE_BO_FLAG_SCANOUT |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -0
+						XE_BO_FLAG_VRAM_IF

_DGFX(xe_device_get_root_tile(xe)) |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -0
+						XE_BO_FLAG_GGTT,

false);
  }

  if (IS_ERR(obj)) {

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +0 -0
index 9f941fc2e36b..58581d7aaae6 100644
@@ -43,11 +43,11 @@ bool intel_dsb_buffer_create(struct intel_crtc
 *crtc, struct intel_dsb_buffer *d
  		return false;


  /* Set scanout flag for WC mapping */

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +0 -1
-	obj = xe_bo_create_pin_map(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +0 -3
-				   NULL, PAGE_ALIGN(size),
-				   ttm_bo_type_kernel,
-				  

XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +0 -1
-				   XE_BO_FLAG_SCANOUT |

XE_BO_FLAG_GGTT);

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +1 -0
+	obj = xe_bo_create_pin_map_novm(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +3 -0
+					PAGE_ALIGN(size),
+					ttm_bo_type_kernel,
+					XE_BO_FLAG_VRAM_IF_DGFX(xe

_device_get_root_tile(xe)) |

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +1 -0
+					XE_BO_FLAG_SCANOUT |

XE_BO_FLAG_GGTT, false);
  if (IS_ERR(obj)) {
  kfree(vma);
  return false;

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -0
index d46ff7ebb0a1..d8e15ebb740c 100644
@@ -102,32 +102,23 @@ static int __xe_pin_fb_vma_dpt(const struct
 intel_framebuffer *fb,
  				 XE_PAGE_SIZE);


  if (IS_DGFX(xe))

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
-						   dpt_size,

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-						   true,
-						  

XE_BO_FLAG_VRAM0 |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   alignment,

false);

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+		dpt = xe_bo_create_pin_map_novm(xe, tile0,

dpt_size,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						ttm_bo_type_kernel

,

drivers/gpu/drm/xe/display/xe_fb_pin.c +3 -0
+						XE_BO_FLAG_VRAM0 |
+						XE_BO_FLAG_GGTT |
+						XE_BO_FLAG_PAGETAB

LE, true);
  else

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
-						   dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-						   true,
-						  

XE_BO_FLAG_STOLEN |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   alignment,

false);

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+		dpt = xe_bo_create_pin_map_novm(xe, tile0,

dpt_size,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						ttm_bo_type_kernel

,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						XE_BO_FLAG_STOLEN

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						XE_BO_FLAG_GGTT |
+						XE_BO_FLAG_PAGETAB

LE, true);
  if (IS_ERR(dpt))

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
-						   dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-						   true,
-						  

XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   alignment,

false);

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+		dpt = xe_bo_create_pin_map_novm(xe, tile0,

dpt_size,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						ttm_bo_type_kernel

,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						XE_BO_FLAG_SYSTEM

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						XE_BO_FLAG_GGTT |
+						XE_BO_FLAG_PAGETAB

LE, true);
  if (IS_ERR(dpt))
  return PTR_ERR(dpt);

drivers/gpu/drm/xe/display/xe_hdcp_gsc.c +0 -0
index 30f1073141fc..4ae847b628e2 100644
@@ -72,10 +72,10 @@ static int
 intel_hdcp_gsc_initialize_message(struct xe_device *xe,
  	int ret = 0;


  /* allocate object of two page for HDCP command memory and store it */

drivers/gpu/drm/xe/display/xe_hdcp_gsc.c +0 -1
-	bo = xe_bo_create_pin_map(xe, xe_device_get_root_tile(xe),

NULL, PAGE_SIZE * 2,

drivers/gpu/drm/xe/display/xe_hdcp_gsc.c +1 -3
-				  ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |
-				  XE_BO_FLAG_GGTT);
+	bo = xe_bo_create_pin_map_novm(xe,

xe_device_get_root_tile(xe), PAGE_SIZE * 2,

drivers/gpu/drm/xe/display/xe_hdcp_gsc.c +3 -0
+				       ttm_bo_type_kernel,
+				       XE_BO_FLAG_SYSTEM |
+				       XE_BO_FLAG_GGTT, false);


  if (IS_ERR(bo)) {
  drm_err(&xe->drm, "Failed to allocate bo for HDCP streaming command!\n");

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
index afa794e56065..5904d658d1f2 100644
@@ -204,7 +204,8 @@ static void xe_migrate_sanity_test(struct
 xe_migrate *m, struct kunit *test,


  big = xe_bo_create_pin_map(xe, tile, m->q->vm, SZ_4M, ttm_bo_type_kernel,

drivers/gpu/drm/xe/tests/xe_migrate.c +2 -1
-				   XE_BO_FLAG_VRAM_IF_DGFX(tile));
+				   XE_BO_FLAG_VRAM_IF_DGFX(tile),
+				   exec);

  if (IS_ERR(big)) {
  KUNIT_FAIL(test, "Failed to allocate bo: %li\n", PTR_ERR(big));
  goto vunmap;

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -212,7 +213,8 @@ static void xe_migrate_sanity_test(struct
 xe_migrate *m, struct kunit *test,


  pt = xe_bo_create_pin_map(xe, tile, m->q->vm, XE_PAGE_SIZE,
    ttm_bo_type_kernel,

drivers/gpu/drm/xe/tests/xe_migrate.c +2 -1
-				  XE_BO_FLAG_VRAM_IF_DGFX(tile));
+				  XE_BO_FLAG_VRAM_IF_DGFX(tile),
+				  exec);

  if (IS_ERR(pt)) {
  KUNIT_FAIL(test, "Failed to allocate fake pt:
%li\n",
     PTR_ERR(pt));

drivers/gpu/drm/xe/tests/xe_migrate.c +2 -1
@@ -222,7 +224,8 @@ static void xe_migrate_sanity_test(struct
 xe_migrate *m, struct kunit *test,
  	tiny = xe_bo_create_pin_map(xe, tile, m->q->vm,
  				    2 * SZ_4K,
  				    ttm_bo_type_kernel,
-				   
 XE_BO_FLAG_VRAM_IF_DGFX(tile));
+				    XE_BO_FLAG_VRAM_IF_DGFX(tile),
+				    exec);
  	if (IS_ERR(tiny)) {

  KUNIT_FAIL(test, "Failed to allocate tiny fake pt:
%li\n",
     PTR_ERR(tiny));

drivers/gpu/drm/xe/xe_bo.c +0 -0
index c9928d4ee5a0..82bf158426ad 100644
@@ -2343,16 +2343,60 @@ xe_bo_create_pin_map_at_novm(struct
 xe_device *xe, struct xe_tile *tile,
  	return ret ? ERR_PTR(ret) : bo;
  }


+/**
+ * xe_bo_create_pin_map() - Create pinned and mapped bo
+ * @xe: The xe device.
+ * @tile: The tile to select for migration of this bo, and the tile used for
+ * @vm: The vm to associate the buffer object with. The vm's resv must be locked
+ * with the transaction represented by @exec.
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @size: The storage size to use for the bo.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @exec: The drm_exec transaction to use for exhaustive eviction, and
+ * previously used for locking @vm's resv.
+ *
+ * Create a pinned and mapped bo. The bo will be external and not associated
+ * with a VM.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set
+ * to true on entry.
+ */
 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     struct xe_vm *vm, size_t size,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-				   enum ttm_bo_type type, u32

flags)

drivers/gpu/drm/xe/xe_bo.c +1 -0
+				   enum ttm_bo_type type, u32

flags,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+				   struct drm_exec *exec)

 {

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) :

XE_VALIDATION_UNIMPLEMENTED;

drivers/gpu/drm/xe/xe_bo.c +1 -1
-
+	xe_assert(xe, exec);

  return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size, ~0ull, type, flags,
         true, 0, exec); }

+/**
+ * xe_bo_create_pin_map_novm() - Create pinned and mapped bo
+ * @xe: The xe device.
+ * @tile: The tile to select for migration of this bo, and the tile used for
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @size: The storage size to use for the bo.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @intr: Whether to execut any waits for backing store interruptible.
+ *
+ * Create a pinned and mapped bo. The bo will be external and not associated
+ * with a VM.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set
+ * to true on entry.
+ */
+struct xe_bo *xe_bo_create_pin_map_novm(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+					size_t size, enum

ttm_bo_type type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +3 -0
+					bool intr)
+{
+	return xe_bo_create_pin_map_at_novm(xe, tile, size, ~0ull,

type, flags, true, 0, intr);

drivers/gpu/drm/xe/xe_bo.c +2 -0
+}
+

 static void __xe_bo_unpin_map_no_vm(void *arg)
 {
  xe_bo_unpin_map_no_vm(arg);

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2365,8 +2409,7 @@ struct xe_bo
 *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile
  	int ret;

KUNIT_STATIC_STUB_REDIRECT(xe_managed_bo_create_pin_map, xe, tile, size, flags);

drivers/gpu/drm/xe/xe_bo.c +0 -2
-
-	bo = xe_bo_create_pin_map(xe, tile, NULL, size,

ttm_bo_type_kernel, flags);

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	bo = xe_bo_create_pin_map_novm(xe, tile, size,

ttm_bo_type_kernel, flags, true);
  if (IS_ERR(bo))
  return bo;

drivers/gpu/drm/xe/xe_bo.h +0 -0
index d06266af9662..802e3c7d7872 100644
@@ -108,7 +108,11 @@ struct xe_bo *xe_bo_create_user(struct

xe_device *xe, struct xe_vm *vm, size_t s
  u16 cpu_caching, u32 flags, struct drm_exec *exec);
 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     struct xe_vm *vm, size_t size,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-				   enum ttm_bo_type type, u32

flags);

drivers/gpu/drm/xe/xe_bo.h +1 -0
+				   enum ttm_bo_type type, u32

flags,

drivers/gpu/drm/xe/xe_bo.h +2 -0
+				   struct drm_exec *exec);
+struct xe_bo *xe_bo_create_pin_map_novm(struct xe_device *xe,

struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.h +1 -0
+					size_t size, enum

ttm_bo_type type, u32 flags,

drivers/gpu/drm/xe/xe_bo.h +1 -0
+					bool intr);

 struct xe_bo *
 xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile *tile,
       size_t size, u64 offset, enum ttm_bo_type type,

drivers/gpu/drm/xe/xe_gsc.c +0 -0
index f5ae28af60d4..83d61bf8ec62 100644
@@ -136,10 +136,10 @@ static int query_compatibility_version(struct
 xe_gsc *gsc)
  	u64 ggtt_offset;
  	int err;

drivers/gpu/drm/xe/xe_gsc.c +0 -1
-	bo = xe_bo_create_pin_map(xe, tile, NULL, GSC_VER_PKT_SZ *

2,

drivers/gpu/drm/xe/xe_gsc.c +1 -3
-				  ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |
-				  XE_BO_FLAG_GGTT);
+	bo = xe_bo_create_pin_map_novm(xe, tile, GSC_VER_PKT_SZ *

2,

drivers/gpu/drm/xe/xe_gsc.c +3 -0
+				       ttm_bo_type_kernel,
+				       XE_BO_FLAG_SYSTEM |
+				       XE_BO_FLAG_GGTT, false);

  if (IS_ERR(bo)) {
  xe_gt_err(gt, "failed to allocate bo for GSC version query\n");
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +0 -0
index 906011671b60..d0a87d7b028b 100644
@@ -1452,7 +1452,6 @@ static bool pf_release_vf_config_lmem(struct

xe_gt *gt, struct xe_gt_sriov_confi
 static int pf_provision_vf_lmem(struct xe_gt *gt, unsigned int vfid, u64 size)
 {
  struct xe_gt_sriov_config *config = pf_pick_vf_config(gt, vfid);

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +0 -1
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;

  struct xe_device *xe = gt_to_xe(gt);
  struct xe_tile *tile = gt_to_tile(gt);
  struct xe_bo *bo;

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +0 -0
@@ -1479,24 +1478,17 @@ static int pf_provision_vf_lmem(struct
 xe_gt *gt, unsigned int vfid, u64 size)
  		return 0;


  xe_gt_assert(gt, pf_get_lmem_alignment(gt) == SZ_2M);

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +5 -8
-	bo = xe_bo_create_locked(xe, tile, NULL,
-				 ALIGN(size, PAGE_SIZE),
-				 ttm_bo_type_kernel,
-				 XE_BO_FLAG_VRAM_IF_DGFX(tile) |
-				 XE_BO_FLAG_NEEDS_2M |
-				 XE_BO_FLAG_PINNED |
-				 XE_BO_FLAG_PINNED_LATE_RESTORE,
-				 exec);
+	bo = xe_bo_create_pin_map_at_novm(xe, tile,
+					  ALIGN(size, PAGE_SIZE),
+					  0,
+					  ttm_bo_type_kernel,
+					 

XE_BO_FLAG_VRAM_IF_DGFX(tile) |

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +2 -0
+					  XE_BO_FLAG_NEEDS_2M |
+					 

XE_BO_FLAG_PINNED_LATE_RESTORE,

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +1 -0
+					  false, 0, false);

  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +0 -7
-	err = xe_bo_pin(bo, exec);
-	xe_bo_unlock(bo);
-	if (unlikely(err)) {
-		xe_bo_put(bo);
-		return err;
-	}
-

  config->lmem_obj = bo;

  if (xe_device_has_lmtt(xe)) {

drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c +0 -0
index c712111aa30d..44cc612b0a75 100644
@@ -55,12 +55,12 @@ static int pf_send_guc_save_vf_state(struct
 xe_gt *gt, unsigned int vfid,
  	xe_gt_assert(gt, size % sizeof(u32) == 0);
  	xe_gt_assert(gt, size == ndwords * sizeof(u32));

drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c +6 -6
-	bo = xe_bo_create_pin_map(xe, tile, NULL,
-				  ALIGN(size, PAGE_SIZE),
-				  ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |
-				  XE_BO_FLAG_GGTT |
-				  XE_BO_FLAG_GGTT_INVALIDATE);
+	bo = xe_bo_create_pin_map_novm(xe, tile,
+				       ALIGN(size, PAGE_SIZE),
+				       ttm_bo_type_kernel,
+				       XE_BO_FLAG_SYSTEM |
+				       XE_BO_FLAG_GGTT |
+				       XE_BO_FLAG_GGTT_INVALIDATE,

false);
  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c +0 -0
@@ -91,12 +91,12 @@ static int pf_send_guc_restore_vf_state(struct
 xe_gt *gt, unsigned int vfid,
  	xe_gt_assert(gt, size % sizeof(u32) == 0);
  	xe_gt_assert(gt, size == ndwords * sizeof(u32));

drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c +6 -6
-	bo = xe_bo_create_pin_map(xe, tile, NULL,
-				  ALIGN(size, PAGE_SIZE),
-				  ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |
-				  XE_BO_FLAG_GGTT |
-				  XE_BO_FLAG_GGTT_INVALIDATE);
+	bo = xe_bo_create_pin_map_novm(xe, tile,
+				       ALIGN(size, PAGE_SIZE),
+				       ttm_bo_type_kernel,
+				       XE_BO_FLAG_SYSTEM |
+				       XE_BO_FLAG_GGTT |
+				       XE_BO_FLAG_GGTT_INVALIDATE,

false);
  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -0
index 92e1f9f41b8c..2b99c1ebdd58 100644
@@ -94,16 +94,17 @@ static int
 allocate_engine_activity_buffers(struct xe_guc *guc,
  	struct xe_tile *tile = gt_to_tile(gt);
  	struct xe_bo *bo, *metadata_bo;

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-	metadata_bo = xe_bo_create_pin_map(gt_to_xe(gt), tile,

NULL, PAGE_ALIGN(metadata_size),

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-					   ttm_bo_type_kernel,

XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-					   XE_BO_FLAG_GGTT |

XE_BO_FLAG_GGTT_INVALIDATE);

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+	metadata_bo = xe_bo_create_pin_map_novm(gt_to_xe(gt),

tile, PAGE_ALIGN(metadata_size),

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+						ttm_bo_type_kernel

, XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+						XE_BO_FLAG_GGTT |

XE_BO_FLAG_GGTT_INVALIDATE,

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+						false);


  if (IS_ERR(metadata_bo))
  return PTR_ERR(metadata_bo);

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-	bo = xe_bo_create_pin_map(gt_to_xe(gt), tile, NULL,

PAGE_ALIGN(size),

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-				  ttm_bo_type_kernel,

XE_BO_FLAG_VRAM_IF_DGFX(tile) |

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-				  XE_BO_FLAG_GGTT |

XE_BO_FLAG_GGTT_INVALIDATE);

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+	bo = xe_bo_create_pin_map_novm(gt_to_xe(gt), tile,

PAGE_ALIGN(size),

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+				       ttm_bo_type_kernel,

XE_BO_FLAG_VRAM_IF_DGFX(tile) |

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+				       XE_BO_FLAG_GGTT |

XE_BO_FLAG_GGTT_INVALIDATE, false);

  if (IS_ERR(bo)) { xe_bo_unpin_map_no_vm(metadata_bo);

drivers/gpu/drm/xe/xe_lmtt.c +0 -0
index a78c9d474a6e..4ad468574174 100644
@@ -67,12 +67,12 @@ static struct xe_lmtt_pt *lmtt_pt_alloc(struct

xe_lmtt *lmtt, unsigned int level
  goto out;
  }

drivers/gpu/drm/xe/xe_lmtt.c +0 -1
-	bo = xe_bo_create_pin_map(lmtt_to_xe(lmtt),

lmtt_to_tile(lmtt), NULL,

drivers/gpu/drm/xe/xe_lmtt.c +0 -1
-				  PAGE_ALIGN(lmtt->ops-
Quoted 1 line

lmtt_pte_size(level) *

drivers/gpu/drm/xe/xe_lmtt.c +0 -1
-					     lmtt->ops-
Quoted 1 line

lmtt_pte_num(level)),

drivers/gpu/drm/xe/xe_lmtt.c +0 -2
-				  ttm_bo_type_kernel,
-				 

XE_BO_FLAG_VRAM_IF_DGFX(lmtt_to_tile(lmtt)) |

drivers/gpu/drm/xe/xe_lmtt.c +1 -1
-				  XE_BO_FLAG_NEEDS_64K);
+	bo = xe_bo_create_pin_map_novm(lmtt_to_xe(lmtt),

lmtt_to_tile(lmtt),

drivers/gpu/drm/xe/xe_lmtt.c +1 -0
+				       PAGE_ALIGN(lmtt->ops-
Quoted 1 line

lmtt_pte_size(level) *

drivers/gpu/drm/xe/xe_lmtt.c +1 -0
+						  lmtt->ops-
Quoted 1 line

lmtt_pte_num(level)),

drivers/gpu/drm/xe/xe_lmtt.c +2 -0
+				       ttm_bo_type_kernel,
+				      

XE_BO_FLAG_VRAM_IF_DGFX(lmtt_to_tile(lmtt)) |

drivers/gpu/drm/xe/xe_lmtt.c +1 -0
+				       XE_BO_FLAG_NEEDS_64K,

false);
  if (IS_ERR(bo)) {
  err = PTR_ERR(bo);
  goto out_free_pt;

drivers/gpu/drm/xe/xe_lrc.c +0 -0
index 8f6c3ba47882..6d52e0eb97f5 100644
@@ -1340,9 +1340,10 @@ static int xe_lrc_init(struct xe_lrc *lrc,
 struct xe_hw_engine *hwe,

  if (vm && vm->xef) /* userspace */ bo_flags |= XE_BO_FLAG_PINNED_LATE_RESTORE;

drivers/gpu/drm/xe/xe_lrc.c +4 -3
-	lrc->bo = xe_bo_create_pin_map(xe, tile, NULL, bo_size,
-				       ttm_bo_type_kernel,
-				       bo_flags);
+	lrc->bo = xe_bo_create_pin_map_novm(xe, tile,
+					    bo_size,
+					    ttm_bo_type_kernel,
+					    bo_flags, false);

  if (IS_ERR(lrc->bo))
  return PTR_ERR(lrc->bo);

drivers/gpu/drm/xe/xe_migrate.c +1 -0
index ddfad7506a82..fe0d15ab340e 100644
@@ -35,6 +35,7 @@
  #include "xe_sched_job.h"
  #include "xe_sync.h"
  #include "xe_trace_bo.h"
+#include "xe_validation.h"
  #include "xe_vm.h"
  #include "xe_vram.h"

drivers/gpu/drm/xe/xe_migrate.c +0 -0
@@ -173,7 +174,7 @@ static void xe_migrate_program_identity(struct
 xe_device *xe, struct xe_vm *vm,
  }


 static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,

drivers/gpu/drm/xe/xe_migrate.c +1 -1
-				 struct xe_vm *vm)
+				 struct xe_vm *vm, struct drm_exec

*exec)
 {
  struct xe_device *xe = tile_to_xe(tile);
  u16 pat_index = xe->pat.idx[XE_CACHE_WB];

drivers/gpu/drm/xe/xe_migrate.c +0 -0
@@ -200,7 +201,7 @@ static int xe_migrate_prepare_vm(struct xe_tile
 *tile, struct xe_migrate *m,
  				  num_entries * XE_PAGE_SIZE,
  				  ttm_bo_type_kernel,

    XE_BO_FLAG_VRAM_IF_DGFX(tile) |

drivers/gpu/drm/xe/xe_migrate.c +1 -1
-				  XE_BO_FLAG_PAGETABLE);
+				  XE_BO_FLAG_PAGETABLE, exec);

  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_migrate.c +2 -0
@@ -404,6 +405,8 @@ int xe_migrate_init(struct xe_migrate *m)
  	struct xe_tile *tile = m->tile;
  	struct xe_gt *primary_gt = tile->primary_gt;
  	struct xe_device *xe = tile_to_xe(tile);
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
  	struct xe_vm *vm;
  	int err;

drivers/gpu/drm/xe/xe_migrate.c +0 -0
@@ -413,11 +416,16 @@ int xe_migrate_init(struct xe_migrate *m)
  	if (IS_ERR(vm))
  		return PTR_ERR(vm);

drivers/gpu/drm/xe/xe_migrate.c +2 -3
-	xe_vm_lock(vm, false);
-	err = xe_migrate_prepare_vm(tile, m, vm);
-	xe_vm_unlock(vm);
+	err = 0;
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, err, false)

{

drivers/gpu/drm/xe/xe_migrate.c +6 -0
+		err = xe_vm_drm_exec_lock(vm, &exec);
+		drm_exec_retry_on_contention(&exec);
+		err = xe_migrate_prepare_vm(tile, m, vm, &exec);
+		drm_exec_retry_on_contention(&exec);
+		xe_validation_retry_on_oom(&ctx, &err);
+	}

  if (err)

drivers/gpu/drm/xe/xe_migrate.c +1 -1
-		goto err_out;
+		return err;


  if (xe->info.has_usm) {
  struct xe_hw_engine *hwe =
xe_gt_hw_engine(primary_gt,

drivers/gpu/drm/xe/xe_oa.c +0 -0
index a188bad172ad..a4894eb0d7f3 100644
@@ -883,9 +883,9 @@ static int xe_oa_alloc_oa_buffer(struct
 xe_oa_stream *stream, size_t size)
  {
  	struct xe_bo *bo;

drivers/gpu/drm/xe/xe_oa.c +0 -1
-	bo = xe_bo_create_pin_map(stream->oa->xe, stream->gt-
Quoted 1 line

tile, NULL,

drivers/gpu/drm/xe/xe_oa.c +0 -2
-				  size, ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_GGTT);

drivers/gpu/drm/xe/xe_oa.c +1 -0
+	bo = xe_bo_create_pin_map_novm(stream->oa->xe, stream->gt-
Quoted 1 line

tile,

drivers/gpu/drm/xe/xe_oa.c +2 -0
+				       size, ttm_bo_type_kernel,
+				       XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_GGTT, false);
  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_pt.c +1 -0
index f3a39e734a90..33ad40418ceb 100644
@@ -88,6 +88,7 @@ static void xe_pt_free(struct xe_pt *pt)
   * @vm: The vm to create for.
   * @tile: The tile to create for.
   * @level: The page-table level.
+ * @exec: The drm_exec object used to lock the vm.
   *
   * Allocate and initialize a single struct xe_pt metadata

structure. Also
  * create the corresponding page-table bo, but don't initialize it. If the

drivers/gpu/drm/xe/xe_pt.c +1 -1
@@ -99,7 +100,7 @@ static void xe_pt_free(struct xe_pt *pt)
   * error.
   */
  struct xe_pt *xe_pt_create(struct xe_vm *vm, struct xe_tile *tile,
-			   unsigned int level)
+			   unsigned int level, struct drm_exec
 *exec)
  {
  	struct xe_pt *pt;

  struct xe_bo *bo;

drivers/gpu/drm/xe/xe_pt.c +0 -0
@@ -123,9 +124,11 @@ struct xe_pt *xe_pt_create(struct xe_vm *vm,
 struct xe_tile *tile,
  		bo_flags |= XE_BO_FLAG_PINNED_LATE_RESTORE;


  pt->level = level;

drivers/gpu/drm/xe/xe_pt.c +2 -0
+
+	drm_WARN_ON(&vm->xe->drm, IS_ERR_OR_NULL(exec));

  bo = xe_bo_create_pin_map(vm->xe, tile, vm, SZ_4K, ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_pt.c +1 -1
-				  bo_flags);
+				  bo_flags, exec);

  if (IS_ERR(bo)) {
  err = PTR_ERR(bo);
  goto err_kfree;

drivers/gpu/drm/xe/xe_pt.c +0 -0
@@ -589,7 +592,8 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent,
 pgoff_t offset,
  	if (covers || !*child) {
  		u64 flags = 0;

drivers/gpu/drm/xe/xe_pt.c +0 -1
-		xe_child = xe_pt_create(xe_walk->vm, xe_walk-
Quoted 1 line

tile, level - 1);

drivers/gpu/drm/xe/xe_pt.c +1 -0
+		xe_child = xe_pt_create(xe_walk->vm, xe_walk-
Quoted 1 line

tile, level - 1,

drivers/gpu/drm/xe/xe_pt.c +1 -0
+					xe_vm_validation_exec(vm))

;
  if (IS_ERR(xe_child))
  return PTR_ERR(xe_child);

drivers/gpu/drm/xe/xe_pt.h +0 -0
index 5ecf003d513c..4daeebaab5a1 100644
@@ -10,6 +10,7 @@
  #include "xe_pt_types.h"


 struct dma_fence;
+struct drm_exec;
 struct xe_bo;
 struct xe_device;
 struct xe_exec_queue;

drivers/gpu/drm/xe/xe_pt.h +0 -0
@@ -29,7 +30,7 @@ struct xe_vma_ops;
  unsigned int xe_pt_shift(unsigned int level);


 struct xe_pt *xe_pt_create(struct xe_vm *vm, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_pt.h +1 -1
-			   unsigned int level);
+			   unsigned int level, struct drm_exec

*exec);

 void xe_pt_populate_empty(struct xe_tile *tile, struct xe_vm *vm, struct xe_pt *pt);

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -0
index ca95f2a4d4ef..54bd6b64dc6d 100644
@@ -54,8 +54,9 @@ static int
 allocate_vcs_execution_resources(struct xe_pxp *pxp)
  	 * Each termination is 16 DWORDS, so 4K is enough to

contain a
  * termination for each sessions.
  */

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -1
-	bo = xe_bo_create_pin_map(xe, tile, NULL, SZ_4K,

ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -1
-				  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_PINNED | XE_BO_FLAG_GGTT);

drivers/gpu/drm/xe/xe_pxp_submit.c +1 -0
+	bo = xe_bo_create_pin_map_novm(xe, tile, SZ_4K,

ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_pxp_submit.c +1 -0
+				       XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_PINNED | XE_BO_FLAG_GGTT,

drivers/gpu/drm/xe/xe_pxp_submit.c +1 -0
+				       false);

  if (IS_ERR(bo)) {
  err = PTR_ERR(bo);
  goto out_queue;

drivers/gpu/drm/xe/xe_pxp_submit.c +2 -0
@@ -87,7 +88,9 @@ static int allocate_gsc_client_resources(struct
 xe_gt *gt,
  {
  	struct xe_tile *tile = gt_to_tile(gt);
  	struct xe_device *xe = tile_to_xe(tile);
+	struct xe_validation_ctx ctx;
  	struct xe_hw_engine *hwe;
+	struct drm_exec exec;
  	struct xe_vm *vm;
  	struct xe_bo *bo;

  struct xe_exec_queue *q;

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -0
@@ -106,15 +109,26 @@ static int
 allocate_gsc_client_resources(struct xe_gt *gt,
  		return PTR_ERR(vm);


  /* We allocate a single object for the batch and the in/out memory */

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -2
-	xe_vm_lock(vm, false);
-	bo = xe_bo_create_pin_map(xe, tile, vm, PXP_BB_SIZE +

inout_size * 2,

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -2
-				  ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_PINNED | XE_BO_FLAG_NEEDS_UC);

drivers/gpu/drm/xe/xe_pxp_submit.c +2 -4
-	xe_vm_unlock(vm);
-	if (IS_ERR(bo)) {
-		err = PTR_ERR(bo);
-		goto vm_out;
+
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, err, false)

{

drivers/gpu/drm/xe/xe_pxp_submit.c +6 -0
+		err = xe_vm_drm_exec_lock(vm, &exec);
+		drm_exec_retry_on_contention(&exec);
+		if (err)
+			break;
+
+		bo = xe_bo_create_pin_map(xe, tile, vm,

PXP_BB_SIZE + inout_size * 2,

drivers/gpu/drm/xe/xe_pxp_submit.c +2 -0
+					  ttm_bo_type_kernel,
+					  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_PINNED |

drivers/gpu/drm/xe/xe_pxp_submit.c +1 -0
+					  XE_BO_FLAG_NEEDS_UC,

&exec);

drivers/gpu/drm/xe/xe_pxp_submit.c +6 -0
+		drm_exec_retry_on_contention(&exec);
+		if (IS_ERR(bo)) {
+			err = PTR_ERR(bo);
+			xe_validation_retry_on_oom(&ctx, &err);
+			break;
+		}

  }

drivers/gpu/drm/xe/xe_pxp_submit.c +2 -0
+	if (err)
+		goto vm_out;


  fence = xe_vm_bind_kernel_bo(vm, bo, NULL, 0, XE_CACHE_WB);
  if (IS_ERR(fence)) {

drivers/gpu/drm/xe/xe_vm.c +1 -0
index 989d84c2e82f..b3ee65126841 100644
@@ -1606,6 +1606,7 @@ static void vm_destroy_work_func(struct
 work_struct *w);
   * @xe: xe device.
   * @tile: tile to set up for.
   * @vm: vm to set up for.
+ * @exec: The struct drm_exec object used to lock the vm resv.
   *
   * Sets up a pagetable tree with one page-table per level and a

single
  * leaf PTE. All pagetable entries point to the single page-table or,

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -1615,20 +1616,19 @@ static void vm_destroy_work_func(struct
 work_struct *w);
   * Return: 0 on success, negative error code on error.
   */

 static int xe_vm_create_scratch(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_vm.c +1 -1
-				struct xe_vm *vm)
+				struct xe_vm *vm, struct drm_exec

*exec)
 {
  u8 id = tile->id;
  int i;

  for (i = MAX_HUGEPTE_LEVEL; i < vm->pt_root[id]->level; i++) {

drivers/gpu/drm/xe/xe_vm.c +1 -1
-		vm->scratch_pt[id][i] = xe_pt_create(vm, tile, i);
+		vm->scratch_pt[id][i] = xe_pt_create(vm, tile, i,

exec);
  if (IS_ERR(vm->scratch_pt[id][i])) { int err = PTR_ERR(vm->scratch_pt[id][i]);
  vm->scratch_pt[id][i] = NULL; return err;
  }
-
  xe_pt_populate_empty(tile, vm, vm-

Quoted 1 line

scratch_pt[id][i]);

  }

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -1656,9 +1656,26 @@ static void xe_vm_free_scratch(struct xe_vm
 *vm)
  	}
  }

drivers/gpu/drm/xe/xe_vm.c +10 -0
+static void xe_vm_pt_destroy(struct xe_vm *vm)
+{
+	struct xe_tile *tile;
+	u8 id;
+
+	xe_vm_assert_held(vm);
+
+	for_each_tile(tile, vm->xe, id) {
+		if (vm->pt_root[id]) {
+			xe_pt_destroy(vm->pt_root[id], vm->flags,

NULL);

drivers/gpu/drm/xe/xe_vm.c +5 -0
+			vm->pt_root[id] = NULL;
+		}
+	}
+}
+

 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags, struct xe_file *xef)
 {
  struct drm_gem_object *vm_resv_obj;

drivers/gpu/drm/xe/xe_vm.c +2 -0
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;

  struct xe_vm *vm;
  int err, number_tiles = 0;
  struct xe_tile *tile;

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -1745,49 +1762,64 @@ struct xe_vm *xe_vm_create(struct xe_device
 *xe, u32 flags, struct xe_file *xef)


  drm_gem_object_put(vm_resv_obj);

drivers/gpu/drm/xe/xe_vm.c +2 -3
-	err = xe_vm_lock(vm, true);
-	if (err)
-		goto err_close;
+	err = 0;
+	xe_validation_guard(&ctx, &xe->val, &exec,

DRM_EXEC_INTERRUPTIBLE_WAIT,

drivers/gpu/drm/xe/xe_vm.c +3 -0
+			    err, true) {
+		err = xe_vm_drm_exec_lock(vm, &exec);
+		drm_exec_retry_on_contention(&exec);

drivers/gpu/drm/xe/xe_vm.c +0 -1
-	if (IS_DGFX(xe) && xe->info.vram_flags &

XE_VRAM_FLAGS_NEED64K)

drivers/gpu/drm/xe/xe_vm.c +1 -1
-		vm->flags |= XE_VM_FLAG_64K;
+		if (IS_DGFX(xe) && xe->info.vram_flags &

XE_VRAM_FLAGS_NEED64K)

drivers/gpu/drm/xe/xe_vm.c +1 -0
+			vm->flags |= XE_VM_FLAG_64K;

drivers/gpu/drm/xe/xe_vm.c +4 -4
-	for_each_tile(tile, xe, id) {
-		if (flags & XE_VM_FLAG_MIGRATION &&
-		    tile->id != XE_VM_FLAG_TILE_ID(flags))
-			continue;
+		for_each_tile(tile, xe, id) {
+			if (flags & XE_VM_FLAG_MIGRATION &&
+			    tile->id != XE_VM_FLAG_TILE_ID(flags))
+				continue;

drivers/gpu/drm/xe/xe_vm.c +0 -1
-		vm->pt_root[id] = xe_pt_create(vm, tile, xe-
Quoted 1 line

info.vm_max_level);

drivers/gpu/drm/xe/xe_vm.c +1 -4
-		if (IS_ERR(vm->pt_root[id])) {
-			err = PTR_ERR(vm->pt_root[id]);
-			vm->pt_root[id] = NULL;
-			goto err_unlock_close;
+			vm->pt_root[id] = xe_pt_create(vm, tile,

xe->info.vm_max_level,

drivers/gpu/drm/xe/xe_vm.c +6 -0
+						       &exec);
+			if (IS_ERR(vm->pt_root[id])) {
+				err = PTR_ERR(vm->pt_root[id]);
+				vm->pt_root[id] = NULL;
+				xe_vm_pt_destroy(vm);
+				drm_exec_retry_on_contention(&exec

);

drivers/gpu/drm/xe/xe_vm.c +1 -0
+				xe_validation_retry_on_oom(&ctx,

&err);

drivers/gpu/drm/xe/xe_vm.c +2 -0
+				goto err_close;
+			}

  }

drivers/gpu/drm/xe/xe_vm.c +0 -1
-	}

drivers/gpu/drm/xe/xe_vm.c +6 -1
-	if (xe_vm_has_scratch(vm)) {
+		if (xe_vm_has_scratch(vm)) {
+			for_each_tile(tile, xe, id) {
+				if (!vm->pt_root[id])
+					continue;
+
+				err = xe_vm_create_scratch(xe,

tile, vm, &exec);

drivers/gpu/drm/xe/xe_vm.c +4 -0
+				if (err) {
+					xe_vm_free_scratch(vm);
+					xe_vm_pt_destroy(vm);
+					drm_exec_retry_on_contenti

on(&exec);

drivers/gpu/drm/xe/xe_vm.c +1 -0
+					xe_validation_retry_on_oom

(&ctx, &err);

drivers/gpu/drm/xe/xe_vm.c +8 -0
+					goto err_close;
+				}
+			}
+			vm->batch_invalidate_tlb = true;
+		}
+
+		if (vm->flags & XE_VM_FLAG_LR_MODE) {
+			INIT_WORK(&vm->preempt.rebind_work,

preempt_rebind_work_func);

drivers/gpu/drm/xe/xe_vm.c +4 -0
+			vm->batch_invalidate_tlb = false;
+		}
+
+		/* Fill pt_root after allocating scratch tables */

  for_each_tile(tile, xe, id) {
  if (!vm->pt_root[id])
  continue;

drivers/gpu/drm/xe/xe_vm.c +1 -3
-			err = xe_vm_create_scratch(xe, tile, vm);
-			if (err)
-				goto err_unlock_close;
+			xe_pt_populate_empty(tile, vm, vm-
Quoted 1 line

pt_root[id]);

  }

drivers/gpu/drm/xe/xe_vm.c +0 -12
-		vm->batch_invalidate_tlb = true;
-	}
-
-	if (vm->flags & XE_VM_FLAG_LR_MODE)
-		vm->batch_invalidate_tlb = false;
-
-	/* Fill pt_root after allocating scratch tables */
-	for_each_tile(tile, xe, id) {
-		if (!vm->pt_root[id])
-			continue;
-
-		xe_pt_populate_empty(tile, vm, vm->pt_root[id]);

  }

drivers/gpu/drm/xe/xe_vm.c +2 -1
-	xe_vm_unlock(vm);
+	if (err)
+		goto err_close;


  /* Kernel migration VM shouldn't have a circular loop.. */ if (!(flags & XE_VM_FLAG_MIGRATION)) {

drivers/gpu/drm/xe/xe_vm.c +1 -1
@@ -1820,7 +1852,7 @@ struct xe_vm *xe_vm_create(struct xe_device
 *xe, u32 flags, struct xe_file *xef)
  				      &xe->usm.next_asid,
 GFP_KERNEL);
  		up_write(&xe->usm.lock);
  		if (err < 0)
-			goto err_unlock_close;
+			goto err_close;


  vm->usm.asid = asid;
  }

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -1829,8 +1861,6 @@ struct xe_vm *xe_vm_create(struct xe_device
 *xe, u32 flags, struct xe_file *xef)


  return vm;

drivers/gpu/drm/xe/xe_vm.c +0 -2
-err_unlock_close:
-	xe_vm_unlock(vm);

 err_close:
  xe_vm_close_and_put(vm);
  return ERR_PTR(err);

drivers/gpu/drm/xe/xe_vm.c +1 -7
@@ -1959,13 +1989,7 @@ void xe_vm_close_and_put(struct xe_vm *vm)
  	 * destroy the pagetables immediately.
  	 */
  	xe_vm_free_scratch(vm);
-
-	for_each_tile(tile, xe, id) {
-		if (vm->pt_root[id]) {
-			xe_pt_destroy(vm->pt_root[id], vm->flags,
 NULL);
-			vm->pt_root[id] = NULL;
-		}
-	}
+	xe_vm_pt_destroy(vm);
  	xe_vm_unlock(vm);


  /*

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -3845,7 +3869,6 @@ struct dma_fence *xe_vm_bind_kernel_bo(struct
 xe_vm *vm, struct xe_bo *bo,
   */
  int xe_vm_lock(struct xe_vm *vm, bool intr)
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_bind_kernel_bo(struct · line 3849

Quoted 2 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
  {
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;

You add this earlier in the series and then delete it here, which seems
odd. If the intent is to apply a workaround (WA) earlier and finalize it
here, that makes sense.

Yes, the patch that introduces these identifies all the call-sites where we need a fix-up, and uses lockdep to verify that a validation transaction starting there doesn't violate the dma_resv locking orders.

Most of the series then eliminates these _UNIMPLEMENTED one by one. Of course one could do it in the opposite order to avoid introducing and then eliminating these but I think that would be harder both for the developer and reviewer.

Quoted 8 lines

However, deleting it here appears to leave a problematic path in xe_svm.c around xe_vm_range_rebind, where vm->validating._exec may remain unset/stale. I’m surprised CI didn’t catch this—perhaps due to an
unbalanced xe_vm_set_validation_exec leaving stale state—or I’m missing
something.

Yes, you are correct. There was an imbalance and I will fix that.

Thanks,
Thomas

Quoted 27 lines

Matt

Quoted 24 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
  	int ret;


  if (intr)

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -3853,9 +3876,6 @@ int xe_vm_lock(struct xe_vm *vm, bool intr)

  else
  ret = dma_resv_lock(xe_vm_resv(vm), NULL);

drivers/gpu/drm/xe/xe_vm.c +0 -3
-	if (!ret)
-		xe_vm_set_validation_exec(vm, exec);
-

  return ret;
 }

drivers/gpu/drm/xe/xe_vm.c +0 -1
@@ -3867,7 +3887,6 @@ int xe_vm_lock(struct xe_vm *vm, bool intr)
   */
  void xe_vm_unlock(struct xe_vm *vm)
  {
-	xe_vm_set_validation_exec(vm, NULL);
  	dma_resv_unlock(xe_vm_resv(vm));
  }

Signature · 1 line
2.50.1
maintainer
#56 lore raw

Re: [PATCH 14/15] drm/xe: Convert xe_bo_create_pin_map() for exhaustive eviction

Quoted 1,098 lines · Thomas Hellström
Quoted 1,088 lines · Matthew Brost
Quoted 1,087 lines · Thomas Hellström

Introduce an xe_bo_create_pin_map_novm() function that does not take the drm_exec paramenter to simplify the conversion of many callsites.
For the rest, ensure that the same drm_exec context that was used for locking the vm is passed down to validation.

Use xe_validation_guard() where appropriate.

---
 drivers/gpu/drm/xe/display/intel_fbdev_fb.c   |  18 +-- drivers/gpu/drm/xe/display/xe_dsb_buffer.c    |  10 +- drivers/gpu/drm/xe/display/xe_fb_pin.c        |  39 +++--- drivers/gpu/drm/xe/display/xe_hdcp_gsc.c      |   8 +- drivers/gpu/drm/xe/tests/xe_migrate.c         |   9 +- drivers/gpu/drm/xe/xe_bo.c                    |  53 +++++++- drivers/gpu/drm/xe/xe_bo.h                    |   6 +- drivers/gpu/drm/xe/xe_gsc.c                   |   8 +- drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c    |  24 ++-- drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c |  24 ++-- drivers/gpu/drm/xe/xe_guc_engine_activity.c   |  13 +- drivers/gpu/drm/xe/xe_lmtt.c                  |  12 +- drivers/gpu/drm/xe/xe_lrc.c                   |   7 +- drivers/gpu/drm/xe/xe_migrate.c               |  20 ++-

 drivers/gpu/drm/xe/xe_oa.c                    |   6 +-
 drivers/gpu/drm/xe/xe_pt.c                    |  10 +-
 drivers/gpu/drm/xe/xe_pt.h                    |   3 +-
 drivers/gpu/drm/xe/xe_pxp_submit.c            |  34 +++--
 drivers/gpu/drm/xe/xe_vm.c                    | 119 ++++++++++----
----
 19 files changed, 252 insertions(+), 171 deletions(-)
drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -0
index d96ba2b51065..8ea9a472113c 100644
@@ -42,11 +42,11 @@ struct intel_framebuffer
 *intel_fbdev_fb_alloc(struct drm_fb_helper *helper,
  	obj = ERR_PTR(-ENODEV);


  if (!IS_DGFX(xe) && !XE_GT_WA(xe_root_mmio_gt(xe), 22019338487_display)) {

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -1
-		obj = xe_bo_create_pin_map(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -2
-					   NULL, size,
-					   ttm_bo_type_kernel,

XE_BO_FLAG_SCANOUT |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -2
-					   XE_BO_FLAG_STOLEN |
-					   XE_BO_FLAG_GGTT);
+		obj = xe_bo_create_pin_map_novm(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +2 -0
+						size,
+						ttm_bo_type_kernel

, XE_BO_FLAG_SCANOUT |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -0
+						XE_BO_FLAG_STOLEN

|

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -0
+						XE_BO_FLAG_GGTT,

false);
  if (!IS_ERR(obj))
  drm_info(&xe->drm, "Allocated fbdev into stolen\n");
  else

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -0
@@ -54,10 +54,10 @@ struct intel_framebuffer
 *intel_fbdev_fb_alloc(struct drm_fb_helper *helper,
  	}


  if (IS_ERR(obj)) {

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -1
-		obj = xe_bo_create_pin_map(xe,

xe_device_get_root_tile(xe), NULL, size,

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -1
-					   ttm_bo_type_kernel,

XE_BO_FLAG_SCANOUT |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +0 -1
-					  

XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -1
-					   XE_BO_FLAG_GGTT);
+		obj = xe_bo_create_pin_map_novm(xe,

xe_device_get_root_tile(xe), size,

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -0
+						ttm_bo_type_kernel

, XE_BO_FLAG_SCANOUT |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -0
+						XE_BO_FLAG_VRAM_IF

_DGFX(xe_device_get_root_tile(xe)) |

drivers/gpu/drm/xe/display/intel_fbdev_fb.c +1 -0
+						XE_BO_FLAG_GGTT,

false);
  }

  if (IS_ERR(obj)) {

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +0 -0
index 9f941fc2e36b..58581d7aaae6 100644
@@ -43,11 +43,11 @@ bool intel_dsb_buffer_create(struct intel_crtc
 *crtc, struct intel_dsb_buffer *d
  		return false;


  /* Set scanout flag for WC mapping */

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +0 -1
-	obj = xe_bo_create_pin_map(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +0 -3
-				   NULL, PAGE_ALIGN(size),
-				   ttm_bo_type_kernel,
-				  

XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +0 -1
-				   XE_BO_FLAG_SCANOUT |

XE_BO_FLAG_GGTT);

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +1 -0
+	obj = xe_bo_create_pin_map_novm(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +3 -0
+					PAGE_ALIGN(size),
+					ttm_bo_type_kernel,
+					XE_BO_FLAG_VRAM_IF_DGFX(xe

_device_get_root_tile(xe)) |

drivers/gpu/drm/xe/display/xe_dsb_buffer.c +1 -0
+					XE_BO_FLAG_SCANOUT |

XE_BO_FLAG_GGTT, false);
  if (IS_ERR(obj)) {
  kfree(vma);
  return false;

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -0
index d46ff7ebb0a1..d8e15ebb740c 100644
@@ -102,32 +102,23 @@ static int __xe_pin_fb_vma_dpt(const struct
 intel_framebuffer *fb,
  				 XE_PAGE_SIZE);


  if (IS_DGFX(xe))

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
-						   dpt_size,

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-						   true,
-						  

XE_BO_FLAG_VRAM0 |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   alignment,

false);

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+		dpt = xe_bo_create_pin_map_novm(xe, tile0,

dpt_size,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						ttm_bo_type_kernel

,

drivers/gpu/drm/xe/display/xe_fb_pin.c +3 -0
+						XE_BO_FLAG_VRAM0 |
+						XE_BO_FLAG_GGTT |
+						XE_BO_FLAG_PAGETAB

LE, true);
  else

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
-						   dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-						   true,
-						  

XE_BO_FLAG_STOLEN |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   alignment,

false);

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+		dpt = xe_bo_create_pin_map_novm(xe, tile0,

dpt_size,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						ttm_bo_type_kernel

,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						XE_BO_FLAG_STOLEN

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						XE_BO_FLAG_GGTT |
+						XE_BO_FLAG_PAGETAB

LE, true);
  if (IS_ERR(dpt))

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
-						   dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -2
-						   true,
-						  

XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						   alignment,

false);

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+		dpt = xe_bo_create_pin_map_novm(xe, tile0,

dpt_size,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						ttm_bo_type_kernel

,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						XE_BO_FLAG_SYSTEM

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						XE_BO_FLAG_GGTT |
+						XE_BO_FLAG_PAGETAB

LE, true);
  if (IS_ERR(dpt))
  return PTR_ERR(dpt);

drivers/gpu/drm/xe/display/xe_hdcp_gsc.c +0 -0
index 30f1073141fc..4ae847b628e2 100644
@@ -72,10 +72,10 @@ static int
 intel_hdcp_gsc_initialize_message(struct xe_device *xe,
  	int ret = 0;


  /* allocate object of two page for HDCP command memory and store it */

drivers/gpu/drm/xe/display/xe_hdcp_gsc.c +0 -1
-	bo = xe_bo_create_pin_map(xe, xe_device_get_root_tile(xe),

NULL, PAGE_SIZE * 2,

drivers/gpu/drm/xe/display/xe_hdcp_gsc.c +1 -3
-				  ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |
-				  XE_BO_FLAG_GGTT);
+	bo = xe_bo_create_pin_map_novm(xe,

xe_device_get_root_tile(xe), PAGE_SIZE * 2,

drivers/gpu/drm/xe/display/xe_hdcp_gsc.c +3 -0
+				       ttm_bo_type_kernel,
+				       XE_BO_FLAG_SYSTEM |
+				       XE_BO_FLAG_GGTT, false);


  if (IS_ERR(bo)) {
  drm_err(&xe->drm, "Failed to allocate bo for HDCP streaming command!\n");

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
index afa794e56065..5904d658d1f2 100644
@@ -204,7 +204,8 @@ static void xe_migrate_sanity_test(struct
 xe_migrate *m, struct kunit *test,


  big = xe_bo_create_pin_map(xe, tile, m->q->vm, SZ_4M, ttm_bo_type_kernel,

drivers/gpu/drm/xe/tests/xe_migrate.c +2 -1
-				   XE_BO_FLAG_VRAM_IF_DGFX(tile));
+				   XE_BO_FLAG_VRAM_IF_DGFX(tile),
+				   exec);

  if (IS_ERR(big)) {
  KUNIT_FAIL(test, "Failed to allocate bo: %li\n", PTR_ERR(big));
  goto vunmap;

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -212,7 +213,8 @@ static void xe_migrate_sanity_test(struct
 xe_migrate *m, struct kunit *test,


  pt = xe_bo_create_pin_map(xe, tile, m->q->vm, XE_PAGE_SIZE,
    ttm_bo_type_kernel,

drivers/gpu/drm/xe/tests/xe_migrate.c +2 -1
-				  XE_BO_FLAG_VRAM_IF_DGFX(tile));
+				  XE_BO_FLAG_VRAM_IF_DGFX(tile),
+				  exec);

  if (IS_ERR(pt)) {
  KUNIT_FAIL(test, "Failed to allocate fake pt:
%li\n",
     PTR_ERR(pt));

drivers/gpu/drm/xe/tests/xe_migrate.c +2 -1
@@ -222,7 +224,8 @@ static void xe_migrate_sanity_test(struct
 xe_migrate *m, struct kunit *test,
  	tiny = xe_bo_create_pin_map(xe, tile, m->q->vm,
  				    2 * SZ_4K,
  				    ttm_bo_type_kernel,
-				   
 XE_BO_FLAG_VRAM_IF_DGFX(tile));
+				    XE_BO_FLAG_VRAM_IF_DGFX(tile),
+				    exec);
  	if (IS_ERR(tiny)) {

  KUNIT_FAIL(test, "Failed to allocate tiny fake pt:
%li\n",
     PTR_ERR(tiny));

drivers/gpu/drm/xe/xe_bo.c +0 -0
index c9928d4ee5a0..82bf158426ad 100644
@@ -2343,16 +2343,60 @@ xe_bo_create_pin_map_at_novm(struct
 xe_device *xe, struct xe_tile *tile,
  	return ret ? ERR_PTR(ret) : bo;
  }


+/**
+ * xe_bo_create_pin_map() - Create pinned and mapped bo
+ * @xe: The xe device.
+ * @tile: The tile to select for migration of this bo, and the tile used for
+ * @vm: The vm to associate the buffer object with. The vm's resv must be locked
+ * with the transaction represented by @exec.
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @size: The storage size to use for the bo.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @exec: The drm_exec transaction to use for exhaustive eviction, and
+ * previously used for locking @vm's resv.
+ *
+ * Create a pinned and mapped bo. The bo will be external and not associated
+ * with a VM.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set
+ * to true on entry.
+ */
 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     struct xe_vm *vm, size_t size,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-				   enum ttm_bo_type type, u32

flags)

drivers/gpu/drm/xe/xe_bo.c +1 -0
+				   enum ttm_bo_type type, u32

flags,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+				   struct drm_exec *exec)

 {

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) :

XE_VALIDATION_UNIMPLEMENTED;

drivers/gpu/drm/xe/xe_bo.c +1 -1
-
+	xe_assert(xe, exec);

  return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size, ~0ull, type, flags,
         true, 0, exec); }

+/**
+ * xe_bo_create_pin_map_novm() - Create pinned and mapped bo
+ * @xe: The xe device.
+ * @tile: The tile to select for migration of this bo, and the tile used for
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @size: The storage size to use for the bo.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @intr: Whether to execut any waits for backing store interruptible.
+ *
+ * Create a pinned and mapped bo. The bo will be external and not associated
+ * with a VM.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set
+ * to true on entry.
+ */
+struct xe_bo *xe_bo_create_pin_map_novm(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+					size_t size, enum

ttm_bo_type type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +3 -0
+					bool intr)
+{
+	return xe_bo_create_pin_map_at_novm(xe, tile, size, ~0ull,

type, flags, true, 0, intr);

drivers/gpu/drm/xe/xe_bo.c +2 -0
+}
+

 static void __xe_bo_unpin_map_no_vm(void *arg)
 {
  xe_bo_unpin_map_no_vm(arg);

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2365,8 +2409,7 @@ struct xe_bo
 *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile
  	int ret;

KUNIT_STATIC_STUB_REDIRECT(xe_managed_bo_create_pin_map, xe, tile, size, flags);

drivers/gpu/drm/xe/xe_bo.c +0 -2
-
-	bo = xe_bo_create_pin_map(xe, tile, NULL, size,

ttm_bo_type_kernel, flags);

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	bo = xe_bo_create_pin_map_novm(xe, tile, size,

ttm_bo_type_kernel, flags, true);
  if (IS_ERR(bo))
  return bo;

drivers/gpu/drm/xe/xe_bo.h +0 -0
index d06266af9662..802e3c7d7872 100644
@@ -108,7 +108,11 @@ struct xe_bo *xe_bo_create_user(struct

xe_device *xe, struct xe_vm *vm, size_t s
  u16 cpu_caching, u32 flags, struct drm_exec *exec);
 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     struct xe_vm *vm, size_t size,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-				   enum ttm_bo_type type, u32

flags);

drivers/gpu/drm/xe/xe_bo.h +1 -0
+				   enum ttm_bo_type type, u32

flags,

drivers/gpu/drm/xe/xe_bo.h +2 -0
+				   struct drm_exec *exec);
+struct xe_bo *xe_bo_create_pin_map_novm(struct xe_device *xe,

struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.h +1 -0
+					size_t size, enum

ttm_bo_type type, u32 flags,

drivers/gpu/drm/xe/xe_bo.h +1 -0
+					bool intr);

 struct xe_bo *
 xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile *tile,
       size_t size, u64 offset, enum ttm_bo_type type,

drivers/gpu/drm/xe/xe_gsc.c +0 -0
index f5ae28af60d4..83d61bf8ec62 100644
@@ -136,10 +136,10 @@ static int query_compatibility_version(struct
 xe_gsc *gsc)
  	u64 ggtt_offset;
  	int err;

drivers/gpu/drm/xe/xe_gsc.c +0 -1
-	bo = xe_bo_create_pin_map(xe, tile, NULL, GSC_VER_PKT_SZ *

2,

drivers/gpu/drm/xe/xe_gsc.c +1 -3
-				  ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |
-				  XE_BO_FLAG_GGTT);
+	bo = xe_bo_create_pin_map_novm(xe, tile, GSC_VER_PKT_SZ *

2,

drivers/gpu/drm/xe/xe_gsc.c +3 -0
+				       ttm_bo_type_kernel,
+				       XE_BO_FLAG_SYSTEM |
+				       XE_BO_FLAG_GGTT, false);

  if (IS_ERR(bo)) {
  xe_gt_err(gt, "failed to allocate bo for GSC version query\n");
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +0 -0
index 906011671b60..d0a87d7b028b 100644
@@ -1452,7 +1452,6 @@ static bool pf_release_vf_config_lmem(struct

xe_gt *gt, struct xe_gt_sriov_confi
 static int pf_provision_vf_lmem(struct xe_gt *gt, unsigned int vfid, u64 size)
 {
  struct xe_gt_sriov_config *config = pf_pick_vf_config(gt, vfid);

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +0 -1
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;

  struct xe_device *xe = gt_to_xe(gt);
  struct xe_tile *tile = gt_to_tile(gt);
  struct xe_bo *bo;

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +0 -0
@@ -1479,24 +1478,17 @@ static int pf_provision_vf_lmem(struct
 xe_gt *gt, unsigned int vfid, u64 size)
  		return 0;


  xe_gt_assert(gt, pf_get_lmem_alignment(gt) == SZ_2M);

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +5 -8
-	bo = xe_bo_create_locked(xe, tile, NULL,
-				 ALIGN(size, PAGE_SIZE),
-				 ttm_bo_type_kernel,
-				 XE_BO_FLAG_VRAM_IF_DGFX(tile) |
-				 XE_BO_FLAG_NEEDS_2M |
-				 XE_BO_FLAG_PINNED |
-				 XE_BO_FLAG_PINNED_LATE_RESTORE,
-				 exec);
+	bo = xe_bo_create_pin_map_at_novm(xe, tile,
+					  ALIGN(size, PAGE_SIZE),
+					  0,
+					  ttm_bo_type_kernel,
+					 

XE_BO_FLAG_VRAM_IF_DGFX(tile) |

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +2 -0
+					  XE_BO_FLAG_NEEDS_2M |
+					 

XE_BO_FLAG_PINNED_LATE_RESTORE,

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +1 -0
+					  false, 0, false);

  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +0 -7
-	err = xe_bo_pin(bo, exec);
-	xe_bo_unlock(bo);
-	if (unlikely(err)) {
-		xe_bo_put(bo);
-		return err;
-	}
-

  config->lmem_obj = bo;

  if (xe_device_has_lmtt(xe)) {

drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c +0 -0
index c712111aa30d..44cc612b0a75 100644
@@ -55,12 +55,12 @@ static int pf_send_guc_save_vf_state(struct
 xe_gt *gt, unsigned int vfid,
  	xe_gt_assert(gt, size % sizeof(u32) == 0);
  	xe_gt_assert(gt, size == ndwords * sizeof(u32));

drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c +6 -6
-	bo = xe_bo_create_pin_map(xe, tile, NULL,
-				  ALIGN(size, PAGE_SIZE),
-				  ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |
-				  XE_BO_FLAG_GGTT |
-				  XE_BO_FLAG_GGTT_INVALIDATE);
+	bo = xe_bo_create_pin_map_novm(xe, tile,
+				       ALIGN(size, PAGE_SIZE),
+				       ttm_bo_type_kernel,
+				       XE_BO_FLAG_SYSTEM |
+				       XE_BO_FLAG_GGTT |
+				       XE_BO_FLAG_GGTT_INVALIDATE,

false);
  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c +0 -0
@@ -91,12 +91,12 @@ static int pf_send_guc_restore_vf_state(struct
 xe_gt *gt, unsigned int vfid,
  	xe_gt_assert(gt, size % sizeof(u32) == 0);
  	xe_gt_assert(gt, size == ndwords * sizeof(u32));

drivers/gpu/drm/xe/xe_gt_sriov_pf_migration.c +6 -6
-	bo = xe_bo_create_pin_map(xe, tile, NULL,
-				  ALIGN(size, PAGE_SIZE),
-				  ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |
-				  XE_BO_FLAG_GGTT |
-				  XE_BO_FLAG_GGTT_INVALIDATE);
+	bo = xe_bo_create_pin_map_novm(xe, tile,
+				       ALIGN(size, PAGE_SIZE),
+				       ttm_bo_type_kernel,
+				       XE_BO_FLAG_SYSTEM |
+				       XE_BO_FLAG_GGTT |
+				       XE_BO_FLAG_GGTT_INVALIDATE,

false);
  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -0
index 92e1f9f41b8c..2b99c1ebdd58 100644
@@ -94,16 +94,17 @@ static int
 allocate_engine_activity_buffers(struct xe_guc *guc,
  	struct xe_tile *tile = gt_to_tile(gt);
  	struct xe_bo *bo, *metadata_bo;

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-	metadata_bo = xe_bo_create_pin_map(gt_to_xe(gt), tile,

NULL, PAGE_ALIGN(metadata_size),

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-					   ttm_bo_type_kernel,

XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-					   XE_BO_FLAG_GGTT |

XE_BO_FLAG_GGTT_INVALIDATE);

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+	metadata_bo = xe_bo_create_pin_map_novm(gt_to_xe(gt),

tile, PAGE_ALIGN(metadata_size),

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+						ttm_bo_type_kernel

, XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+						XE_BO_FLAG_GGTT |

XE_BO_FLAG_GGTT_INVALIDATE,

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+						false);


  if (IS_ERR(metadata_bo))
  return PTR_ERR(metadata_bo);

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-	bo = xe_bo_create_pin_map(gt_to_xe(gt), tile, NULL,

PAGE_ALIGN(size),

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-				  ttm_bo_type_kernel,

XE_BO_FLAG_VRAM_IF_DGFX(tile) |

drivers/gpu/drm/xe/xe_guc_engine_activity.c +0 -1
-				  XE_BO_FLAG_GGTT |

XE_BO_FLAG_GGTT_INVALIDATE);

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+	bo = xe_bo_create_pin_map_novm(gt_to_xe(gt), tile,

PAGE_ALIGN(size),

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+				       ttm_bo_type_kernel,

XE_BO_FLAG_VRAM_IF_DGFX(tile) |

drivers/gpu/drm/xe/xe_guc_engine_activity.c +1 -0
+				       XE_BO_FLAG_GGTT |

XE_BO_FLAG_GGTT_INVALIDATE, false);

  if (IS_ERR(bo)) { xe_bo_unpin_map_no_vm(metadata_bo);

drivers/gpu/drm/xe/xe_lmtt.c +0 -0
index a78c9d474a6e..4ad468574174 100644
@@ -67,12 +67,12 @@ static struct xe_lmtt_pt *lmtt_pt_alloc(struct

xe_lmtt *lmtt, unsigned int level
  goto out;
  }

drivers/gpu/drm/xe/xe_lmtt.c +0 -1
-	bo = xe_bo_create_pin_map(lmtt_to_xe(lmtt),

lmtt_to_tile(lmtt), NULL,

drivers/gpu/drm/xe/xe_lmtt.c +0 -1
-				  PAGE_ALIGN(lmtt->ops-
Quoted 1 line

lmtt_pte_size(level) *

drivers/gpu/drm/xe/xe_lmtt.c +0 -1
-					     lmtt->ops-
Quoted 1 line

lmtt_pte_num(level)),

drivers/gpu/drm/xe/xe_lmtt.c +0 -2
-				  ttm_bo_type_kernel,
-				 

XE_BO_FLAG_VRAM_IF_DGFX(lmtt_to_tile(lmtt)) |

drivers/gpu/drm/xe/xe_lmtt.c +1 -1
-				  XE_BO_FLAG_NEEDS_64K);
+	bo = xe_bo_create_pin_map_novm(lmtt_to_xe(lmtt),

lmtt_to_tile(lmtt),

drivers/gpu/drm/xe/xe_lmtt.c +1 -0
+				       PAGE_ALIGN(lmtt->ops-
Quoted 1 line

lmtt_pte_size(level) *

drivers/gpu/drm/xe/xe_lmtt.c +1 -0
+						  lmtt->ops-
Quoted 1 line

lmtt_pte_num(level)),

drivers/gpu/drm/xe/xe_lmtt.c +2 -0
+				       ttm_bo_type_kernel,
+				      

XE_BO_FLAG_VRAM_IF_DGFX(lmtt_to_tile(lmtt)) |

drivers/gpu/drm/xe/xe_lmtt.c +1 -0
+				       XE_BO_FLAG_NEEDS_64K,

false);
  if (IS_ERR(bo)) {
  err = PTR_ERR(bo);
  goto out_free_pt;

drivers/gpu/drm/xe/xe_lrc.c +0 -0
index 8f6c3ba47882..6d52e0eb97f5 100644
@@ -1340,9 +1340,10 @@ static int xe_lrc_init(struct xe_lrc *lrc,
 struct xe_hw_engine *hwe,

  if (vm && vm->xef) /* userspace */ bo_flags |= XE_BO_FLAG_PINNED_LATE_RESTORE;

drivers/gpu/drm/xe/xe_lrc.c +4 -3
-	lrc->bo = xe_bo_create_pin_map(xe, tile, NULL, bo_size,
-				       ttm_bo_type_kernel,
-				       bo_flags);
+	lrc->bo = xe_bo_create_pin_map_novm(xe, tile,
+					    bo_size,
+					    ttm_bo_type_kernel,
+					    bo_flags, false);

  if (IS_ERR(lrc->bo))
  return PTR_ERR(lrc->bo);

drivers/gpu/drm/xe/xe_migrate.c +1 -0
index ddfad7506a82..fe0d15ab340e 100644
@@ -35,6 +35,7 @@
  #include "xe_sched_job.h"
  #include "xe_sync.h"
  #include "xe_trace_bo.h"
+#include "xe_validation.h"
  #include "xe_vm.h"
  #include "xe_vram.h"

drivers/gpu/drm/xe/xe_migrate.c +0 -0
@@ -173,7 +174,7 @@ static void xe_migrate_program_identity(struct
 xe_device *xe, struct xe_vm *vm,
  }


 static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,

drivers/gpu/drm/xe/xe_migrate.c +1 -1
-				 struct xe_vm *vm)
+				 struct xe_vm *vm, struct drm_exec

*exec)
 {
  struct xe_device *xe = tile_to_xe(tile);
  u16 pat_index = xe->pat.idx[XE_CACHE_WB];

drivers/gpu/drm/xe/xe_migrate.c +0 -0
@@ -200,7 +201,7 @@ static int xe_migrate_prepare_vm(struct xe_tile
 *tile, struct xe_migrate *m,
  				  num_entries * XE_PAGE_SIZE,
  				  ttm_bo_type_kernel,

    XE_BO_FLAG_VRAM_IF_DGFX(tile) |

drivers/gpu/drm/xe/xe_migrate.c +1 -1
-				  XE_BO_FLAG_PAGETABLE);
+				  XE_BO_FLAG_PAGETABLE, exec);

  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_migrate.c +2 -0
@@ -404,6 +405,8 @@ int xe_migrate_init(struct xe_migrate *m)
  	struct xe_tile *tile = m->tile;
  	struct xe_gt *primary_gt = tile->primary_gt;
  	struct xe_device *xe = tile_to_xe(tile);
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
  	struct xe_vm *vm;
  	int err;

drivers/gpu/drm/xe/xe_migrate.c +0 -0
@@ -413,11 +416,16 @@ int xe_migrate_init(struct xe_migrate *m)
  	if (IS_ERR(vm))
  		return PTR_ERR(vm);

drivers/gpu/drm/xe/xe_migrate.c +2 -3
-	xe_vm_lock(vm, false);
-	err = xe_migrate_prepare_vm(tile, m, vm);
-	xe_vm_unlock(vm);
+	err = 0;
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, err, false)

{

drivers/gpu/drm/xe/xe_migrate.c +6 -0
+		err = xe_vm_drm_exec_lock(vm, &exec);
+		drm_exec_retry_on_contention(&exec);
+		err = xe_migrate_prepare_vm(tile, m, vm, &exec);
+		drm_exec_retry_on_contention(&exec);
+		xe_validation_retry_on_oom(&ctx, &err);
+	}

  if (err)

drivers/gpu/drm/xe/xe_migrate.c +1 -1
-		goto err_out;
+		return err;


  if (xe->info.has_usm) {
  struct xe_hw_engine *hwe =
xe_gt_hw_engine(primary_gt,

drivers/gpu/drm/xe/xe_oa.c +0 -0
index a188bad172ad..a4894eb0d7f3 100644
@@ -883,9 +883,9 @@ static int xe_oa_alloc_oa_buffer(struct
 xe_oa_stream *stream, size_t size)
  {
  	struct xe_bo *bo;

drivers/gpu/drm/xe/xe_oa.c +0 -1
-	bo = xe_bo_create_pin_map(stream->oa->xe, stream->gt-
Quoted 1 line

tile, NULL,

drivers/gpu/drm/xe/xe_oa.c +0 -2
-				  size, ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_GGTT);

drivers/gpu/drm/xe/xe_oa.c +1 -0
+	bo = xe_bo_create_pin_map_novm(stream->oa->xe, stream->gt-
Quoted 1 line

tile,

drivers/gpu/drm/xe/xe_oa.c +2 -0
+				       size, ttm_bo_type_kernel,
+				       XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_GGTT, false);
  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_pt.c +1 -0
index f3a39e734a90..33ad40418ceb 100644
@@ -88,6 +88,7 @@ static void xe_pt_free(struct xe_pt *pt)
   * @vm: The vm to create for.
   * @tile: The tile to create for.
   * @level: The page-table level.
+ * @exec: The drm_exec object used to lock the vm.
   *
   * Allocate and initialize a single struct xe_pt metadata

structure. Also
  * create the corresponding page-table bo, but don't initialize it. If the

drivers/gpu/drm/xe/xe_pt.c +1 -1
@@ -99,7 +100,7 @@ static void xe_pt_free(struct xe_pt *pt)
   * error.
   */
  struct xe_pt *xe_pt_create(struct xe_vm *vm, struct xe_tile *tile,
-			   unsigned int level)
+			   unsigned int level, struct drm_exec
 *exec)
  {
  	struct xe_pt *pt;

  struct xe_bo *bo;

drivers/gpu/drm/xe/xe_pt.c +0 -0
@@ -123,9 +124,11 @@ struct xe_pt *xe_pt_create(struct xe_vm *vm,
 struct xe_tile *tile,
  		bo_flags |= XE_BO_FLAG_PINNED_LATE_RESTORE;


  pt->level = level;

drivers/gpu/drm/xe/xe_pt.c +2 -0
+
+	drm_WARN_ON(&vm->xe->drm, IS_ERR_OR_NULL(exec));

  bo = xe_bo_create_pin_map(vm->xe, tile, vm, SZ_4K, ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_pt.c +1 -1
-				  bo_flags);
+				  bo_flags, exec);

  if (IS_ERR(bo)) {
  err = PTR_ERR(bo);
  goto err_kfree;

drivers/gpu/drm/xe/xe_pt.c +0 -0
@@ -589,7 +592,8 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent,
 pgoff_t offset,
  	if (covers || !*child) {
  		u64 flags = 0;

drivers/gpu/drm/xe/xe_pt.c +0 -1
-		xe_child = xe_pt_create(xe_walk->vm, xe_walk-
Quoted 1 line

tile, level - 1);

drivers/gpu/drm/xe/xe_pt.c +1 -0
+		xe_child = xe_pt_create(xe_walk->vm, xe_walk-
Quoted 1 line

tile, level - 1,

drivers/gpu/drm/xe/xe_pt.c +1 -0
+					xe_vm_validation_exec(vm))

;
  if (IS_ERR(xe_child))
  return PTR_ERR(xe_child);

drivers/gpu/drm/xe/xe_pt.h +0 -0
index 5ecf003d513c..4daeebaab5a1 100644
@@ -10,6 +10,7 @@
  #include "xe_pt_types.h"


 struct dma_fence;
+struct drm_exec;
 struct xe_bo;
 struct xe_device;
 struct xe_exec_queue;

drivers/gpu/drm/xe/xe_pt.h +0 -0
@@ -29,7 +30,7 @@ struct xe_vma_ops;
  unsigned int xe_pt_shift(unsigned int level);


 struct xe_pt *xe_pt_create(struct xe_vm *vm, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_pt.h +1 -1
-			   unsigned int level);
+			   unsigned int level, struct drm_exec

*exec);

 void xe_pt_populate_empty(struct xe_tile *tile, struct xe_vm *vm, struct xe_pt *pt);

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -0
index ca95f2a4d4ef..54bd6b64dc6d 100644
@@ -54,8 +54,9 @@ static int
 allocate_vcs_execution_resources(struct xe_pxp *pxp)
  	 * Each termination is 16 DWORDS, so 4K is enough to

contain a
  * termination for each sessions.
  */

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -1
-	bo = xe_bo_create_pin_map(xe, tile, NULL, SZ_4K,

ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -1
-				  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_PINNED | XE_BO_FLAG_GGTT);

drivers/gpu/drm/xe/xe_pxp_submit.c +1 -0
+	bo = xe_bo_create_pin_map_novm(xe, tile, SZ_4K,

ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_pxp_submit.c +1 -0
+				       XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_PINNED | XE_BO_FLAG_GGTT,

drivers/gpu/drm/xe/xe_pxp_submit.c +1 -0
+				       false);

  if (IS_ERR(bo)) {
  err = PTR_ERR(bo);
  goto out_queue;

drivers/gpu/drm/xe/xe_pxp_submit.c +2 -0
@@ -87,7 +88,9 @@ static int allocate_gsc_client_resources(struct
 xe_gt *gt,
  {
  	struct xe_tile *tile = gt_to_tile(gt);
  	struct xe_device *xe = tile_to_xe(tile);
+	struct xe_validation_ctx ctx;
  	struct xe_hw_engine *hwe;
+	struct drm_exec exec;
  	struct xe_vm *vm;
  	struct xe_bo *bo;

  struct xe_exec_queue *q;

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -0
@@ -106,15 +109,26 @@ static int
 allocate_gsc_client_resources(struct xe_gt *gt,
  		return PTR_ERR(vm);


  /* We allocate a single object for the batch and the in/out memory */

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -2
-	xe_vm_lock(vm, false);
-	bo = xe_bo_create_pin_map(xe, tile, vm, PXP_BB_SIZE +

inout_size * 2,

drivers/gpu/drm/xe/xe_pxp_submit.c +0 -2
-				  ttm_bo_type_kernel,
-				  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_PINNED | XE_BO_FLAG_NEEDS_UC);

drivers/gpu/drm/xe/xe_pxp_submit.c +2 -4
-	xe_vm_unlock(vm);
-	if (IS_ERR(bo)) {
-		err = PTR_ERR(bo);
-		goto vm_out;
+
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, err, false)

{

drivers/gpu/drm/xe/xe_pxp_submit.c +6 -0
+		err = xe_vm_drm_exec_lock(vm, &exec);
+		drm_exec_retry_on_contention(&exec);
+		if (err)
+			break;
+
+		bo = xe_bo_create_pin_map(xe, tile, vm,

PXP_BB_SIZE + inout_size * 2,

drivers/gpu/drm/xe/xe_pxp_submit.c +2 -0
+					  ttm_bo_type_kernel,
+					  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_PINNED |

drivers/gpu/drm/xe/xe_pxp_submit.c +1 -0
+					  XE_BO_FLAG_NEEDS_UC,

&exec);

drivers/gpu/drm/xe/xe_pxp_submit.c +6 -0
+		drm_exec_retry_on_contention(&exec);
+		if (IS_ERR(bo)) {
+			err = PTR_ERR(bo);
+			xe_validation_retry_on_oom(&ctx, &err);
+			break;
+		}

  }

drivers/gpu/drm/xe/xe_pxp_submit.c +2 -0
+	if (err)
+		goto vm_out;


  fence = xe_vm_bind_kernel_bo(vm, bo, NULL, 0, XE_CACHE_WB);
  if (IS_ERR(fence)) {

drivers/gpu/drm/xe/xe_vm.c +1 -0
index 989d84c2e82f..b3ee65126841 100644
@@ -1606,6 +1606,7 @@ static void vm_destroy_work_func(struct
 work_struct *w);
   * @xe: xe device.
   * @tile: tile to set up for.
   * @vm: vm to set up for.
+ * @exec: The struct drm_exec object used to lock the vm resv.
   *
   * Sets up a pagetable tree with one page-table per level and a

single
  * leaf PTE. All pagetable entries point to the single page-table or,

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -1615,20 +1616,19 @@ static void vm_destroy_work_func(struct
 work_struct *w);
   * Return: 0 on success, negative error code on error.
   */

 static int xe_vm_create_scratch(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_vm.c +1 -1
-				struct xe_vm *vm)
+				struct xe_vm *vm, struct drm_exec

*exec)
 {
  u8 id = tile->id;
  int i;

  for (i = MAX_HUGEPTE_LEVEL; i < vm->pt_root[id]->level; i++) {

drivers/gpu/drm/xe/xe_vm.c +1 -1
-		vm->scratch_pt[id][i] = xe_pt_create(vm, tile, i);
+		vm->scratch_pt[id][i] = xe_pt_create(vm, tile, i,

exec);
  if (IS_ERR(vm->scratch_pt[id][i])) { int err = PTR_ERR(vm->scratch_pt[id][i]);
  vm->scratch_pt[id][i] = NULL; return err;
  }
-
  xe_pt_populate_empty(tile, vm, vm-

Quoted 1 line

scratch_pt[id][i]);

  }

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -1656,9 +1656,26 @@ static void xe_vm_free_scratch(struct xe_vm
 *vm)
  	}
  }

drivers/gpu/drm/xe/xe_vm.c +10 -0
+static void xe_vm_pt_destroy(struct xe_vm *vm)
+{
+	struct xe_tile *tile;
+	u8 id;
+
+	xe_vm_assert_held(vm);
+
+	for_each_tile(tile, vm->xe, id) {
+		if (vm->pt_root[id]) {
+			xe_pt_destroy(vm->pt_root[id], vm->flags,

NULL);

drivers/gpu/drm/xe/xe_vm.c +5 -0
+			vm->pt_root[id] = NULL;
+		}
+	}
+}
+

 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags, struct xe_file *xef)
 {
  struct drm_gem_object *vm_resv_obj;

drivers/gpu/drm/xe/xe_vm.c +2 -0
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;

  struct xe_vm *vm;
  int err, number_tiles = 0;
  struct xe_tile *tile;

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -1745,49 +1762,64 @@ struct xe_vm *xe_vm_create(struct xe_device
 *xe, u32 flags, struct xe_file *xef)


  drm_gem_object_put(vm_resv_obj);

drivers/gpu/drm/xe/xe_vm.c +2 -3
-	err = xe_vm_lock(vm, true);
-	if (err)
-		goto err_close;
+	err = 0;
+	xe_validation_guard(&ctx, &xe->val, &exec,

DRM_EXEC_INTERRUPTIBLE_WAIT,

drivers/gpu/drm/xe/xe_vm.c +3 -0
+			    err, true) {
+		err = xe_vm_drm_exec_lock(vm, &exec);
+		drm_exec_retry_on_contention(&exec);

drivers/gpu/drm/xe/xe_vm.c +0 -1
-	if (IS_DGFX(xe) && xe->info.vram_flags &

XE_VRAM_FLAGS_NEED64K)

drivers/gpu/drm/xe/xe_vm.c +1 -1
-		vm->flags |= XE_VM_FLAG_64K;
+		if (IS_DGFX(xe) && xe->info.vram_flags &

XE_VRAM_FLAGS_NEED64K)

drivers/gpu/drm/xe/xe_vm.c +1 -0
+			vm->flags |= XE_VM_FLAG_64K;

drivers/gpu/drm/xe/xe_vm.c +4 -4
-	for_each_tile(tile, xe, id) {
-		if (flags & XE_VM_FLAG_MIGRATION &&
-		    tile->id != XE_VM_FLAG_TILE_ID(flags))
-			continue;
+		for_each_tile(tile, xe, id) {
+			if (flags & XE_VM_FLAG_MIGRATION &&
+			    tile->id != XE_VM_FLAG_TILE_ID(flags))
+				continue;

drivers/gpu/drm/xe/xe_vm.c +0 -1
-		vm->pt_root[id] = xe_pt_create(vm, tile, xe-
Quoted 1 line

info.vm_max_level);

drivers/gpu/drm/xe/xe_vm.c +1 -4
-		if (IS_ERR(vm->pt_root[id])) {
-			err = PTR_ERR(vm->pt_root[id]);
-			vm->pt_root[id] = NULL;
-			goto err_unlock_close;
+			vm->pt_root[id] = xe_pt_create(vm, tile,

xe->info.vm_max_level,

drivers/gpu/drm/xe/xe_vm.c +6 -0
+						       &exec);
+			if (IS_ERR(vm->pt_root[id])) {
+				err = PTR_ERR(vm->pt_root[id]);
+				vm->pt_root[id] = NULL;
+				xe_vm_pt_destroy(vm);
+				drm_exec_retry_on_contention(&exec

);

drivers/gpu/drm/xe/xe_vm.c +1 -0
+				xe_validation_retry_on_oom(&ctx,

&err);

drivers/gpu/drm/xe/xe_vm.c +2 -0
+				goto err_close;
+			}

  }

drivers/gpu/drm/xe/xe_vm.c +0 -1
-	}

drivers/gpu/drm/xe/xe_vm.c +6 -1
-	if (xe_vm_has_scratch(vm)) {
+		if (xe_vm_has_scratch(vm)) {
+			for_each_tile(tile, xe, id) {
+				if (!vm->pt_root[id])
+					continue;
+
+				err = xe_vm_create_scratch(xe,

tile, vm, &exec);

drivers/gpu/drm/xe/xe_vm.c +4 -0
+				if (err) {
+					xe_vm_free_scratch(vm);
+					xe_vm_pt_destroy(vm);
+					drm_exec_retry_on_contenti

on(&exec);

drivers/gpu/drm/xe/xe_vm.c +1 -0
+					xe_validation_retry_on_oom

(&ctx, &err);

drivers/gpu/drm/xe/xe_vm.c +8 -0
+					goto err_close;
+				}
+			}
+			vm->batch_invalidate_tlb = true;
+		}
+
+		if (vm->flags & XE_VM_FLAG_LR_MODE) {
+			INIT_WORK(&vm->preempt.rebind_work,

preempt_rebind_work_func);

drivers/gpu/drm/xe/xe_vm.c +4 -0
+			vm->batch_invalidate_tlb = false;
+		}
+
+		/* Fill pt_root after allocating scratch tables */

  for_each_tile(tile, xe, id) {
  if (!vm->pt_root[id])
  continue;

drivers/gpu/drm/xe/xe_vm.c +1 -3
-			err = xe_vm_create_scratch(xe, tile, vm);
-			if (err)
-				goto err_unlock_close;
+			xe_pt_populate_empty(tile, vm, vm-
Quoted 1 line

pt_root[id]);

  }

drivers/gpu/drm/xe/xe_vm.c +0 -12
-		vm->batch_invalidate_tlb = true;
-	}
-
-	if (vm->flags & XE_VM_FLAG_LR_MODE)
-		vm->batch_invalidate_tlb = false;
-
-	/* Fill pt_root after allocating scratch tables */
-	for_each_tile(tile, xe, id) {
-		if (!vm->pt_root[id])
-			continue;
-
-		xe_pt_populate_empty(tile, vm, vm->pt_root[id]);

  }

drivers/gpu/drm/xe/xe_vm.c +2 -1
-	xe_vm_unlock(vm);
+	if (err)
+		goto err_close;


  /* Kernel migration VM shouldn't have a circular loop.. */ if (!(flags & XE_VM_FLAG_MIGRATION)) {

drivers/gpu/drm/xe/xe_vm.c +1 -1
@@ -1820,7 +1852,7 @@ struct xe_vm *xe_vm_create(struct xe_device
 *xe, u32 flags, struct xe_file *xef)
  				      &xe->usm.next_asid,
 GFP_KERNEL);
  		up_write(&xe->usm.lock);
  		if (err < 0)
-			goto err_unlock_close;
+			goto err_close;


  vm->usm.asid = asid;
  }

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -1829,8 +1861,6 @@ struct xe_vm *xe_vm_create(struct xe_device
 *xe, u32 flags, struct xe_file *xef)


  return vm;

drivers/gpu/drm/xe/xe_vm.c +0 -2
-err_unlock_close:
-	xe_vm_unlock(vm);

 err_close:
  xe_vm_close_and_put(vm);
  return ERR_PTR(err);

drivers/gpu/drm/xe/xe_vm.c +1 -7
@@ -1959,13 +1989,7 @@ void xe_vm_close_and_put(struct xe_vm *vm)
  	 * destroy the pagetables immediately.
  	 */
  	xe_vm_free_scratch(vm);
-
-	for_each_tile(tile, xe, id) {
-		if (vm->pt_root[id]) {
-			xe_pt_destroy(vm->pt_root[id], vm->flags,
 NULL);
-			vm->pt_root[id] = NULL;
-		}
-	}
+	xe_vm_pt_destroy(vm);
  	xe_vm_unlock(vm);


  /*

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -3845,7 +3869,6 @@ struct dma_fence *xe_vm_bind_kernel_bo(struct
 xe_vm *vm, struct xe_bo *bo,
   */
  int xe_vm_lock(struct xe_vm *vm, bool intr)
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_bind_kernel_bo(struct · line 3849

Quoted 2 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
  {
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;

You add this earlier in the series and then delete it here, which seems
odd. If the intent is to apply a workaround (WA) earlier and finalize it
here, that makes sense.

Quoted 9 lines

Yes, the patch that introduces these identifies all the call-sites where we need a fix-up, and uses lockdep to verify that a validation transaction starting there doesn't violate the dma_resv locking orders.

Most of the series then eliminates these _UNIMPLEMENTED one by one. Of course one could do it in the opposite order to avoid introducing and then eliminating these but I think that would be harder both for the developer and reviewer.

Fine with the ordering, just wanted to make sure I was understanding why this was done.

Quoted 11 lines
Quoted 8 lines

However, deleting it here appears to leave a problematic path in xe_svm.c around xe_vm_range_rebind, where vm->validating._exec may remain unset/stale. I’m surprised CI didn’t catch this—perhaps due to an
unbalanced xe_vm_set_validation_exec leaving stale state—or I’m missing
something.

Yes, you are correct. There was an imbalance and I will fix that.

+1

Matt

Quoted 31 lines

Thanks,
Thomas

Quoted 27 lines

Matt

Quoted 24 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
  	int ret;


  if (intr)

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -3853,9 +3876,6 @@ int xe_vm_lock(struct xe_vm *vm, bool intr)

  else
  ret = dma_resv_lock(xe_vm_resv(vm), NULL);

drivers/gpu/drm/xe/xe_vm.c +0 -3
-	if (!ret)
-		xe_vm_set_validation_exec(vm, exec);
-

  return ret;
 }

drivers/gpu/drm/xe/xe_vm.c +0 -1
@@ -3867,7 +3887,6 @@ int xe_vm_lock(struct xe_vm *vm, bool intr)
   */
  void xe_vm_unlock(struct xe_vm *vm)
  {
-	xe_vm_set_validation_exec(vm, NULL);
  	dma_resv_unlock(xe_vm_resv(vm));
  }

Signature · 1 line
2.50.1
maintainer
#51 lore raw

Re: [PATCH 13/15] drm/xe: Convert xe_bo_create_pin_map_at() for exhaustive eviction

In reply to Thomas Hellström
Quoted 162 lines · Thomas Hellström

Most users of xe_bo_create_pin_map_at() and xe_bo_create_pin_map_at_aligned() are not using the vm parameter, and that simplifies conversion. Introduce an xe_bo_create_pin_map_at_novm() function and make the _aligned() version static. Use xe_validation_guard() for conversion.

---
 .../compat-i915-headers/gem/i915_gem_stolen.h | 24 ++----
 drivers/gpu/drm/xe/display/xe_fb_pin.c        | 45 +++++-----
 drivers/gpu/drm/xe/display/xe_plane_initial.c |  4 +-
 drivers/gpu/drm/xe/xe_bo.c                    | 83 ++++++++++++++-----
 drivers/gpu/drm/xe/xe_bo.h                    | 13 +--
 drivers/gpu/drm/xe/xe_eu_stall.c              |  6 +-
 6 files changed, 101 insertions(+), 74 deletions(-)
drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +6 -18
index 1ce1e9da975b..ab48635ddffa 100644
@@ -21,9 +21,7 @@ static inline int i915_gem_stolen_insert_node_in_range(struct xe_device *xe,
 						       u32 size, u32 align,
 						       u32 start, u32 end)
 {
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
 	struct xe_bo *bo;
-	int err;
 	u32 flags = XE_BO_FLAG_PINNED | XE_BO_FLAG_STOLEN;
 
 	if (start < SZ_4K)
@@ -34,25 +32,15 @@ static inline int i915_gem_stolen_insert_node_in_range(struct xe_device *xe,
 		start = ALIGN(start, align);
 	}
 
-	bo = xe_bo_create_locked_range(xe, xe_device_get_root_tile(xe),
-				       NULL, size, start, end,
-				       ttm_bo_type_kernel, flags, 0, exec);
-	if (IS_ERR(bo)) {
-		err = PTR_ERR(bo);
-		bo = NULL;
-		return err;
-	}
-	err = xe_bo_pin(bo, exec);
-	xe_bo_unlock_vm_held(bo);
-
-	if (err) {
-		xe_bo_put(fb->bo);
-		bo = NULL;
-	}
+	bo = xe_bo_create_pin_map_at_novm(xe, xe_device_get_root_tile(xe),
+					  size, start, ttm_bo_type_kernel, flags,
+					  false, 0, true);
+	if (IS_ERR(bo))
+		return PTR_ERR(bo);
 
 	fb->bo = bo;
 
-	return err;
+	return 0;
 }
 
 static inline int i915_gem_stolen_insert_node(struct xe_device *xe,
drivers/gpu/drm/xe/display/xe_fb_pin.c +24 -21
index 43c45344ea26..d46ff7ebb0a1 100644
@@ -102,29 +102,32 @@ static int __xe_pin_fb_vma_dpt(const struct intel_framebuffer *fb,
 				 XE_PAGE_SIZE);
 
 	if (IS_DGFX(xe))
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0, NULL,
-						      dpt_size, ~0ull,
-						      ttm_bo_type_kernel,
-						      XE_BO_FLAG_VRAM0 |
-						      XE_BO_FLAG_GGTT |
-						      XE_BO_FLAG_PAGETABLE,
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size, ~0ull,
+						   ttm_bo_type_kernel,
+						   true,
+						   XE_BO_FLAG_VRAM0 |
+						   XE_BO_FLAG_GGTT |
+						   XE_BO_FLAG_PAGETABLE,
+						   alignment, false);
 	else
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0, NULL,
-						      dpt_size,  ~0ull,
-						      ttm_bo_type_kernel,
-						      XE_BO_FLAG_STOLEN |
-						      XE_BO_FLAG_GGTT |
-						      XE_BO_FLAG_PAGETABLE,
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size,  ~0ull,
+						   ttm_bo_type_kernel,
+						   true,
+						   XE_BO_FLAG_STOLEN |
+						   XE_BO_FLAG_GGTT |
+						   XE_BO_FLAG_PAGETABLE,
+						   alignment, false);
 	if (IS_ERR(dpt))
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0, NULL,
-						      dpt_size,  ~0ull,
-						      ttm_bo_type_kernel,
-						      XE_BO_FLAG_SYSTEM |
-						      XE_BO_FLAG_GGTT |
-						      XE_BO_FLAG_PAGETABLE,
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size,  ~0ull,
+						   ttm_bo_type_kernel,
+						   true,
+						   XE_BO_FLAG_SYSTEM |
+						   XE_BO_FLAG_GGTT |
+						   XE_BO_FLAG_PAGETABLE,
+						   alignment, false);
 	if (IS_ERR(dpt))
 		return PTR_ERR(dpt);
 
drivers/gpu/drm/xe/display/xe_plane_initial.c +2 -2
index 826ac3d578b7..79d00127caf4 100644
@@ -140,8 +140,8 @@ initial_plane_bo(struct xe_device *xe,
 			page_size);
 	size -= base;
 
-	bo = xe_bo_create_pin_map_at(xe, tile0, NULL, size, phys_base,
-				     ttm_bo_type_kernel, flags);
+	bo = xe_bo_create_pin_map_at_novm(xe, tile0, size, phys_base,
+					  ttm_bo_type_kernel, flags, true, 0, false);
 	if (IS_ERR(bo)) {
 		drm_dbg(&xe->drm,
 			"Failed to create bo phys_base=%pa size %u with flags %x: %li\n",
drivers/gpu/drm/xe/xe_bo.c +4 -15
index 23b28eeef59f..c9928d4ee5a0 100644
@@ -2253,29 +2253,20 @@ struct xe_bo *xe_bo_create_user(struct xe_device *xe,
 	return bo;
 }
 
-struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,
-				      struct xe_vm *vm,
-				      size_t size, u64 offset,
-				      enum ttm_bo_type type, u32 flags)
-{
-	return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size, offset,
-					       type, flags, 0);
-}
-
-struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
-					      struct xe_tile *tile,
-					      struct xe_vm *vm,
-					      size_t size, u64 offset,
-					      enum ttm_bo_type type, u32 flags,
-					      u64 alignment)
+static struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
+						     struct xe_tile *tile,
+						     struct xe_vm *vm,
+						     size_t size, u64 offset,

drivers/gpu/drm/xe/xe_bo.c struct xe_bo *xe_bo_create_user(struct xe_device *xe, · line 2268

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +3 -1
+						     enum ttm_bo_type type, u32 flags,
+						     bool vmap, u64 alignment,
+						     struct drm_exec *exec)
 {
 	struct xe_bo *bo;
 	int err;
 	u64 start = offset == ~0ull ? 0 : offset;
 	u64 end = offset == ~0ull ? offset : start + size;
-	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) : XE_VALIDATION_UNIMPLEMENTED;
 

General comment for the series: should all BO-layer functions that allocate (or may allocate) memory include a lockdep assertion that the xe_validation_device->val lock is held? We already have xe_validation_assert_exec in several places, which is similar, but IMO it wouldn’t hurt to also assert xe_validation_device->val in the relevant driver paths. The new TTM manager functions are good candidates as well. Consider adding a follow-up patch at the end of the series to add these assertions once all allocation paths adhere to the new locking model.

Matt

Quoted 125 lines
drivers/gpu/drm/xe/xe_bo.c +55 -5
-	if (flags & XE_BO_FLAG_STOLEN &&
+	if (flags & XE_BO_FLAG_STOLEN && vmap &&
 	    xe_ttm_stolen_cpu_access_needs_ggtt(xe))
 		flags |= XE_BO_FLAG_GGTT;
 
@@ -2289,9 +2280,11 @@ struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
 	if (err)
 		goto err_put;
 
-	err = xe_bo_vmap(bo);
-	if (err)
-		goto err_unpin;
+	if (vmap) {
+		err = xe_bo_vmap(bo);
+		if (err)
+			goto err_unpin;
+	}
 
 	xe_bo_unlock_vm_held(bo);
 
@@ -2305,11 +2298,59 @@ struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
 	return ERR_PTR(err);
 }
 
+/**
+ * xe_bo_create_pin_map_at_novm() - Create pinned and mapped bo at optional VRAM offset
+ * @xe: The xe device.
+ * @tile: The tile to select for migration of this bo, and the tile used for
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @size: The storage size to use for the bo.
+ * @offset: Optional VRAM offset or %0 for don't care.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @vmap: Whether to create a buffer object map.
+ * @alignment: GGTT alignment.
+ * @intr: Whether to execut any waits for backing store interruptible.
+ *
+ * Create a pinned and optionally mapped bo with VRAM offset and GGTT alignment
+ * options. The bo will be external and not associated with a VM.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set
+ * to true on entry.
+ */
+struct xe_bo *
+xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile *tile,
+			     size_t size, u64 offset, enum ttm_bo_type type, u32 flags,
+			     bool vmap, u64 alignment, bool intr)
+{
+	u32 drm_exec_flags = intr ? DRM_EXEC_INTERRUPTIBLE_WAIT : 0;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
+	struct xe_bo *bo;
+	int ret = 0;
+
+	xe_validation_guard(&ctx, &xe->val, &exec, drm_exec_flags, ret, false) {
+		bo = xe_bo_create_pin_map_at_aligned(xe, tile, NULL, size, offset,
+						     type, flags, vmap,
+						     alignment, &exec);
+		drm_exec_retry_on_contention(&exec);
+		if (IS_ERR(bo)) {
+			ret = PTR_ERR(bo);
+			xe_validation_retry_on_oom(&ctx, &ret);
+		}
+	}
+
+	return ret ? ERR_PTR(ret) : bo;
+}
+
 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
 				   struct xe_vm *vm, size_t size,
 				   enum ttm_bo_type type, u32 flags)
 {
-	return xe_bo_create_pin_map_at(xe, tile, vm, size, ~0ull, type, flags);
+	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) : XE_VALIDATION_UNIMPLEMENTED;
+
+	return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size, ~0ull, type, flags,
+					       true, 0, exec);
 }
 
 static void __xe_bo_unpin_map_no_vm(void *arg)
drivers/gpu/drm/xe/xe_bo.h +4 -9
index a625806deeb6..d06266af9662 100644
@@ -109,15 +109,10 @@ struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_vm *vm, size_t s
 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
 				   struct xe_vm *vm, size_t size,
 				   enum ttm_bo_type type, u32 flags);
-struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,
-				      struct xe_vm *vm, size_t size, u64 offset,
-				      enum ttm_bo_type type, u32 flags);
-struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
-					      struct xe_tile *tile,
-					      struct xe_vm *vm,
-					      size_t size, u64 offset,
-					      enum ttm_bo_type type, u32 flags,
-					      u64 alignment);
+struct xe_bo *
+xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile *tile,
+			     size_t size, u64 offset, enum ttm_bo_type type,
+			     u32 flags, bool vmap, u64 alignment, bool intr);
 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
 					   size_t size, u32 flags);
 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
drivers/gpu/drm/xe/xe_eu_stall.c +3 -3
index fdd514fec5ef..afabfc125488 100644
@@ -617,9 +617,9 @@ static int xe_eu_stall_data_buf_alloc(struct xe_eu_stall_data_stream *stream,
 
 	size = stream->per_xecore_buf_size * last_xecore;
 
-	bo = xe_bo_create_pin_map_at_aligned(tile->xe, tile, NULL,
-					     size, ~0ull, ttm_bo_type_kernel,
-					     XE_BO_FLAG_SYSTEM | XE_BO_FLAG_GGTT, SZ_64);
+	bo = xe_bo_create_pin_map_at_novm(tile->xe, tile, size, ~0ull, ttm_bo_type_kernel,
+					  XE_BO_FLAG_SYSTEM | XE_BO_FLAG_GGTT, true,
+					  SZ_64, false);
 	if (IS_ERR(bo)) {
 		kfree(stream->xecore_buf);
 		return PTR_ERR(bo);
Signature · 1 line
2.50.1
maintainer
#12 lore raw

Re: [PATCH 04/15] drm/xe: Pass down drm_exec context to validation

In reply to Thomas Hellström
Quoted 13 lines · Thomas Hellström
Quoted 11 lines · Matthew Brost
Quoted 7 lines · Thomas Hellström

We want all validation (potential backing store allocation) to be part of a drm_exec transaction. Therefore add a drm_exec pointer argument to xe_bo_validate() and ___xe_bo_create_locked(). Upcoming patches will deal with making all (or nearly all) calls to these functions part of a drm_exec transaction. In the meantime, define special values of the drm_exec pointer:

Quoted 5 lines
Quoted 1 line

Would the eventual idea be pass the exec further down to TTM?

Yes. The original series did this, and required multiple changes both to drm_exec and to TTM. Christian had some other ideas, though although the final goal was the same. So it's a task for us and AMD to agree on something here. The TTM object refcount removal series from Christian is a step on the way there.

Ok, I thought that was the idea and wanted to confirm.

Let me look at Christian's series now too.

Quoted 10 lines
Quoted 10 lines

Quoted 5 lines

XE_VALIDATION_UNIMPLEMENTED: Implementation of the drm_exec transaction has not been done yet.
XE_VALIDATION_UNSUPPORTED: Some Middle-layers (dma-buf) doesn't allow the drm_exec context to be passed down to map_attachment where validation takes place.

Quoted 6 lines
Quoted 2 lines

What is the expected longterm implictation of paths that are UNIMPLEMENTED and UNSUPPORTED?

IMO Unimplemented should not be allowed moving forward other than for debugging. UNIMPLEMENTED requires a new dma-buf mapping interface with an exec argument. I don't think all peers will support that, though and those won't participate fully in the scheme.

That was my thinking too—once the dma-buf mapping is fixed up, disallow this.

Matt

Quoted 1,492 lines
Quoted 1,489 lines
Quoted 1,290 lines

XE_VALIDATION_OPT_OUT: May be used only for kunit tests where exhaustive eviction isn't crucial and the ROI of converting those is very small.

For XE_VALIDATION_UNIMPLEMENTED and XE_VALIDATION_OPT_OUT there is also a lockdep check that a drm_exec transaction can indeed start at the location where the macro is expanded. This is to encourage developers to take this into consideration early in the code development process.

---
 drivers/gpu/drm/xe/Makefile                   |   1 + .../compat-i915-headers/gem/i915_gem_stolen.h |   6 +- drivers/gpu/drm/xe/display/xe_fb_pin.c        |   5 +- drivers/gpu/drm/xe/tests/xe_bo.c              |  20 +-- drivers/gpu/drm/xe/tests/xe_dma_buf.c         |  12 +- drivers/gpu/drm/xe/tests/xe_migrate.c         |  45 +++--- drivers/gpu/drm/xe/xe_bo.c                    | 129 +++++++++++++++--- drivers/gpu/drm/xe/xe_bo.h                    |  20 +-- drivers/gpu/drm/xe/xe_dma_buf.c               |  19 ++-

 drivers/gpu/drm/xe/xe_exec.c                  |   6 +-
 drivers/gpu/drm/xe/xe_ggtt.c                  |  15 +-
 drivers/gpu/drm/xe/xe_ggtt.h                  |   5 +-

 drivers/gpu/drm/xe/xe_gt_pagefault.c          |   4 +- drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c    |   6 +- drivers/gpu/drm/xe/xe_svm.c                   |   4 +- drivers/gpu/drm/xe/xe_validation.c            |  49 +++++++ drivers/gpu/drm/xe/xe_validation.h            |  69 ++++++++++ drivers/gpu/drm/xe/xe_vm.c                    |  26 +++- drivers/gpu/drm/xe/xe_vm.h                    |  33 ++++- drivers/gpu/drm/xe/xe_vm_types.h              |  32 +++--

 20 files changed, 401 insertions(+), 105 deletions(-)

 create mode 100644 drivers/gpu/drm/xe/xe_validation.c create mode 100644 drivers/gpu/drm/xe/xe_validation.h

drivers/gpu/drm/xe/Makefile +1 -0
index 8e0c3412a757..8ee7d275128d 100644
@@ -127,6 +127,7 @@ xe-y += xe_bb.o \
  	xe_tuning.o \
  	xe_uc.o \
  	xe_uc_fw.o \
+	xe_validation.o \
  	xe_vm.o \
  	xe_vram.o \
  	xe_vram_freq.o \
drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -0
index 41d39d67817a..1ce1e9da975b 100644
@@ -8,6 +8,7 @@


 #include "xe_ttm_stolen_mgr.h"
 #include "xe_res_cursor.h"
+#include "xe_validation.h"

 struct xe_bo;

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -0
@@ -20,6 +21,7 @@ static inline int i915_gem_stolen_insert_node_in_range(struct xe_device *xe,
  						       u32 size, u32 align,
  						       u32 start, u32 end)
  {
+	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
  	struct xe_bo *bo;
  	int err;
  	u32 flags = XE_BO_FLAG_PINNED | XE_BO_FLAG_STOLEN;
@@ -34,13 +36,13 @@ static inline int i915_gem_stolen_insert_node_in_range(struct xe_device *xe,


  bo = xe_bo_create_locked_range(xe, xe_device_get_root_tile(xe), NULL, size, start, end,

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -1
-				       ttm_bo_type_kernel, flags, 0);
+				       ttm_bo_type_kernel, flags, 0, exec);

  if (IS_ERR(bo)) {
  err = PTR_ERR(bo);
  bo = NULL;
  return err;
  }

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -1
-	err = xe_bo_pin(bo);
+	err = xe_bo_pin(bo, exec);

  xe_bo_unlock_vm_held(bo);

  if (err) {

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
index f1f8b5ab53ef..4b0748e6fdd6 100644
@@ -281,6 +281,7 @@ static struct i915_vma *__xe_pin_fb_vma(const struct intel_framebuffer *fb,
  	struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
  	struct drm_gem_object *obj = intel_fb_bo(&fb->base);
  	struct xe_bo *bo = gem_to_xe_bo(obj);
+	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
  	int ret;


  if (!vma)

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -0
@@ -313,9 +314,9 @@ static struct i915_vma *__xe_pin_fb_vma(const struct intel_framebuffer *fb,
  		goto err;


  if (IS_DGFX(xe))

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -1
-		ret = xe_bo_migrate(bo, XE_PL_VRAM0);
+		ret = xe_bo_migrate(bo, XE_PL_VRAM0, exec);

  else

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -1
-		ret = xe_bo_validate(bo, NULL, true);
+		ret = xe_bo_validate(bo, NULL, true, exec);

  if (!ret)
  ttm_bo_pin(&bo->ttm);
  ttm_bo_unreserve(&bo->ttm);

drivers/gpu/drm/xe/tests/xe_bo.c +0 -0
index bb469096d072..06ceba6c3c25 100644
@@ -23,7 +23,7 @@


 static int ccs_test_migrate(struct xe_tile *tile, struct xe_bo *bo, bool clear, u64 get_val, u64 assign_val,

drivers/gpu/drm/xe/tests/xe_bo.c +1 -1
-			    struct kunit *test)
+			    struct kunit *test, struct drm_exec *exec)

 {
  struct dma_fence *fence;
  struct ttm_tt *ttm;

drivers/gpu/drm/xe/tests/xe_bo.c +0 -0
@@ -35,7 +35,7 @@ static int ccs_test_migrate(struct xe_tile *tile, struct xe_bo *bo,
  	u32 offset;


  /* Move bo to VRAM if not already there. */

drivers/gpu/drm/xe/tests/xe_bo.c +1 -1
-	ret = xe_bo_validate(bo, NULL, false);
+	ret = xe_bo_validate(bo, NULL, false, exec);

  if (ret) {
  KUNIT_FAIL(test, "Failed to validate bo.\n"); return ret;

drivers/gpu/drm/xe/tests/xe_bo.c +0 -0
@@ -60,7 +60,7 @@ static int ccs_test_migrate(struct xe_tile *tile, struct xe_bo *bo,
  	}


  /* Evict to system. CCS data should be copied. */

drivers/gpu/drm/xe/tests/xe_bo.c +1 -1
-	ret = xe_bo_evict(bo);
+	ret = xe_bo_evict(bo, exec);

  if (ret) {
  KUNIT_FAIL(test, "Failed to evict bo.\n"); return ret;

drivers/gpu/drm/xe/tests/xe_bo.c +0 -0
@@ -132,6 +132,7 @@ static void ccs_test_run_tile(struct xe_device *xe, struct xe_tile *tile,


  /* TODO: Sanity check */
  unsigned int bo_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile);

drivers/gpu/drm/xe/tests/xe_bo.c +1 -0
+	struct drm_exec *exec = XE_VALIDATION_OPT_OUT;


  if (IS_DGFX(xe))
  kunit_info(test, "Testing vram id %u\n", tile->id);

drivers/gpu/drm/xe/tests/xe_bo.c +0 -0
@@ -149,18 +150,18 @@ static void ccs_test_run_tile(struct xe_device *xe, struct xe_tile *tile,


  kunit_info(test, "Verifying that CCS data is cleared on creation.\n"); ret = ccs_test_migrate(tile, bo, false, 0ULL, 0xdeadbeefdeadbeefULL,

drivers/gpu/drm/xe/tests/xe_bo.c +1 -1
-			       test);
+			       test, exec);

  if (ret)
  goto out_unlock;

  kunit_info(test, "Verifying that CCS data survives migration.\n"); ret = ccs_test_migrate(tile, bo, false, 0xdeadbeefdeadbeefULL,

drivers/gpu/drm/xe/tests/xe_bo.c +1 -1
-			       0xdeadbeefdeadbeefULL, test);
+			       0xdeadbeefdeadbeefULL, test, exec);

  if (ret)
  goto out_unlock;

  kunit_info(test, "Verifying that CCS data can be properly cleared.\n");

drivers/gpu/drm/xe/tests/xe_bo.c +1 -1
-	ret = ccs_test_migrate(tile, bo, true, 0ULL, 0ULL, test);
+	ret = ccs_test_migrate(tile, bo, true, 0ULL, 0ULL, test, exec);


 out_unlock:
  xe_bo_unlock(bo);

drivers/gpu/drm/xe/tests/xe_bo.c +1 -0
@@ -210,6 +211,7 @@ static int evict_test_run_tile(struct xe_device *xe, struct xe_tile *tile, struc
  	struct xe_bo *bo, *external;
  	unsigned int bo_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile);
  	struct xe_vm *vm = xe_migrate_get_vm(xe_device_get_root_tile(xe)->migrate);
+	struct drm_exec *exec = XE_VALIDATION_OPT_OUT;
  	struct xe_gt *__gt;
  	int err, i, id;

drivers/gpu/drm/xe/tests/xe_bo.c +0 -0
@@ -236,7 +238,7 @@ static int evict_test_run_tile(struct xe_device *xe, struct xe_tile *tile, struc
  		}


  xe_bo_lock(external, false);

drivers/gpu/drm/xe/tests/xe_bo.c +1 -1
-		err = xe_bo_pin_external(external);
+		err = xe_bo_pin_external(external, exec);

  xe_bo_unlock(external);
  if (err) {
  KUNIT_FAIL(test, "external bo pin err=%pe\n",

drivers/gpu/drm/xe/tests/xe_bo.c +2 -2
@@ -294,7 +296,7 @@ static int evict_test_run_tile(struct xe_device *xe, struct xe_tile *tile, struc
  		if (i) {
  			down_read(&vm->lock);
  			xe_vm_lock(vm, false);
-			err = xe_bo_validate(bo, bo->vm, false);
+			err = xe_bo_validate(bo, bo->vm, false, exec);
  			xe_vm_unlock(vm);
  			up_read(&vm->lock);
  			if (err) {
@@ -303,7 +305,7 @@ static int evict_test_run_tile(struct xe_device *xe, struct xe_tile *tile, struc
  				goto cleanup_all;
  			}
  			xe_bo_lock(external, false);
-			err = xe_bo_validate(external, NULL, false);
+			err = xe_bo_validate(external, NULL, false, exec);
  			xe_bo_unlock(external);
  			if (err) {
  				KUNIT_FAIL(test, "external bo valid err=%pe\n",
drivers/gpu/drm/xe/tests/xe_dma_buf.c +0 -0
index cde9530bef8c..965dd3280468 100644
@@ -27,7 +27,8 @@ static bool is_dynamic(struct dma_buf_test_params *params)
  }


 static void check_residency(struct kunit *test, struct xe_bo *exported,

drivers/gpu/drm/xe/tests/xe_dma_buf.c +2 -1
-			    struct xe_bo *imported, struct dma_buf *dmabuf)
+			    struct xe_bo *imported, struct dma_buf *dmabuf,
+			    struct drm_exec *exec)

 {
  struct dma_buf_test_params *params = to_dma_buf_test_params(test->priv); u32 mem_type;

drivers/gpu/drm/xe/tests/xe_dma_buf.c +1 -1
@@ -62,7 +63,7 @@ static void check_residency(struct kunit *test, struct xe_bo *exported,
  	 * importer is on a different device. If they're on the same device,
  	 * the exporter and the importer should be the same bo.
  	 */
-	ret = xe_bo_evict(exported);
+	ret = xe_bo_evict(exported, exec);
  	if (ret) {
  		if (ret != -EINTR && ret != -ERESTARTSYS)
  			KUNIT_FAIL(test, "Evicting exporter failed with err=%d.\n",
@@ -77,7 +78,7 @@ static void check_residency(struct kunit *test, struct xe_bo *exported,
  	}


  /* Re-validate the importer. This should move also exporter in. */

drivers/gpu/drm/xe/tests/xe_dma_buf.c +1 -1
-	ret = xe_bo_validate(imported, NULL, false);
+	ret = xe_bo_validate(imported, NULL, false, exec);

  if (ret) {
  if (ret != -EINTR && ret != -ERESTARTSYS) KUNIT_FAIL(test, "Validating importer failed with err=%d.\n",

drivers/gpu/drm/xe/tests/xe_dma_buf.c +1 -0
@@ -150,11 +151,12 @@ static void xe_test_dmabuf_import_same_driver(struct xe_device *xe)
  			KUNIT_FAIL(test,
  				   "xe_gem_prime_import() succeeded when it shouldn't have\n");
  		} else {
+			struct drm_exec *exec = XE_VALIDATION_OPT_OUT;
  			int err;


  /* Is everything where we expect it to be? */ xe_bo_lock(import_bo, false);

drivers/gpu/drm/xe/tests/xe_dma_buf.c +1 -1
-			err = xe_bo_validate(import_bo, NULL, false);
+			err = xe_bo_validate(import_bo, NULL, false, exec);


  /* Pinning in VRAM is not allowed. */ if (!is_dynamic(params) &&

drivers/gpu/drm/xe/tests/xe_dma_buf.c +0 -0
@@ -167,7 +169,7 @@ static void xe_test_dmabuf_import_same_driver(struct xe_device *xe)
  						  err == -ERESTARTSYS);


  if (!err)

drivers/gpu/drm/xe/tests/xe_dma_buf.c +1 -1
-				check_residency(test, bo, import_bo, dmabuf);
+				check_residency(test, bo, import_bo, dmabuf, exec);

  xe_bo_unlock(import_bo);
  }
  drm_gem_object_put(import);

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
index edd1e701aa1c..dfb445d09759 100644
@@ -70,7 +70,7 @@ static int run_sanity_job(struct xe_migrate *m, struct xe_device *xe,
  		} } while (0)


 static void test_copy(struct xe_migrate *m, struct xe_bo *bo,

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-		      struct kunit *test, u32 region)
+		      struct kunit *test, u32 region, struct drm_exec *exec)

 {
  struct xe_device *xe = tile_to_xe(m->tile); u64 retval, expected = 0;

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -84,14 +84,15 @@ static void test_copy(struct xe_migrate *m, struct xe_bo *bo,
  						   ttm_bo_type_kernel,

     region | XE_BO_FLAG_NEEDS_CPU_ACCESS |

drivers/gpu/drm/xe/tests/xe_migrate.c +2 -1
-						   XE_BO_FLAG_PINNED);
+						   XE_BO_FLAG_PINNED,
+						   exec);

  if (IS_ERR(remote)) {
  KUNIT_FAIL(test, "Failed to allocate remote bo for %s: %pe\n", str, remote);
  return;
  }

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	err = xe_bo_validate(remote, NULL, false);
+	err = xe_bo_validate(remote, NULL, false, exec);

  if (err) {
  KUNIT_FAIL(test, "Failed to validate system bo for %s: %i\n", str, err);

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -161,13 +162,13 @@ static void test_copy(struct xe_migrate *m, struct xe_bo *bo,
  }


 static void test_copy_sysmem(struct xe_migrate *m, struct xe_bo *bo,

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-			     struct kunit *test)
+			     struct drm_exec *exec, struct kunit *test)

 {

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	test_copy(m, bo, test, XE_BO_FLAG_SYSTEM);
+	test_copy(m, bo, test, XE_BO_FLAG_SYSTEM, exec);

 }

 static void test_copy_vram(struct xe_migrate *m, struct xe_bo *bo,

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-			   struct kunit *test)
+			   struct drm_exec *exec, struct kunit *test)

 {
  u32 region;

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -178,10 +179,11 @@ static void test_copy_vram(struct xe_migrate *m, struct xe_bo *bo,
  		region = XE_BO_FLAG_VRAM1;

  else
  region = XE_BO_FLAG_VRAM0;

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	test_copy(m, bo, test, region);
+	test_copy(m, bo, test, region, exec);

 }

drivers/gpu/drm/xe/tests/xe_migrate.c +2 -1
-static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test)
+static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test,
+				   struct drm_exec *exec)

 {
  struct xe_tile *tile = m->tile;
  struct xe_device *xe = tile_to_xe(tile);

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -290,10 +292,10 @@ static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test)
  	check(retval, expected, "Command clear small last value", test);


  kunit_info(test, "Copying small buffer object to system\n");

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	test_copy_sysmem(m, tiny, test);
+	test_copy_sysmem(m, tiny, exec, test);

  if (xe->info.tile_count > 1) {
  kunit_info(test, "Copying small buffer object to other vram\n");

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-		test_copy_vram(m, tiny, test);
+		test_copy_vram(m, tiny, exec, test);

  }

  /* Clear a big bo */

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -312,10 +314,10 @@ static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test)
  	check(retval, expected, "Command clear big last value", test);


  kunit_info(test, "Copying big buffer object to system\n");

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	test_copy_sysmem(m, big, test);
+	test_copy_sysmem(m, big, exec, test);

  if (xe->info.tile_count > 1) {
  kunit_info(test, "Copying big buffer object to other vram\n");

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-		test_copy_vram(m, big, test);
+		test_copy_vram(m, big, exec, test);

  }

 out:

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -343,10 +345,11 @@ static int migrate_test_run_device(struct xe_device *xe)


  for_each_tile(tile, xe, id) {
  struct xe_migrate *m = tile->migrate;

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -0
+		struct drm_exec *exec = XE_VALIDATION_OPT_OUT;


  kunit_info(test, "Testing tile id %d.\n", id); xe_vm_lock(m->q->vm, false);

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-		xe_migrate_sanity_test(m, test);
+		xe_migrate_sanity_test(m, test, exec);

  xe_vm_unlock(m->q->vm);
  }

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -490,7 +493,7 @@ static struct dma_fence *blt_copy(struct xe_tile *tile,


 static void test_migrate(struct xe_device *xe, struct xe_tile *tile,
  struct xe_bo *sys_bo, struct xe_bo *vram_bo, struct xe_bo *ccs_bo,

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-			 struct kunit *test)
+			 struct drm_exec *exec, struct kunit *test)

 {
  struct dma_fence *fence;
  u64 expected, retval;

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -509,7 +512,7 @@ static void test_migrate(struct xe_device *xe, struct xe_tile *tile,
  	dma_fence_put(fence);


  kunit_info(test, "Evict vram buffer object\n");

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	ret = xe_bo_evict(vram_bo);
+	ret = xe_bo_evict(vram_bo, exec);

  if (ret) {
  KUNIT_FAIL(test, "Failed to evict bo.\n"); return;

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -538,7 +541,7 @@ static void test_migrate(struct xe_device *xe, struct xe_tile *tile,
  	dma_fence_put(fence);


  kunit_info(test, "Restore vram buffer object\n");

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	ret = xe_bo_validate(vram_bo, NULL, false);
+	ret = xe_bo_validate(vram_bo, NULL, false, exec);

  if (ret) {
  KUNIT_FAIL(test, "Failed to validate vram bo for: %li\n", ret); return;

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -0
@@ -636,6 +639,7 @@ static void validate_ccs_test_run_tile(struct xe_device *xe, struct xe_tile *til
  {
  	struct xe_bo *sys_bo, *vram_bo = NULL, *ccs_bo = NULL;
  	unsigned int bo_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile);
+	struct drm_exec *exec;
  	long ret;


  sys_bo = xe_bo_create_user(xe, NULL, NULL, SZ_4M,

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -650,8 +654,9 @@ static void validate_ccs_test_run_tile(struct xe_device *xe, struct xe_tile *til
  		return;
  	}

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -0
+	exec = XE_VALIDATION_OPT_OUT;

  xe_bo_lock(sys_bo, false);

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	ret = xe_bo_validate(sys_bo, NULL, false);
+	ret = xe_bo_validate(sys_bo, NULL, false, exec);

  if (ret) {
  KUNIT_FAIL(test, "Failed to validate system bo for: %li\n", ret); goto free_sysbo;

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -676,7 +681,7 @@ static void validate_ccs_test_run_tile(struct xe_device *xe, struct xe_tile *til
  	}


  xe_bo_lock(ccs_bo, false);

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	ret = xe_bo_validate(ccs_bo, NULL, false);
+	ret = xe_bo_validate(ccs_bo, NULL, false, exec);

  if (ret) {
  KUNIT_FAIL(test, "Failed to validate system bo for: %li\n", ret); goto free_ccsbo;

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -700,7 +705,7 @@ static void validate_ccs_test_run_tile(struct xe_device *xe, struct xe_tile *til
  	}


  xe_bo_lock(vram_bo, false);

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	ret = xe_bo_validate(vram_bo, NULL, false);
+	ret = xe_bo_validate(vram_bo, NULL, false, exec);

  if (ret) {
  KUNIT_FAIL(test, "Failed to validate vram bo for: %li\n", ret); goto free_vrambo;

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -0
@@ -713,7 +718,7 @@ static void validate_ccs_test_run_tile(struct xe_device *xe, struct xe_tile *til
  	}


  test_clear(xe, tile, sys_bo, vram_bo, test);

drivers/gpu/drm/xe/tests/xe_migrate.c +1 -1
-	test_migrate(xe, tile, sys_bo, vram_bo, ccs_bo, test);
+	test_migrate(xe, tile, sys_bo, vram_bo, ccs_bo, exec, test);

  xe_bo_unlock(vram_bo);

  xe_bo_lock(vram_bo, false);

drivers/gpu/drm/xe/xe_bo.c +1 -0
index 11eaf3b06766..e71addf51ed0 100644
@@ -1139,6 +1139,7 @@ long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
  int xe_bo_notifier_prepare_pinned(struct xe_bo *bo)
  {
  	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
+	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
  	struct xe_bo *backup;
  	int ret = 0;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -1163,7 +1164,7 @@ int xe_bo_notifier_prepare_pinned(struct xe_bo *bo)
  	backup = ___xe_bo_create_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL, xe_bo_size(bo),
  					DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,

  XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS |

drivers/gpu/drm/xe/xe_bo.c +1 -1
-					XE_BO_FLAG_PINNED);
+					XE_BO_FLAG_PINNED, exec);

  if (IS_ERR(backup)) {
  ret = PTR_ERR(backup);
  goto out_unlock_bo;

drivers/gpu/drm/xe/xe_bo.c +1 -0
@@ -1214,6 +1215,7 @@ int xe_bo_notifier_unprepare_pinned(struct xe_bo *bo)
  int xe_bo_evict_pinned(struct xe_bo *bo)
  {
  	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
+	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
  	struct xe_bo *backup = bo->backup_obj;
  	bool backup_created = false;
  	bool unmap = false;
@@ -1242,7 +1244,7 @@ int xe_bo_evict_pinned(struct xe_bo *bo)
  						NULL, xe_bo_size(bo),
  						DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,

  XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS |

drivers/gpu/drm/xe/xe_bo.c +1 -1
-						XE_BO_FLAG_PINNED);
+						XE_BO_FLAG_PINNED, exec);

  if (IS_ERR(backup)) {
  ret = PTR_ERR(backup);
  goto out_unlock_bo;

drivers/gpu/drm/xe/xe_bo.c +1 -0
@@ -1718,12 +1720,14 @@ static vm_fault_t xe_gem_fault(struct vm_fault *vmf)
  	struct xe_device *xe = to_xe_device(ddev);
  	struct xe_bo *bo = ttm_to_xe_bo(tbo);
  	bool needs_rpm = bo->flags & XE_BO_FLAG_VRAM_MASK;
+	struct drm_exec *exec;
  	vm_fault_t ret;
  	int idx;


  if (needs_rpm)
  xe_pm_runtime_get(xe);

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	exec = XE_VALIDATION_UNIMPLEMENTED;

  ret = ttm_bo_vm_reserve(tbo, vmf);
  if (ret)
  goto out;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -1731,6 +1735,7 @@ static vm_fault_t xe_gem_fault(struct vm_fault *vmf)
  	if (drm_dev_enter(ddev, &idx)) {
  		trace_xe_bo_cpu_fault(bo);

drivers/gpu/drm/xe/xe_bo.c +1 -0
+		xe_validation_assert_exec(xe, exec, &tbo->base);

  ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot, TTM_BO_VM_NUM_PREFAULT); drm_dev_exit(idx);

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -1850,11 +1855,32 @@ void xe_bo_free(struct xe_bo *bo)
  	kfree(bo);
  }


+/**
+ * ___xe_bo_create_locked() - Initialize or create an xe_bo.
+ * @xe: The xe device.
+ * @bo: An already allocated buffer object or NULL
+ * if the function should allocate a new one.
+ * @tile: The tile to select for migration of this bo, and the tile used for
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @resv: Pointer to a locked shared reservation object to use fo this bo,
+ * or NULL for the xe_bo to use its own.
+ * @bulk: The bulk move to use for LRU bumping, or NULL for external bos.
+ * @size: The storage size to use for the bo.
+ * @cpu_caching: The cpu caching used for system memory backing store.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @exec: The drm_exec transaction to use for exhaustive eviction.
+ *
+ * Initialize or create an xe buffer object. On failure, any allocated buffer
+ * object passed in @bo will have been unreferenced.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ */
 struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo, struct xe_tile *tile, struct dma_resv *resv, struct ttm_lru_bulk_move *bulk, size_t size, u16 cpu_caching, enum ttm_bo_type type,

drivers/gpu/drm/xe/xe_bo.c +1 -1
-				     u32 flags)
+				     u32 flags, struct drm_exec *exec)

 {
  struct ttm_operation_ctx ctx = {
  .interruptible = true,

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -1923,6 +1949,7 @@ struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
  		ctx.resv = resv;
  	}

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	xe_validation_assert_exec(xe, exec, &bo->ttm.base);

  if (!(flags & XE_BO_FLAG_FIXED_PLACEMENT)) {
  err = __xe_bo_placement_for_flags(xe, bo, bo->flags); if (WARN_ON(err)) {

drivers/gpu/drm/xe/xe_bo.c +1 -1
@@ -2024,7 +2051,7 @@ __xe_bo_create_locked(struct xe_device *xe,
  		      struct xe_tile *tile, struct xe_vm *vm,
  		      size_t size, u64 start, u64 end,
  		      u16 cpu_caching, enum ttm_bo_type type, u32 flags,
-		      u64 alignment)
+		      u64 alignment, struct drm_exec *exec)
  {
  	struct xe_bo *bo = NULL;
  	int err;
@@ -2049,7 +2076,7 @@ __xe_bo_create_locked(struct xe_device *xe,

      vm && !xe_vm_in_fault_mode(vm) && flags & XE_BO_FLAG_USER ? &vm->lru_bulk_move : NULL, size,

drivers/gpu/drm/xe/xe_bo.c +1 -1
-				    cpu_caching, type, flags);
+				    cpu_caching, type, flags, exec);

  if (IS_ERR(bo))
  return bo;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2083,9 +2110,10 @@ __xe_bo_create_locked(struct xe_device *xe,


  if (flags & XE_BO_FLAG_FIXED_PLACEMENT) { err = xe_ggtt_insert_bo_at(t->mem.ggtt, bo,

drivers/gpu/drm/xe/xe_bo.c +2 -1
-							   start + xe_bo_size(bo), U64_MAX);
+							   start + xe_bo_size(bo), U64_MAX,
+							   exec);

  } else {

drivers/gpu/drm/xe/xe_bo.c +1 -1
-				err = xe_ggtt_insert_bo(t->mem.ggtt, bo);
+				err = xe_ggtt_insert_bo(t->mem.ggtt, bo, exec);

  }
  if (err)
  goto err_unlock_put_bo;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2102,22 +2130,59 @@ __xe_bo_create_locked(struct xe_device *xe,
  	return ERR_PTR(err);
  }


+/**
+ * xe_bo_create_locked_range() - Create a BO with range- and alignment options
+ * @xe: The xe device.
+ * @tile: The tile to select for migration of this bo, and the tile used for
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @vm: The local vm or NULL for external objects.
+ * @size: The storage size to use for the bo.
+ * @start: Start of fixed VRAM range or 0.
+ * @end: End of fixed VRAM range or ~0ULL.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @alignment: For GGTT buffer objects, the minimum GGTT alignment.
+ * @exec: The drm_exec transaction to use for exhaustive eviction.
+ *
+ * Create an Xe BO with range- and alignment options. If @start and @end indicate
+ * a fixed VRAM range, this must be a ttm_bo_type_kernel bo with VRAM placement
+ * only. The @alignment parameter can be used for GGTT alignment.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ */
 struct xe_bo *
 xe_bo_create_locked_range(struct xe_device *xe,
    struct xe_tile *tile, struct xe_vm *vm, size_t size, u64 start, u64 end,

drivers/gpu/drm/xe/xe_bo.c +2 -1
-			  enum ttm_bo_type type, u32 flags, u64 alignment)
+			  enum ttm_bo_type type, u32 flags, u64 alignment,
+			  struct drm_exec *exec)

 {
  return __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, type,

drivers/gpu/drm/xe/xe_bo.c +1 -1
-				     flags, alignment);
+				     flags, alignment, exec);

 }

+/**
+ * xe_bo_create_locked() - Create a BO
+ * @xe: The xe device.
+ * @tile: The tile to select for migration of this bo, and the tile used for
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @vm: The local vm or NULL for external objects.
+ * @size: The storage size to use for the bo.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @exec: The drm_exec transaction to use for exhaustive eviction.
+ *
+ * Create a locked xe BO with no range- nor alignment restrictions.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ */
 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile, struct xe_vm *vm, size_t size,

drivers/gpu/drm/xe/xe_bo.c +2 -1
-				  enum ttm_bo_type type, u32 flags)
+				  enum ttm_bo_type type, u32 flags,
+				  struct drm_exec *exec)

 {
  return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, type,

drivers/gpu/drm/xe/xe_bo.c +1 -1
-				     flags, 0);
+				     flags, 0, exec);

 }

 struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.c +2 -1
@@ -2125,9 +2190,10 @@ struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile,
  				u16 cpu_caching,
  				u32 flags)
  {
+	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) : XE_VALIDATION_UNIMPLEMENTED;
  	struct xe_bo *bo = __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL,
  						 cpu_caching, ttm_bo_type_device,
-						 flags | XE_BO_FLAG_USER, 0);
+						 flags | XE_BO_FLAG_USER, 0, exec);
  	if (!IS_ERR(bo))
  		xe_bo_unlock_vm_held(bo);

drivers/gpu/drm/xe/xe_bo.c +2 -1
@@ -2138,7 +2204,8 @@ struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile,
  			   struct xe_vm *vm, size_t size,
  			   enum ttm_bo_type type, u32 flags)
  {
-	struct xe_bo *bo = xe_bo_create_locked(xe, tile, vm, size, type, flags);
+	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) : XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_bo *bo = xe_bo_create_locked(xe, tile, vm, size, type, flags, exec);


  if (!IS_ERR(bo))
  xe_bo_unlock_vm_held(bo);

drivers/gpu/drm/xe/xe_bo.c +1 -0
@@ -2166,6 +2233,7 @@ struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
  	int err;
  	u64 start = offset == ~0ull ? 0 : offset;
  	u64 end = offset == ~0ull ? offset : start + size;
+	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) : XE_VALIDATION_UNIMPLEMENTED;


  if (flags & XE_BO_FLAG_STOLEN && xe_ttm_stolen_cpu_access_needs_ggtt(xe))

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2173,11 +2241,11 @@ struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,


  bo = xe_bo_create_locked_range(xe, tile, vm, size, start, end, type, flags | XE_BO_FLAG_NEEDS_CPU_ACCESS | XE_BO_FLAG_PINNED,

drivers/gpu/drm/xe/xe_bo.c +1 -1
-				       alignment);
+				       alignment, exec);

  if (IS_ERR(bo))
  return bo;

drivers/gpu/drm/xe/xe_bo.c +1 -1
-	err = xe_bo_pin(bo);
+	err = xe_bo_pin(bo, exec);

  if (err)
  goto err_put;

drivers/gpu/drm/xe/xe_bo.c +2 -1
@@ -2299,6 +2367,7 @@ uint64_t vram_region_gpu_offset(struct ttm_resource *res)
  /**
   * xe_bo_pin_external - pin an external BO
   * @bo: buffer object to be pinned
+ * @exec: The drm_exec transaction to use for exhaustive eviction.
   *
   * Pin an external (not tied to a VM, can be exported via dma-buf / prime FD)
   * BO. Unique call compared to xe_bo_pin as this function has it own set of
@@ -2306,7 +2375,7 @@ uint64_t vram_region_gpu_offset(struct ttm_resource *res)
   *
   * Returns 0 for success, negative error code otherwise.
   */
-int xe_bo_pin_external(struct xe_bo *bo)
+int xe_bo_pin_external(struct xe_bo *bo, struct drm_exec *exec)
  {
  	struct xe_device *xe = xe_bo_device(bo);
  	int err;
@@ -2315,7 +2384,7 @@ int xe_bo_pin_external(struct xe_bo *bo)
  	xe_assert(xe, xe_bo_is_user(bo));


  if (!xe_bo_is_pinned(bo)) {

drivers/gpu/drm/xe/xe_bo.c +1 -1
-		err = xe_bo_validate(bo, NULL, false);
+		err = xe_bo_validate(bo, NULL, false, exec);

  if (err)
  return err;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2337,7 +2406,17 @@ int xe_bo_pin_external(struct xe_bo *bo)
  	return 0;
  }

drivers/gpu/drm/xe/xe_bo.c +11 -1
-int xe_bo_pin(struct xe_bo *bo)
+/**
+ * xe_bo_pin() - Pin a kernel bo after potentially migrating it
+ * @bo: The kernel bo to pin.
+ * @exec: The drm_exec transaction to use for exhaustive eviction.
+ *
+ * Attempts to migrate a bo to @bo->placement. If that succeeds,
+ * pins the bo.
+ *
+ * Return: %0 on success, negative error code on migration failure.
+ */
+int xe_bo_pin(struct xe_bo *bo, struct drm_exec *exec)

 {
  struct ttm_place *place = &bo->placements[0]; struct xe_device *xe = xe_bo_device(bo);

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2359,7 +2438,7 @@ int xe_bo_pin(struct xe_bo *bo)
  	/* We only expect at most 1 pin */
  	xe_assert(xe, !xe_bo_is_pinned(bo));

drivers/gpu/drm/xe/xe_bo.c +1 -1
-	err = xe_bo_validate(bo, NULL, false);
+	err = xe_bo_validate(bo, NULL, false, exec);

  if (err)
  return err;

drivers/gpu/drm/xe/xe_bo.c +3 -1
@@ -2452,6 +2531,7 @@ void xe_bo_unpin(struct xe_bo *bo)
   *      NULL. Used together with @allow_res_evict.
   * @allow_res_evict: Whether it's allowed to evict bos sharing @vm's
   *                   reservation object.
+ * @exec: The drm_exec transaction to use for exhaustive eviction.
   *
   * Make sure the bo is in allowed placement, migrating it if necessary. If
   * needed, other bos will be evicted. If bos selected for eviction shares
@@ -2461,7 +2541,8 @@ void xe_bo_unpin(struct xe_bo *bo)
   * Return: 0 on success, negative error code on failure. May return
   * -EINTR or -ERESTARTSYS if internal waits are interrupted by a signal.
   */
-int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict)
+int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict,
+		   struct drm_exec *exec)
  {
  	struct ttm_operation_ctx ctx = {
  		.interruptible = true,
@@ -2480,6 +2561,7 @@ int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict)


  xe_vm_set_validating(vm, allow_res_evict); trace_xe_bo_validate(bo);

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	xe_validation_assert_exec(xe_bo_device(bo), exec, &bo->ttm.base);

  ret = ttm_bo_validate(&bo->ttm, &bo->placement, &ctx); xe_vm_clear_validating(vm, allow_res_evict);

drivers/gpu/drm/xe/xe_bo.c +2 -1
@@ -2917,6 +2999,7 @@ static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place)
   * xe_bo_migrate - Migrate an object to the desired region id
   * @bo: The buffer object to migrate.
   * @mem_type: The TTM region type to migrate to.
+ * @exec: The drm_exec transaction to use for exhaustive eviction.
   *
   * Attempt to migrate the buffer object to the desired memory region. The
   * buffer object may not be pinned, and must be locked.
@@ -2928,7 +3011,7 @@ static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place)
   * Return: 0 on success. Negative error code on failure. In particular may
   * return -EINTR or -ERESTARTSYS if signal pending.
   */
-int xe_bo_migrate(struct xe_bo *bo, u32 mem_type)
+int xe_bo_migrate(struct xe_bo *bo, u32 mem_type, struct drm_exec *exec)
  {
  	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
  	struct ttm_operation_ctx ctx = {
@@ -2966,19 +3049,21 @@ int xe_bo_migrate(struct xe_bo *bo, u32 mem_type)
  		add_vram(xe, bo, &requested, bo->flags, mem_type, &c);
  	}

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	xe_validation_assert_exec(xe_bo_device(bo), exec, &bo->ttm.base);

  return ttm_bo_validate(&bo->ttm, &placement, &ctx);
 }

 /**
  * xe_bo_evict - Evict an object to evict placement
  * @bo: The buffer object to migrate.
+ * @exec: The drm_exec transaction to use for exhaustive eviction. *
  * On successful completion, the object memory will be moved to evict
  * placement. This function blocks until the object has been fully moved. *
  * Return: 0 on success. Negative error code on failure.
  */

drivers/gpu/drm/xe/xe_bo.c +1 -1
-int xe_bo_evict(struct xe_bo *bo)
+int xe_bo_evict(struct xe_bo *bo, struct drm_exec *exec)

 {
  struct ttm_operation_ctx ctx = {
  .interruptible = false,

drivers/gpu/drm/xe/xe_bo.h +0 -0
index 8cce413b5235..b1b6cb622d71 100644
@@ -10,6 +10,7 @@


 #include "xe_bo_types.h"
 #include "xe_macros.h"
+#include "xe_validation.h"
 #include "xe_vm_types.h"
 #include "xe_vm.h"
 #include "xe_vram_types.h"

drivers/gpu/drm/xe/xe_bo.h +1 -1
@@ -92,15 +93,17 @@ struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
  				     struct xe_tile *tile, struct dma_resv *resv,
  				     struct ttm_lru_bulk_move *bulk, size_t size,
  				     u16 cpu_caching, enum ttm_bo_type type,
-				     u32 flags);
+				     u32 flags, struct drm_exec *exec);

 struct xe_bo *
 xe_bo_create_locked_range(struct xe_device *xe,
    struct xe_tile *tile, struct xe_vm *vm, size_t size, u64 start, u64 end,

drivers/gpu/drm/xe/xe_bo.h +2 -1
-			  enum ttm_bo_type type, u32 flags, u64 alignment);
+			  enum ttm_bo_type type, u32 flags, u64 alignment,
+			  struct drm_exec *exec);

 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile, struct xe_vm *vm, size_t size,

drivers/gpu/drm/xe/xe_bo.h +2 -1
-				  enum ttm_bo_type type, u32 flags);
+				  enum ttm_bo_type type, u32 flags,
+				  struct drm_exec *exec);

 struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile, struct xe_vm *vm, size_t size, enum ttm_bo_type type, u32 flags);

drivers/gpu/drm/xe/xe_bo.h +0 -0
@@ -200,11 +203,12 @@ static inline void xe_bo_unlock_vm_held(struct xe_bo *bo)
  	}
  }

drivers/gpu/drm/xe/xe_bo.h +2 -2
-int xe_bo_pin_external(struct xe_bo *bo);
-int xe_bo_pin(struct xe_bo *bo);
+int xe_bo_pin_external(struct xe_bo *bo, struct drm_exec *exec);
+int xe_bo_pin(struct xe_bo *bo, struct drm_exec *exec);

 void xe_bo_unpin_external(struct xe_bo *bo);
 void xe_bo_unpin(struct xe_bo *bo);

drivers/gpu/drm/xe/xe_bo.h +2 -1
-int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict);
+int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict,
+		   struct drm_exec *exec);


 static inline bool xe_bo_is_pinned(struct xe_bo *bo) {

drivers/gpu/drm/xe/xe_bo.h +0 -0
@@ -285,8 +289,8 @@ uint64_t vram_region_gpu_offset(struct ttm_resource *res);


 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type);

drivers/gpu/drm/xe/xe_bo.h +2 -2
-int xe_bo_migrate(struct xe_bo *bo, u32 mem_type);
-int xe_bo_evict(struct xe_bo *bo);
+int xe_bo_migrate(struct xe_bo *bo, u32 mem_type, struct drm_exec *exec);
+int xe_bo_evict(struct xe_bo *bo, struct drm_exec *exec);


 int xe_bo_evict_pinned(struct xe_bo *bo); int xe_bo_notifier_prepare_pinned(struct xe_bo *bo);

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
index 346f857f3837..78a827d4e726 100644
@@ -51,6 +51,7 @@ static int xe_dma_buf_pin(struct dma_buf_attachment *attach)
  	struct drm_gem_object *obj = attach->dmabuf->priv;
  	struct xe_bo *bo = gem_to_xe_bo(obj);
  	struct xe_device *xe = xe_bo_device(bo);
+	struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED;
  	int ret;


  /*

drivers/gpu/drm/xe/xe_dma_buf.c +0 -0
@@ -63,7 +64,7 @@ static int xe_dma_buf_pin(struct dma_buf_attachment *attach)
  		return -EINVAL;
  	}

drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
-	ret = xe_bo_migrate(bo, XE_PL_TT);
+	ret = xe_bo_migrate(bo, XE_PL_TT, exec);

  if (ret) {
  if (ret != -EINTR && ret != -ERESTARTSYS) drm_dbg(&xe->drm,

drivers/gpu/drm/xe/xe_dma_buf.c +0 -0
@@ -72,7 +73,7 @@ static int xe_dma_buf_pin(struct dma_buf_attachment *attach)
  		return ret;
  	}

drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
-	ret = xe_bo_pin_external(bo);
+	ret = xe_bo_pin_external(bo, exec);

  xe_assert(xe, !ret);

  return 0;

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
@@ -92,6 +93,7 @@ static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,
  	struct dma_buf *dma_buf = attach->dmabuf;
  	struct drm_gem_object *obj = dma_buf->priv;
  	struct xe_bo *bo = gem_to_xe_bo(obj);
+	struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED;
  	struct sg_table *sgt;
  	int r = 0;

drivers/gpu/drm/xe/xe_dma_buf.c +0 -0
@@ -100,9 +102,9 @@ static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,


  if (!xe_bo_is_pinned(bo)) {
  if (!attach->peer2peer)

drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
-			r = xe_bo_migrate(bo, XE_PL_TT);
+			r = xe_bo_migrate(bo, XE_PL_TT, exec);

  else

drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
-			r = xe_bo_validate(bo, NULL, false);
+			r = xe_bo_validate(bo, NULL, false, exec);

  if (r)
  return ERR_PTR(r);
  }

drivers/gpu/drm/xe/xe_dma_buf.c +0 -0
@@ -161,13 +163,14 @@ static int xe_dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
  	struct xe_bo *bo = gem_to_xe_bo(obj);

  bool reads =  (direction == DMA_BIDIRECTIONAL || direction == DMA_FROM_DEVICE);

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
+	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;


  if (!reads)
  return 0;

  /* Can we do interruptible lock here? */
  xe_bo_lock(bo, false);

drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
-	(void)xe_bo_migrate(bo, XE_PL_TT);
+	(void)xe_bo_migrate(bo, XE_PL_TT, exec);

  xe_bo_unlock(bo);

  return 0;

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
@@ -208,13 +211,14 @@ xe_dma_buf_init_obj(struct drm_device *dev, struct xe_bo *storage,
  {
  	struct dma_resv *resv = dma_buf->resv;
  	struct xe_device *xe = to_xe_device(dev);
+	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
  	struct xe_bo *bo;
  	int ret;


  dma_resv_lock(resv, NULL);
  bo = ___xe_bo_create_locked(xe, storage, NULL, resv, NULL, dma_buf->size, 0, /* Will require 1way or 2way for vm_bind */

drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
-				    ttm_bo_type_sg, XE_BO_FLAG_SYSTEM);
+				    ttm_bo_type_sg, XE_BO_FLAG_SYSTEM, exec);

  if (IS_ERR(bo)) {
  ret = PTR_ERR(bo);
  goto error;

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
@@ -232,8 +236,9 @@ static void xe_dma_buf_move_notify(struct dma_buf_attachment *attach)
  {
  	struct drm_gem_object *obj = attach->importer_priv;
  	struct xe_bo *bo = gem_to_xe_bo(obj);
+	struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED;

drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
-	XE_WARN_ON(xe_bo_evict(bo));
+	XE_WARN_ON(xe_bo_evict(bo, exec));

 }

 static const struct dma_buf_attach_ops xe_dma_buf_attach_ops = {

drivers/gpu/drm/xe/xe_exec.c +1 -0
index 44364c042ad7..0bcb4fb9a10e 100644
@@ -97,9 +97,13 @@
  static int xe_exec_fn(struct drm_gpuvm_exec *vm_exec)
  {
  	struct xe_vm *vm = container_of(vm_exec->vm, struct xe_vm, gpuvm);
+	int ret;


  /* The fence slot added here is intended for the exec sched job. */

drivers/gpu/drm/xe/xe_exec.c +4 -1
-	return xe_vm_validate_rebind(vm, &vm_exec->exec, 1);
+	xe_vm_set_validation_exec(vm, &vm_exec->exec);
+	ret = xe_vm_validate_rebind(vm, &vm_exec->exec, 1);
+	xe_vm_set_validation_exec(vm, NULL);
+	return ret;

 }

 int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)

drivers/gpu/drm/xe/xe_ggtt.c +0 -0
index e03222f5ac5a..a47c0131956b 100644
@@ -731,7 +731,7 @@ void xe_ggtt_map_bo_unlocked(struct xe_ggtt *ggtt, struct xe_bo *bo)
  }


 static int __xe_ggtt_insert_bo_at(struct xe_ggtt *ggtt, struct xe_bo *bo,

drivers/gpu/drm/xe/xe_ggtt.c +1 -1
-				  u64 start, u64 end)
+				  u64 start, u64 end, struct drm_exec *exec)

 {
  u64 alignment = bo->min_align > 0 ? bo->min_align : XE_PAGE_SIZE; u8 tile_id = ggtt->tile->id;

drivers/gpu/drm/xe/xe_ggtt.c +0 -0
@@ -746,7 +746,7 @@ static int __xe_ggtt_insert_bo_at(struct xe_ggtt *ggtt, struct xe_bo *bo,
  		return 0;
  	}

drivers/gpu/drm/xe/xe_ggtt.c +1 -1
-	err = xe_bo_validate(bo, NULL, false);
+	err = xe_bo_validate(bo, NULL, false, exec);

  if (err)
  return err;

drivers/gpu/drm/xe/xe_ggtt.c +3 -2
@@ -788,25 +788,28 @@ static int __xe_ggtt_insert_bo_at(struct xe_ggtt *ggtt, struct xe_bo *bo,
   * @bo: the &xe_bo to be inserted
   * @start: address where it will be inserted
   * @end: end of the range where it will be inserted
+ * @exec: The drm_exec transaction to use for exhaustive eviction.
   *
   * Return: 0 on success or a negative error code on failure.
   */
  int xe_ggtt_insert_bo_at(struct xe_ggtt *ggtt, struct xe_bo *bo,
-			 u64 start, u64 end)
+			 u64 start, u64 end, struct drm_exec *exec)
  {
-	return __xe_ggtt_insert_bo_at(ggtt, bo, start, end);
+	return __xe_ggtt_insert_bo_at(ggtt, bo, start, end, exec);
  }


 /**
  * xe_ggtt_insert_bo - Insert BO into GGTT
  * @ggtt: the &xe_ggtt where bo will be inserted
  * @bo: the &xe_bo to be inserted
+ * @exec: The drm_exec transaction to use for exhaustive eviction. *
  * Return: 0 on success or a negative error code on failure. */

drivers/gpu/drm/xe/xe_ggtt.c +2 -1
-int xe_ggtt_insert_bo(struct xe_ggtt *ggtt, struct xe_bo *bo)
+int xe_ggtt_insert_bo(struct xe_ggtt *ggtt, struct xe_bo *bo,
+		      struct drm_exec *exec)

 {

drivers/gpu/drm/xe/xe_ggtt.c +1 -1
-	return __xe_ggtt_insert_bo_at(ggtt, bo, 0, U64_MAX);
+	return __xe_ggtt_insert_bo_at(ggtt, bo, 0, U64_MAX, exec);

 }

 /**

drivers/gpu/drm/xe/xe_ggtt.h +0 -0
index fbe1e397d05d..75fc7a1efea7 100644
@@ -10,6 +10,7 @@


 struct drm_printer;
 struct xe_tile;
+struct drm_exec;

 struct xe_ggtt *xe_ggtt_alloc(struct xe_tile *tile); int xe_ggtt_init_early(struct xe_ggtt *ggtt);

drivers/gpu/drm/xe/xe_ggtt.h +2 -2
@@ -31,9 +32,9 @@ bool xe_ggtt_node_allocated(const struct xe_ggtt_node *node);
  void xe_ggtt_map_bo(struct xe_ggtt *ggtt, struct xe_ggtt_node *node,
  		    struct xe_bo *bo, u16 pat_index);
  void xe_ggtt_map_bo_unlocked(struct xe_ggtt *ggtt, struct xe_bo *bo);
-int xe_ggtt_insert_bo(struct xe_ggtt *ggtt, struct xe_bo *bo);
+int xe_ggtt_insert_bo(struct xe_ggtt *ggtt, struct xe_bo *bo, struct drm_exec *exec);
  int xe_ggtt_insert_bo_at(struct xe_ggtt *ggtt, struct xe_bo *bo,
-			 u64 start, u64 end);
+			 u64 start, u64 end, struct drm_exec *exec);
  void xe_ggtt_remove_bo(struct xe_ggtt *ggtt, struct xe_bo *bo);
  u64 xe_ggtt_largest_hole(struct xe_ggtt *ggtt, u64 alignment, u64 *spare);

drivers/gpu/drm/xe/xe_gt_pagefault.c +0 -0
index ab43dec52776..2c7f10cc423f 100644
@@ -94,12 +94,12 @@ static int xe_pf_begin(struct drm_exec *exec, struct xe_vma *vma,
  		}


  /* Migrate to VRAM, move should invalidate the VMA first */

drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -1
-		err = xe_bo_migrate(bo, vram->placement);
+		err = xe_bo_migrate(bo, vram->placement, exec);

  if (err)
  return err;
  } else if (bo) {
  /* Create backing store if needed */

drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -1
-		err = xe_bo_validate(bo, vm, true);
+		err = xe_bo_validate(bo, vm, true, exec);

  if (err)
  return err;
  }

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +1 -0
index c8f0320d032f..906011671b60 100644
@@ -1452,6 +1452,7 @@ static bool pf_release_vf_config_lmem(struct xe_gt *gt, struct xe_gt_sriov_confi
  static int pf_provision_vf_lmem(struct xe_gt *gt, unsigned int vfid, u64 size)
  {
  	struct xe_gt_sriov_config *config = pf_pick_vf_config(gt, vfid);
+	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
  	struct xe_device *xe = gt_to_xe(gt);
  	struct xe_tile *tile = gt_to_tile(gt);
  	struct xe_bo *bo;
@@ -1484,11 +1485,12 @@ static int pf_provision_vf_lmem(struct xe_gt *gt, unsigned int vfid, u64 size)

  XE_BO_FLAG_VRAM_IF_DGFX(tile) | XE_BO_FLAG_NEEDS_2M | XE_BO_FLAG_PINNED |

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +2 -1
-				 XE_BO_FLAG_PINNED_LATE_RESTORE);
+				 XE_BO_FLAG_PINNED_LATE_RESTORE,
+				 exec);

  if (IS_ERR(bo))
  return PTR_ERR(bo);

drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +1 -1
-	err = xe_bo_pin(bo);
+	err = xe_bo_pin(bo, exec);

  xe_bo_unlock(bo);
  if (unlikely(err)) {
  xe_bo_put(bo);

drivers/gpu/drm/xe/xe_svm.c +1 -0
index e35c6d4def20..39e3aa6df25a 100644
@@ -700,6 +700,7 @@ static int xe_drm_pagemap_populate_mm(struct drm_pagemap *dpagemap,
  	struct device *dev = xe->drm.dev;
  	struct drm_buddy_block *block;
  	struct list_head *blocks;
+	struct drm_exec *exec;
  	struct xe_bo *bo;
  	ktime_t time_end = 0;
  	int err, idx;
@@ -708,12 +709,13 @@ static int xe_drm_pagemap_populate_mm(struct drm_pagemap *dpagemap,
  		return -ENODEV;


  xe_pm_runtime_get(xe);

drivers/gpu/drm/xe/xe_svm.c +1 -0
+	exec = XE_VALIDATION_UNIMPLEMENTED;


  retry:
  bo = xe_bo_create_locked(vr->xe, NULL, NULL, end - start, ttm_bo_type_device,
  (IS_DGFX(xe) ? XE_BO_FLAG_VRAM(vr) : XE_BO_FLAG_SYSTEM) |

drivers/gpu/drm/xe/xe_svm.c +1 -1
-				 XE_BO_FLAG_CPU_ADDR_MIRROR);
+				 XE_BO_FLAG_CPU_ADDR_MIRROR, exec);

  if (IS_ERR(bo)) {
  err = PTR_ERR(bo);
  if (xe_vm_validate_should_retry(NULL, err, &time_end))

drivers/gpu/drm/xe/xe_validation.c new file +49 -0
new file mode 100644
index 000000000000..cc0684d24e02
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+#include "xe_bo.h"
+#include <drm/drm_exec.h>
+#include <drm/drm_gem.h>
+
+#include "xe_assert.h"
+#include "xe_validation.h"
+
+#ifdef CONFIG_DRM_XE_DEBUG
+/**
+ * xe_validation_assert_exec() - Assert that the drm_exec pointer is suitable
+ * for validation.
+ * @xe: Pointer to the xe device.
+ * @exec: The drm_exec pointer to check.
+ * @obj: Pointer to the object subject to validation.
+ *
+ * NULL exec pointers are not allowed.
+ * For XE_VALIDATION_UNIMPLEMENTED, no checking.
+ * For XE_VLIDATION_OPT_OUT, check that the caller is a kunit test
+ * For XE_VALIDATION_UNSUPPORTED, check that the object subject to
+ * validation is a dma-buf, for which support for ww locking is
+ * not in place in the dma-buf layer.
+ */
+void xe_validation_assert_exec(const struct xe_device *xe,
+			       const struct drm_exec *exec,
+			       const struct drm_gem_object *obj)
+{
+	xe_assert(xe, exec);
+	if (IS_ERR(exec)) {
+		switch (PTR_ERR(exec)) {
+		case __XE_VAL_UNIMPLEMENTED:
+			break;
+		case __XE_VAL_UNSUPPORTED:
+			xe_assert(xe, !!obj->dma_buf);
+			break;
+#if IS_ENABLED(CONFIG_KUNIT)
+		case __XE_VAL_OPT_OUT:
+			xe_assert(xe, current->kunit_test);
+			break;
+#endif
+		default:
+			xe_assert(xe, false);
+		}
+	}
+}
+#endif
drivers/gpu/drm/xe/xe_validation.h new file +69 -0
new file mode 100644
index 000000000000..db50feacad7a
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+#ifndef _XE_VALIDATION_H_
+#define _XE_VALIDATION_H_
+
+#include <linux/dma-resv.h>
+#include <linux/types.h>
+
+struct drm_exec;
+struct drm_gem_object;
+struct xe_device;
+
+#ifdef CONFIG_PROVE_LOCKING
+/**
+ * xe_validation_lockdep() - Assert that a drm_exec locking transaction can
+ * be initialized at this point.
+ */
+static inline void xe_validation_lockdep(void)
+{
+	struct ww_acquire_ctx ticket;
+
+	ww_acquire_init(&ticket, &reservation_ww_class);
+	ww_acquire_fini(&ticket);
+}
+#else
+static inline void xe_validation_lockdep(void)
+{
+}
+#endif
+
+/*
+ * Various values of the drm_exec pointer where we've not (yet)
+ * implemented full ww locking.
+ *
+ * XE_VALIDATION_UNIMPLEMENTED means implementation is pending.
+ * A lockdep check is made to assure that a drm_exec locking
+ * transaction can actually take place where the macro is
+ * used. If this asserts, the exec pointer needs to be assigned
+ * higher up in the callchain and passed down.
+ *
+ * XE_VALIDATION_UNSUPPORTED is for dma-buf code only where
+ * the dma-buf layer doesn't support WW locking.
+ *
+ * XE_VALIDATION_OPT_OUT is for simplification of kunit tests where
+ * exhaustive eviction isn't necessary.
+ */
+#define __XE_VAL_UNIMPLEMENTED -EINVAL
+#define XE_VALIDATION_UNIMPLEMENTED (xe_validation_lockdep(),		\
+				     (struct drm_exec *)ERR_PTR(__XE_VAL_UNIMPLEMENTED))
+
+#define __XE_VAL_UNSUPPORTED -EOPNOTSUPP
+#define XE_VALIDATION_UNSUPPORTED ((struct drm_exec *)ERR_PTR(__XE_VAL_UNSUPPORTED))
+
+#define __XE_VAL_OPT_OUT -ENOMEM
+#define XE_VALIDATION_OPT_OUT (xe_validation_lockdep(), \
+			       (struct drm_exec *)ERR_PTR(__XE_VAL_OPT_OUT))
+#ifdef CONFIG_DRM_XE_DEBUG
+void xe_validation_assert_exec(const struct xe_device *xe, const struct drm_exec *exec,
+			       const struct drm_gem_object *obj);
+#else
+#define xe_validation_assert_exec(_xe, _exec, _obj)	\
+	do {						\
+		(void)_xe; (void)_exec; (void)_obj;	\
+	} while (0)
+#endif
+
+#endif
drivers/gpu/drm/xe/xe_vm.c +0 -0
index 12e661960244..600aaadb4bee 100644
@@ -393,7 +393,7 @@ static int xe_gpuvm_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec)
  		list_move_tail(&gpuva_to_vma(gpuva)->combined_links.rebind,
  			       &vm->rebind_list);

drivers/gpu/drm/xe/xe_vm.c +1 -1
-	ret = xe_bo_validate(gem_to_xe_bo(vm_bo->obj), vm, false);
+	ret = xe_bo_validate(gem_to_xe_bo(vm_bo->obj), vm, false, exec);

  if (ret)
  return ret;

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -451,6 +451,7 @@ static int xe_preempt_work_begin(struct drm_exec *exec, struct xe_vm *vm,
  	if (err)
  		return err;

drivers/gpu/drm/xe/xe_vm.c +1 -0
+	xe_vm_set_validation_exec(vm, exec);

  if (xe_vm_is_idle(vm)) { vm->preempt.rebind_deactivated = true; *done = true;

drivers/gpu/drm/xe/xe_vm.c +1 -0
@@ -516,6 +517,7 @@ static void preempt_rebind_work_func(struct work_struct *w)
  		err = xe_preempt_work_begin(&exec, vm, &done);
  		drm_exec_retry_on_contention(&exec);
  		if (err || done) {
+			xe_vm_set_validation_exec(vm, NULL);
  			drm_exec_fini(&exec);
  			if (err && xe_vm_validate_should_retry(&exec, err, &end))
  				err = -EAGAIN;
@@ -565,6 +567,7 @@ static void preempt_rebind_work_func(struct work_struct *w)
  	up_read(&vm->userptr.notifier_lock);


 out_unlock:

drivers/gpu/drm/xe/xe_vm.c +1 -0
+	xe_vm_set_validation_exec(vm, NULL);

  drm_exec_fini(&exec);
 out_unlock_outer:
  if (err == -EAGAIN) {

drivers/gpu/drm/xe/xe_vm.c int xe_vm_lock_vma(struct drm_exec *exec, struct xe_vma *vma) · line 1382

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +2 -0
@@ -1375,6 +1378,8 @@ int xe_vm_lock_vma(struct drm_exec *exec, struct xe_vma *vma)
  	err = drm_exec_lock_obj(exec, xe_vm_obj(vm));
  	if (!err && bo && !bo->vm)
  		err = drm_exec_lock_obj(exec, &bo->ttm.base);
+	if (!err)
+		xe_vm_set_validation_exec(vm, exec);

Do you have imbalance here? I see this function called in xe_pf_begin and xe_vma_destroy_unlocked but I don't see xe_vm_set_validation_exec(vm, NULL) called.

Quoted 131 lines


  return err;
 }

drivers/gpu/drm/xe/xe_vm.c +1 -1
@@ -2889,7 +2894,7 @@ static int vma_lock_and_validate(struct drm_exec *exec, struct xe_vma *vma,
  			err = drm_exec_lock_obj(exec, &bo->ttm.base);
  		if (!err && validate)
  			err = xe_bo_validate(bo, vm,
-					     !xe_vm_in_preempt_fence_mode(vm));
+					     !xe_vm_in_preempt_fence_mode(vm), exec);
  	}


  return err;

drivers/gpu/drm/xe/xe_vm.c +2 -1
@@ -3012,7 +3017,8 @@ static int op_lock_and_prep(struct drm_exec *exec, struct xe_vm *vm,
  					    false);
  		if (!err && !xe_vma_has_no_bo(vma))
  			err = xe_bo_migrate(xe_vma_bo(vma),
-					    region_to_mem_type[region]);
+					    region_to_mem_type[region],
+					    exec);
  		break;
  	}

  default:

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -3052,6 +3058,7 @@ static int vm_bind_ioctl_ops_lock_and_prep(struct drm_exec *exec,
  	if (err)
  		return err;

drivers/gpu/drm/xe/xe_vm.c +1 -0
+	xe_vm_set_validation_exec(vm, exec);

  list_for_each_entry(op, &vops->list, link) { err = op_lock_and_prep(exec, vm, op); if (err)

drivers/gpu/drm/xe/xe_vm.c +9 -1
@@ -3850,10 +3857,18 @@ struct dma_fence *xe_vm_bind_kernel_bo(struct xe_vm *vm, struct xe_bo *bo,
   */
  int xe_vm_lock(struct xe_vm *vm, bool intr)
  {
+	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	int ret;
+
  	if (intr)
-		return dma_resv_lock_interruptible(xe_vm_resv(vm), NULL);
+		ret = dma_resv_lock_interruptible(xe_vm_resv(vm), NULL);
+	else
+		ret = dma_resv_lock(xe_vm_resv(vm), NULL);
+
+	if (!ret)
+		xe_vm_set_validation_exec(vm, exec);

drivers/gpu/drm/xe/xe_vm.c +1 -1
-	return dma_resv_lock(xe_vm_resv(vm), NULL);
+	return ret;

 }

 /**

drivers/gpu/drm/xe/xe_vm.c +1 -0
@@ -3864,6 +3879,7 @@ int xe_vm_lock(struct xe_vm *vm, bool intr)
   */
  void xe_vm_unlock(struct xe_vm *vm)
  {
+	xe_vm_set_validation_exec(vm, NULL);
  	dma_resv_unlock(xe_vm_resv(vm));
  }

drivers/gpu/drm/xe/xe_vm.h +1 -1
index 2ecb417c19a2..4ba26eed7e96 100644
@@ -321,7 +321,7 @@ static inline void xe_vm_set_validating(struct xe_vm *vm, bool allow_res_evict)
  	if (vm && !allow_res_evict) {
  		xe_vm_assert_held(vm);
  		/* Pairs with READ_ONCE in xe_vm_is_validating() */
-		WRITE_ONCE(vm->validating, current);
+		WRITE_ONCE(vm->validation.validating, current);
  	}
  }

drivers/gpu/drm/xe/xe_vm.h +1 -1
@@ -339,7 +339,7 @@ static inline void xe_vm_clear_validating(struct xe_vm *vm, bool allow_res_evict
  {
  	if (vm && !allow_res_evict) {
  		/* Pairs with READ_ONCE in xe_vm_is_validating() */
-		WRITE_ONCE(vm->validating, NULL);
+		WRITE_ONCE(vm->validation.validating, NULL);
  	}
  }

drivers/gpu/drm/xe/xe_vm.h +1 -1
@@ -357,13 +357,40 @@ static inline void xe_vm_clear_validating(struct xe_vm *vm, bool allow_res_evict
  static inline bool xe_vm_is_validating(struct xe_vm *vm)
  {
  	/* Pairs with WRITE_ONCE in xe_vm_is_validating() */
-	if (READ_ONCE(vm->validating) == current) {
+	if (READ_ONCE(vm->validation.validating) == current) {
  		xe_vm_assert_held(vm);
  		return true;
  	}
  	return false;
  }

drivers/gpu/drm/xe/xe_vm.h +27 -0
+/**
+ * xe_vm_set_validation_exec() - Accessor to set the drm_exec object
+ * @vm: The vm we want to register a drm_exec object with.
+ * @exec: The exec object we want to register.
+ *
+ * Set the drm_exec object used to lock the vm's resv.
+ */
+static inline void xe_vm_set_validation_exec(struct xe_vm *vm, struct drm_exec *exec)
+{
+	xe_vm_assert_held(vm);
+	vm->validation._exec = exec;
+}
+
+/**
+ * xe_vm_set_validation_exec() - Accessor to read the drm_exec object
+ * @vm: The vm we want to register a drm_exec object with.
+ *
+ * Return: The drm_exec object used to lock the vm's resv. The value
+ * is a valid pointer, %NULL, or one of the special values defined in
+ * xe_validation.h.
+ */
+static inline struct drm_exec *xe_vm_validation_exec(struct xe_vm *vm)
+{
+	xe_vm_assert_held(vm);
+	return vm->validation._exec;
+}
+

 /**
  * xe_vm_has_valid_gpu_mapping() - Advisory helper to check if VMA or SVM range has
  * a valid GPU mapping

drivers/gpu/drm/xe/xe_vm_types.h +0 -0
index 8a07feef503b..2f88808e36bb 100644
@@ -312,19 +312,35 @@ struct xe_vm {
  		bool capture_once;
  	} error_capture;

drivers/gpu/drm/xe/xe_vm_types.h struct xe_vm { · line 320

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm_types.h +6 -0
+	/**
+	 * @validation: Validation data only valid with the vm resv held.
+	 * Note: This is really task state of the task holding the vm resv,
+	 * and moving forward we should
+	 * come up with a better way of passing this down the call-
+	 * chain.

I've already mentioned this, attaching the _exec xe_vma_ops might be good option as xe_vma_ops has lifetime of only existing for the bind (i.e., it is stack variable) so you'd only need to set it (i.e., no clear required).

I think patch largely makes sense.

Matt

Quoted 37 lines
drivers/gpu/drm/xe/xe_vm_types.h +18 -0
+	 */
+	struct {
+		/**
+		 * @validation.validating: The task that is currently making bos resident.
+		 * for this vm.
+		 * Protected by the VM's resv for writing. Opportunistic reading can be done
+		 * using READ_ONCE. Note: This is a workaround for the
+		 * TTM eviction_valuable() callback not being passed a struct
+		 * ttm_operation_context(). Future work might want to address this.
+		 */
+		struct task_struct *validating;
+		/**
+		 *  @validation.exec The drm_exec context used when locking the vm resv.
+		 *  Protected by the vm's resv.
+		 */
+		struct drm_exec *_exec;
+	} validation;
+

  /**
  * @tlb_flush_seqno: Required TLB flush seqno for the next exec.
  * protected by the vm resv.
  */
  u64 tlb_flush_seqno;

drivers/gpu/drm/xe/xe_vm_types.h +0 -8
-	/**
-	 * @validating: The task that is currently making bos resident for this vm.
-	 * Protected by the VM's resv for writing. Opportunistic reading can be done
-	 * using READ_ONCE. Note: This is a workaround for the
-	 * TTM eviction_valuable() callback not being passed a struct
-	 * ttm_operation_context(). Future work might want to address this.
-	 */
-	struct task_struct *validating;

  /** @batch_invalidate_tlb: Always invalidate TLB before batch start */ bool batch_invalidate_tlb;
  /** @xef: XE file handle for tracking this VM's drm client */

Signature · 1 line
2.50.1
maintainer
#60 lore raw

Re: [PATCH 15/15] drm/xe: Convert pinned suspend eviction for exhaustive eviction

In reply to Thomas Hellström
Quoted 238 lines · Thomas Hellström

Pinned suspend eviction and preparation for eviction validates system memory for eviction buffers. Do that under a validation exclusive lock to avoid interfering with other processes validating system graphics memory.

---
 drivers/gpu/drm/xe/xe_bo.c | 205 +++++++++++++++++++------------------
 1 file changed, 108 insertions(+), 97 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +95 -88
index 82bf158426ad..efb9c88b6aa7 100644
@@ -1139,43 +1139,47 @@ long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
 int xe_bo_notifier_prepare_pinned(struct xe_bo *bo)
 {
 	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
 	struct xe_bo *backup;
 	int ret = 0;
 
-	xe_bo_lock(bo, false);
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, ret, true) {
+		ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
+		drm_exec_retry_on_contention(&exec);
+		xe_assert(xe, !ret);
+		xe_assert(xe, !bo->backup_obj);
 
-	xe_assert(xe, !bo->backup_obj);
+		/*
+		 * Since this is called from the PM notifier we might have raced with
+		 * someone unpinning this after we dropped the pinned list lock and
+		 * grabbing the above bo lock.
+		 */
+		if (!xe_bo_is_pinned(bo))
+			break;
 
-	/*
-	 * Since this is called from the PM notifier we might have raced with
-	 * someone unpinning this after we dropped the pinned list lock and
-	 * grabbing the above bo lock.
-	 */
-	if (!xe_bo_is_pinned(bo))
-		goto out_unlock_bo;
+		if (!xe_bo_is_vram(bo))
+			break;
 
-	if (!xe_bo_is_vram(bo))
-		goto out_unlock_bo;
+		if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
+			break;
 
-	if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
-		goto out_unlock_bo;
+		backup = xe_bo_init_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL, xe_bo_size(bo),
+					   DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,
+					   XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS |
+					   XE_BO_FLAG_PINNED, &exec);
+		if (IS_ERR(backup)) {
+			drm_exec_retry_on_contention(&exec);
+			ret = PTR_ERR(backup);
+			xe_validation_retry_on_oom(&ctx, &ret);
+			break;
+		}
 
-	backup = xe_bo_init_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL, xe_bo_size(bo),
-				   DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,
-				   XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS |
-				   XE_BO_FLAG_PINNED, exec);
-	if (IS_ERR(backup)) {
-		ret = PTR_ERR(backup);
-		goto out_unlock_bo;
+		backup->parent_obj = xe_bo_get(bo); /* Released by bo_destroy */
+		ttm_bo_pin(&backup->ttm);
+		bo->backup_obj = backup;
 	}
 
-	backup->parent_obj = xe_bo_get(bo); /* Released by bo_destroy */
-	ttm_bo_pin(&backup->ttm);
-	bo->backup_obj = backup;
-
-out_unlock_bo:
-	xe_bo_unlock(bo);
 	return ret;
 }
 
@@ -1215,99 +1219,106 @@ int xe_bo_notifier_unprepare_pinned(struct xe_bo *bo)
 int xe_bo_evict_pinned(struct xe_bo *bo)
 {
 	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
 	struct xe_bo *backup = bo->backup_obj;
 	bool backup_created = false;
 	bool unmap = false;
 	int ret = 0;
 
-	xe_bo_lock(bo, false);
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, ret, true) {
+		ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
+		drm_exec_retry_on_contention(&exec);
+		xe_assert(xe, !ret);
 
-	if (WARN_ON(!bo->ttm.resource)) {
-		ret = -EINVAL;
-		goto out_unlock_bo;
-	}
+		if (WARN_ON(!bo->ttm.resource)) {
+			ret = -EINVAL;
+			break;
+		}
 
-	if (WARN_ON(!xe_bo_is_pinned(bo))) {
-		ret = -EINVAL;
-		goto out_unlock_bo;
-	}
+		if (WARN_ON(!xe_bo_is_pinned(bo))) {
+			ret = -EINVAL;
+			break;
+		}
 
-	if (!xe_bo_is_vram(bo))
-		goto out_unlock_bo;
+		if (!xe_bo_is_vram(bo))
+			break;
 
-	if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
-		goto out_unlock_bo;
+		if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
+			break;
 
-	if (!backup) {
-		backup = xe_bo_init_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL, xe_bo_size(bo),
-					   DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,
-					   XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS |
-					   XE_BO_FLAG_PINNED, exec);
-		if (IS_ERR(backup)) {
-			ret = PTR_ERR(backup);
-			goto out_unlock_bo;
+		if (!backup) {
+			backup = xe_bo_init_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL,
+						   xe_bo_size(bo),
+						   DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,
+						   XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS |
+						   XE_BO_FLAG_PINNED, &exec);
+			if (IS_ERR(backup)) {
+				drm_exec_retry_on_contention(&exec);
+				ret = PTR_ERR(backup);
+				xe_validation_retry_on_oom(&ctx, &ret);
+				break;
+			}
+			backup->parent_obj = xe_bo_get(bo); /* Released by bo_destroy */
+			backup_created = true;
 		}
-		backup->parent_obj = xe_bo_get(bo); /* Released by bo_destroy */
-		backup_created = true;
-	}
 
-	if (xe_bo_is_user(bo) || (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) {
-		struct xe_migrate *migrate;
-		struct dma_fence *fence;
-
-		if (bo->tile)
-			migrate = bo->tile->migrate;
-		else
-			migrate = mem_type_to_migrate(xe, bo->ttm.resource->mem_type);
+		if (xe_bo_is_user(bo) || (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) {
+			struct xe_migrate *migrate;
+			struct dma_fence *fence;
 
-		ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
-		if (ret)
-			goto out_backup;
+			if (bo->tile)
+				migrate = bo->tile->migrate;
+			else
+				migrate = mem_type_to_migrate(xe, bo->ttm.resource->mem_type);
 
-		ret = dma_resv_reserve_fences(backup->ttm.base.resv, 1);
-		if (ret)
-			goto out_backup;
+			ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
+			if (ret)
+				goto out_backup;
 
-		fence = xe_migrate_copy(migrate, bo, backup, bo->ttm.resource,
-					backup->ttm.resource, false);
-		if (IS_ERR(fence)) {
-			ret = PTR_ERR(fence);
-			goto out_backup;
-		}
+			ret = dma_resv_reserve_fences(backup->ttm.base.resv, 1);
+			if (ret)
+				goto out_backup;
 
-		dma_resv_add_fence(bo->ttm.base.resv, fence,
-				   DMA_RESV_USAGE_KERNEL);
-		dma_resv_add_fence(backup->ttm.base.resv, fence,
-				   DMA_RESV_USAGE_KERNEL);
-		dma_fence_put(fence);
-	} else {
-		ret = xe_bo_vmap(backup);
-		if (ret)
-			goto out_backup;
+			fence = xe_migrate_copy(migrate, bo, backup, bo->ttm.resource,
+						backup->ttm.resource, false);
+			if (IS_ERR(fence)) {
+				ret = PTR_ERR(fence);
+				goto out_backup;
+			}
 
-		if (iosys_map_is_null(&bo->vmap)) {
-			ret = xe_bo_vmap(bo);
+			dma_resv_add_fence(bo->ttm.base.resv, fence,
+					   DMA_RESV_USAGE_KERNEL);
+			dma_resv_add_fence(backup->ttm.base.resv, fence,
+					   DMA_RESV_USAGE_KERNEL);
+			dma_fence_put(fence);
+		} else {
+			ret = xe_bo_vmap(backup);
 			if (ret)
 				goto out_backup;
-			unmap = true;
-		}
 
-		xe_map_memcpy_from(xe, backup->vmap.vaddr, &bo->vmap, 0,
-				   xe_bo_size(bo));
-	}
+			if (iosys_map_is_null(&bo->vmap)) {
+				ret = xe_bo_vmap(bo);
+				if (ret)
+					goto out_vunmap;
+				unmap = true;
+			}

drivers/gpu/drm/xe/xe_bo.c int xe_bo_notifier_unprepare_pinned(struct xe_bo *bo) · line 1313

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +6 -2
 
-	if (!bo->backup_obj)
-		bo->backup_obj = backup;
+			xe_map_memcpy_from(xe, backup->vmap.vaddr, &bo->vmap, 0,
+					   xe_bo_size(bo));
+		}
 
+		if (!bo->backup_obj)
+			bo->backup_obj = backup;
+out_vunmap:

I just want to confirm that this is safe. The cleanup.h documentation discourages the use of goto because of scoping issues. I assume that since this label is within the scope of the guard, it is fine.

It might be worth adding a quick note in the validation guard’s kernel-doc mentioning that goto can be dangerous, explaining what is allowed, and perhaps referencing the cleanup.h documentation. I could see this being something developers might trip over.

Patch LGTM, though.

Matt

Quoted 21 lines
drivers/gpu/drm/xe/xe_bo.c +7 -7
+		xe_bo_vunmap(backup);
 out_backup:
-	xe_bo_vunmap(backup);
-	if (ret && backup_created)
-		xe_bo_put(backup);
-out_unlock_bo:
-	if (unmap)
-		xe_bo_vunmap(bo);
-	xe_bo_unlock(bo);
+		if (ret && backup_created)
+			xe_bo_put(backup);
+		if (unmap)
+			xe_bo_vunmap(bo);
+	}
+
 	return ret;
 }
 
Signature · 1 line
2.50.1
maintainer
#42 lore raw

Re: [PATCH 11/15] drm/xe: Convert xe_dma_buf.c for exhaustive eviction

In reply to Thomas Hellström
Quoted 31 lines · Thomas Hellström

Convert dma-buf migration to XE_PL_TT and dma-buf import to support exhaustive eviction, using xe_validation_guard(). It seems unlikely that the import would result in an -ENOMEM, but convert import anyway for completeness.

The dma-buf map_attachment() functionality unfortunately doesn't support passing a drm_exec, which means that foreign devices validating a dma-buf that we exported will not, unless they are xeKMD devices, participate in the exhaustive eviction scheme.

---
 drivers/gpu/drm/xe/xe_dma_buf.c | 59 +++++++++++++++++++++++----------
 1 file changed, 42 insertions(+), 17 deletions(-)
drivers/gpu/drm/xe/xe_dma_buf.c +3 -1
index 78a827d4e726..56df1d84df21 100644
@@ -163,16 +163,27 @@ static int xe_dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
 	struct xe_bo *bo = gem_to_xe_bo(obj);
 	bool reads =  (direction == DMA_BIDIRECTIONAL ||
 		       direction == DMA_FROM_DEVICE);
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
+	int ret = 0;
 
 	if (!reads)
 		return 0;
 

drivers/gpu/drm/xe/xe_dma_buf.c static int xe_dma_buf_begin_cpu_access(struct dma_buf *dma_buf, · line 178

Quoted 10 lines
drivers/gpu/drm/xe/xe_dma_buf.c +5 -4
 	/* Can we do interruptible lock here? */
-	xe_bo_lock(bo, false);
-	(void)xe_bo_migrate(bo, XE_PL_TT, exec);
-	xe_bo_unlock(bo);
-
+	xe_validation_guard(&ctx, &xe_bo_device(bo)->val, &exec, 0, ret, false) {
+		ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
+		drm_exec_retry_on_contention(&exec);
+		if (ret)
+			goto out;

Does this work? The label is out of scope, which I believe is a no-no with guards if I correctly understand the rules detailed in the cleanup.h kernel documentation.

Quoted 40 lines
drivers/gpu/drm/xe/xe_dma_buf.c +22 -10
+
+		ret = xe_bo_migrate(bo, XE_PL_TT, &exec);
+		drm_exec_retry_on_contention(&exec);
+		xe_validation_retry_on_oom(&ctx, &ret);
+	}
+out:
+	/* If we failed, cpu-access takes place in current placement. */
+	(void)ret;
 	return 0;
 }
 
@@ -211,24 +222,38 @@ xe_dma_buf_init_obj(struct drm_device *dev, struct xe_bo *storage,
 {
 	struct dma_resv *resv = dma_buf->resv;
 	struct xe_device *xe = to_xe_device(dev);
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_validation_ctx ctx;
+	struct drm_gem_object *dummy_obj;
+	struct drm_exec exec;
 	struct xe_bo *bo;
-	int ret;
-
-	dma_resv_lock(resv, NULL);
-	bo = ___xe_bo_create_locked(xe, storage, NULL, resv, NULL, dma_buf->size,
-				    0, /* Will require 1way or 2way for vm_bind */
-				    ttm_bo_type_sg, XE_BO_FLAG_SYSTEM, exec);
-	if (IS_ERR(bo)) {
-		ret = PTR_ERR(bo);
-		goto error;
+	int ret = 0;
+
+	dummy_obj = drm_gpuvm_resv_object_alloc(&xe->drm);
+	if (!dummy_obj)
+		return ERR_PTR(-ENOMEM);
+
+	dummy_obj->resv = resv;
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, ret, false) {
+		ret = drm_exec_lock_obj(&exec, dummy_obj);
+		drm_exec_retry_on_contention(&exec);
+		if (ret)

drivers/gpu/drm/xe/xe_dma_buf.c xe_dma_buf_init_obj(struct drm_device *dev, struct xe_bo *storage, · line 249

Quoted 10 lines
drivers/gpu/drm/xe/xe_dma_buf.c +10 -0
+			goto error;
+
+		bo = ___xe_bo_create_locked(xe, storage, NULL, resv, NULL, dma_buf->size,
+					    0, /* Will require 1way or 2way for vm_bind */
+					    ttm_bo_type_sg, XE_BO_FLAG_SYSTEM, &exec);
+		drm_exec_retry_on_contention(&exec);
+		if (IS_ERR(bo)) {
+			ret = PTR_ERR(bo);
+			xe_validation_retry_on_oom(&ctx, &ret);
+			goto error;

Same question / issue here with goto error label.

Matt

Quoted 15 lines
drivers/gpu/drm/xe/xe_dma_buf.c +2 -2
+		}
 	}
-	dma_resv_unlock(resv);
+	drm_gem_object_put(dummy_obj);
 
 	return &bo->ttm.base;
 
 error:
-	dma_resv_unlock(resv);
 	return ERR_PTR(ret);
 }
 
Signature · 1 line
2.50.1
author maintainer
#43 lore raw

Re: [PATCH 11/15] drm/xe: Convert xe_dma_buf.c for exhaustive eviction

Quoted 42 lines · Matthew Brost
Quoted 41 lines · Thomas Hellström

Convert dma-buf migration to XE_PL_TT and dma-buf import to support exhaustive eviction, using xe_validation_guard(). It seems unlikely that the import would result in an -ENOMEM, but convert import anyway for completeness.

The dma-buf map_attachment() functionality unfortunately doesn't support passing a drm_exec, which means that foreign devices validating a dma-buf that we exported will not, unless they are xeKMD devices, participate in the exhaustive eviction scheme.

---
 drivers/gpu/drm/xe/xe_dma_buf.c | 59 +++++++++++++++++++++++------
----

 1 file changed, 42 insertions(+), 17 deletions(-)
drivers/gpu/drm/xe/xe_dma_buf.c +0 -0
index 78a827d4e726..56df1d84df21 100644
@@ -163,16 +163,27 @@ static int xe_dma_buf_begin_cpu_access(struct
 dma_buf *dma_buf,
  	struct xe_bo *bo = gem_to_xe_bo(obj);

  bool reads =  (direction == DMA_BIDIRECTIONAL || direction == DMA_FROM_DEVICE);

drivers/gpu/drm/xe/xe_dma_buf.c +3 -1
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
+	int ret = 0;


  if (!reads)
  return 0;

  /* Can we do interruptible lock here? */

drivers/gpu/drm/xe/xe_dma_buf.c +1 -4
-	xe_bo_lock(bo, false);
-	(void)xe_bo_migrate(bo, XE_PL_TT, exec);
-	xe_bo_unlock(bo);
-
+	xe_validation_guard(&ctx, &xe_bo_device(bo)->val, &exec,

0, ret, false) {

Quoted 8 lines

drivers/gpu/drm/xe/xe_dma_buf.c static int xe_dma_buf_begin_cpu_access(struct dma_buf *dma_buf, · line 178

Quoted 4 lines
drivers/gpu/drm/xe/xe_dma_buf.c +4 -0
+		ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
+		drm_exec_retry_on_contention(&exec);
+		if (ret)
+			goto out;

Does this work? The label is out of scope, which I believe is a no-no with guards if I correctly understand the rules detailed in the cleanup.h kernel documentation.

Good point.

My impression is that it would work in this particular case, since we just exit a scope.

But since it obviously goes against the guidelines in cleanup.h we shouln't be using these and replace them with a break and an error check after the guard if needed.

I'll fix this up and look at other instances of this to.

Thanks,
Thomas

Quoted 80 lines
Quoted 55 lines
drivers/gpu/drm/xe/xe_dma_buf.c +7 -0
+
+		ret = xe_bo_migrate(bo, XE_PL_TT, &exec);
+		drm_exec_retry_on_contention(&exec);
+		xe_validation_retry_on_oom(&ctx, &ret);
+	}
+out:
+	/* If we failed, cpu-access takes place in current

placement. */

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
+	(void)ret;

  return 0;
 }

drivers/gpu/drm/xe/xe_dma_buf.c +3 -6
@@ -211,24 +222,38 @@ xe_dma_buf_init_obj(struct drm_device *dev,
 struct xe_bo *storage,
  {
  	struct dma_resv *resv = dma_buf->resv;
  	struct xe_device *xe = to_xe_device(dev);
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_validation_ctx ctx;
+	struct drm_gem_object *dummy_obj;
+	struct drm_exec exec;
  	struct xe_bo *bo;
-	int ret;
-
-	dma_resv_lock(resv, NULL);
-	bo = ___xe_bo_create_locked(xe, storage, NULL, resv, NULL,
 dma_buf->size,
-				    0, /* Will require 1way or

2way for vm_bind */

drivers/gpu/drm/xe/xe_dma_buf.c +0 -1
-				    ttm_bo_type_sg,

XE_BO_FLAG_SYSTEM, exec);

drivers/gpu/drm/xe/xe_dma_buf.c +8 -3
-	if (IS_ERR(bo)) {
-		ret = PTR_ERR(bo);
-		goto error;
+	int ret = 0;
+
+	dummy_obj = drm_gpuvm_resv_object_alloc(&xe->drm);
+	if (!dummy_obj)
+		return ERR_PTR(-ENOMEM);
+
+	dummy_obj->resv = resv;
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, ret, false)

{

drivers/gpu/drm/xe/xe_dma_buf.c +6 -0
+		ret = drm_exec_lock_obj(&exec, dummy_obj);
+		drm_exec_retry_on_contention(&exec);
+		if (ret)
+			goto error;
+
+		bo = ___xe_bo_create_locked(xe, storage, NULL,

resv, NULL, dma_buf->size,

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
+					    0, /* Will require

1way or 2way for vm_bind */

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
+					    ttm_bo_type_sg,

XE_BO_FLAG_SYSTEM, &exec);

drivers/gpu/drm/xe/xe_dma_buf.c xe_dma_buf_init_obj(struct drm_device *dev, struct xe_bo *storage, · line 249

Quoted 5 lines
drivers/gpu/drm/xe/xe_dma_buf.c +5 -0
+		drm_exec_retry_on_contention(&exec);
+		if (IS_ERR(bo)) {
+			ret = PTR_ERR(bo);
+			xe_validation_retry_on_oom(&ctx, &ret);
+			goto error;

Same question / issue here with goto error label.

Matt

Quoted 15 lines
drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
+		}

  }

drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
-	dma_resv_unlock(resv);
+	drm_gem_object_put(dummy_obj);


  return &bo->ttm.base;

 error:

drivers/gpu/drm/xe/xe_dma_buf.c +0 -1
-	dma_resv_unlock(resv);

  return ERR_PTR(ret);
 }

Signature · 1 line
2.50.1
author maintainer
#52 lore raw

Re: [PATCH 13/15] drm/xe: Convert xe_bo_create_pin_map_at() for exhaustive eviction

In reply to Matthew Brost
Quoted 252 lines · Matthew Brost
Quoted 242 lines · Thomas Hellström

Most users of xe_bo_create_pin_map_at() and xe_bo_create_pin_map_at_aligned() are not using the vm parameter, and that simplifies conversion. Introduce an xe_bo_create_pin_map_at_novm() function and make the _aligned() version static. Use xe_validation_guard() for conversion.

---
 .../compat-i915-headers/gem/i915_gem_stolen.h | 24 ++---- drivers/gpu/drm/xe/display/xe_fb_pin.c        | 45 +++++----- drivers/gpu/drm/xe/display/xe_plane_initial.c |  4 +- drivers/gpu/drm/xe/xe_bo.c                    | 83 ++++++++++++++-
----
 drivers/gpu/drm/xe/xe_bo.h                    | 13 +-- drivers/gpu/drm/xe/xe_eu_stall.c              |  6 +-

 6 files changed, 101 insertions(+), 74 deletions(-)
(quoted diff) +0 -0

headers/gem/i915_gem_stolen.h b/drivers/gpu/drm/xe/compat-i915- headers/gem/i915_gem_stolen.h
index 1ce1e9da975b..ab48635ddffa 100644

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -2
@@ -21,9 +21,7 @@ static inline int
 i915_gem_stolen_insert_node_in_range(struct xe_device *xe,
  						       u32 size,
 u32 align,
  						       u32 start,
 u32 end)
  {
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
  	struct xe_bo *bo;
-	int err;

  u32 flags = XE_BO_FLAG_PINNED | XE_BO_FLAG_STOLEN;
  if (start < SZ_4K)

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -0
@@ -34,25 +32,15 @@ static inline int
 i915_gem_stolen_insert_node_in_range(struct xe_device *xe,
  		start = ALIGN(start, align);
  	}

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -1
-	bo = xe_bo_create_locked_range(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -2
-				       NULL, size, start, end,
-				       ttm_bo_type_kernel, flags,

0, exec);

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -12
-	if (IS_ERR(bo)) {
-		err = PTR_ERR(bo);
-		bo = NULL;
-		return err;
-	}
-	err = xe_bo_pin(bo, exec);
-	xe_bo_unlock_vm_held(bo);
-
-	if (err) {
-		xe_bo_put(fb->bo);
-		bo = NULL;
-	}
+	bo = xe_bo_create_pin_map_at_novm(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -0
+					  size, start,

ttm_bo_type_kernel, flags,

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +3 -0
+					  false, 0, true);
+	if (IS_ERR(bo))
+		return PTR_ERR(bo);


  fb->bo = bo;

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -1
-	return err;
+	return 0;

 }

 static inline int i915_gem_stolen_insert_node(struct xe_device *xe,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -0
index 43c45344ea26..d46ff7ebb0a1 100644
@@ -102,29 +102,32 @@ static int __xe_pin_fb_vma_dpt(const struct
 intel_framebuffer *fb,
  				 XE_PAGE_SIZE);


  if (IS_DGFX(xe))

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0,

NULL,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						      dpt_size,

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_VRAM0 |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_GGTT |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -1
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size,

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						   true,
+						  

XE_BO_FLAG_VRAM0 |

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   alignment,

false);
  else

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0,

NULL,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						      dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_STOLEN |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_GGTT |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -1
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						   true,
+						  

XE_BO_FLAG_STOLEN |

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   alignment,

false);
  if (IS_ERR(dpt))

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0,

NULL,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						      dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_GGTT |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -1
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						   true,
+						  

XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   alignment,

false);
  if (IS_ERR(dpt))
  return PTR_ERR(dpt);

drivers/gpu/drm/xe/display/xe_plane_initial.c +0 -0
index 826ac3d578b7..79d00127caf4 100644
@@ -140,8 +140,8 @@ initial_plane_bo(struct xe_device *xe,
  			page_size);
  	size -= base;

drivers/gpu/drm/xe/display/xe_plane_initial.c +0 -1
-	bo = xe_bo_create_pin_map_at(xe, tile0, NULL, size,

phys_base,

drivers/gpu/drm/xe/display/xe_plane_initial.c +1 -1
-				     ttm_bo_type_kernel, flags);
+	bo = xe_bo_create_pin_map_at_novm(xe, tile0, size,

phys_base,

drivers/gpu/drm/xe/display/xe_plane_initial.c +1 -0
+					  ttm_bo_type_kernel,

flags, true, 0, false);
  if (IS_ERR(bo)) {
  drm_dbg(&xe->drm,
  "Failed to create bo phys_base=%pa size %u with flags %x: %li\n",

drivers/gpu/drm/xe/xe_bo.c +0 -0
index 23b28eeef59f..c9928d4ee5a0 100644
@@ -2253,29 +2253,20 @@ struct xe_bo *xe_bo_create_user(struct
 xe_device *xe,
  	return bo;
  }


-struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.c +0 -3
-				      struct xe_vm *vm,
-				      size_t size, u64 offset,
-				      enum ttm_bo_type type, u32

flags)

drivers/gpu/drm/xe/xe_bo.c +0 -2
-{
-	return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size,

offset,

drivers/gpu/drm/xe/xe_bo.c +0 -4
-					       type, flags, 0);
-}
-
-struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device

*xe,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-					      struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.c +0 -2
-					      struct xe_vm *vm,
-					      size_t size, u64

offset,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-					      enum ttm_bo_type

type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +1 -1
-					      u64 alignment)
+static struct xe_bo *xe_bo_create_pin_map_at_aligned(struct

xe_device *xe,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     struct

xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     struct xe_vm

*vm,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     size_t size,

u64 offset,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     enum

ttm_bo_type type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     bool vmap,

u64 alignment,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     struct

drm_exec *exec)
 {
  struct xe_bo *bo;
  int err;
  u64 start = offset == ~0ull ? 0 : offset; u64 end = offset == ~0ull ? offset : start + size;

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) :
Quoted 2 lines

XE_VALIDATION_UNIMPLEMENTED;

General comment for the series: should all BO-layer functions that allocate (or may allocate) memory include a lockdep assertion that the
xe_validation_device->val lock is held? We already have xe_validation_assert_exec in several places, which is similar, but IMO

Quoted 8 lines

it wouldn’t hurt to also assert xe_validation_device->val in the relevant driver paths. The new TTM manager functions are good candidates
as well. Consider adding a follow-up patch at the end of the series to
add these assertions once all allocation paths adhere to the new locking
model.

Good idea, although the val lock may go away when TTM matures, we can then perhaps replace that with just a lockdep map.

/Thomas

Quoted 172 lines

Matt

Quoted 169 lines
drivers/gpu/drm/xe/xe_bo.c +1 -1
-	if (flags & XE_BO_FLAG_STOLEN &&
+	if (flags & XE_BO_FLAG_STOLEN && vmap &&

      xe_ttm_stolen_cpu_access_needs_ggtt(xe)) flags |= XE_BO_FLAG_GGTT;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2289,9 +2280,11 @@ struct xe_bo
 *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
  	if (err)
  		goto err_put;

drivers/gpu/drm/xe/xe_bo.c +5 -3
-	err = xe_bo_vmap(bo);
-	if (err)
-		goto err_unpin;
+	if (vmap) {
+		err = xe_bo_vmap(bo);
+		if (err)
+			goto err_unpin;
+	}


  xe_bo_unlock_vm_held(bo);

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2305,11 +2298,59 @@ struct xe_bo
 *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
  	return ERR_PTR(err);
  }


+/**
+ * xe_bo_create_pin_map_at_novm() - Create pinned and mapped bo at optional VRAM offset
+ * @xe: The xe device.
+ * @tile: The tile to select for migration of this bo, and the tile used for
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @size: The storage size to use for the bo.
+ * @offset: Optional VRAM offset or %0 for don't care.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @vmap: Whether to create a buffer object map.
+ * @alignment: GGTT alignment.
+ * @intr: Whether to execut any waits for backing store interruptible.
+ *
+ * Create a pinned and optionally mapped bo with VRAM offset and GGTT alignment
+ * options. The bo will be external and not associated with a VM.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set
+ * to true on entry.
+ */

drivers/gpu/drm/xe/xe_bo.c +2 -0
+struct xe_bo *
+xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+			     size_t size, u64 offset, enum

ttm_bo_type type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +3 -0
+			     bool vmap, u64 alignment, bool intr)
+{
+	u32 drm_exec_flags = intr ? DRM_EXEC_INTERRUPTIBLE_WAIT :

0;

drivers/gpu/drm/xe/xe_bo.c +6 -0
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
+	struct xe_bo *bo;
+	int ret = 0;
+
+	xe_validation_guard(&ctx, &xe->val, &exec, drm_exec_flags,

ret, false) {

drivers/gpu/drm/xe/xe_bo.c +1 -0
+		bo = xe_bo_create_pin_map_at_aligned(xe, tile,

NULL, size, offset,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     type, flags,

vmap,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     alignment,

&exec);

drivers/gpu/drm/xe/xe_bo.c +10 -0
+		drm_exec_retry_on_contention(&exec);
+		if (IS_ERR(bo)) {
+			ret = PTR_ERR(bo);
+			xe_validation_retry_on_oom(&ctx, &ret);
+		}
+	}
+
+	return ret ? ERR_PTR(ret) : bo;
+}
+

 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     struct xe_vm *vm, size_t size, enum ttm_bo_type type, u32 flags)
 {

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	return xe_bo_create_pin_map_at(xe, tile, vm, size, ~0ull,

type, flags);

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) :

XE_VALIDATION_UNIMPLEMENTED;

drivers/gpu/drm/xe/xe_bo.c +2 -0
+
+	return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size,

~0ull, type, flags,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+					       true, 0, exec);

 }

 static void __xe_bo_unpin_map_no_vm(void *arg)

drivers/gpu/drm/xe/xe_bo.h +0 -0
index a625806deeb6..d06266af9662 100644
@@ -109,15 +109,10 @@ struct xe_bo *xe_bo_create_user(struct

xe_device *xe, struct xe_vm *vm, size_t s
 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     struct xe_vm *vm, size_t size, enum ttm_bo_type type, u32 flags);
-struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-				      struct xe_vm *vm, size_t

size, u64 offset,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-				      enum ttm_bo_type type, u32

flags);
-struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-					      struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.h +0 -2
-					      struct xe_vm *vm,
-					      size_t size, u64

offset,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-					      enum ttm_bo_type

type, u32 flags,

drivers/gpu/drm/xe/xe_bo.h +2 -1
-					      u64 alignment);
+struct xe_bo *
+xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.h +1 -0
+			     size_t size, u64 offset, enum

ttm_bo_type type,

drivers/gpu/drm/xe/xe_bo.h +1 -0
+			     u32 flags, bool vmap, u64 alignment,

bool intr);
 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     size_t size, u32 flags);
 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_eu_stall.c +0 -0
index fdd514fec5ef..afabfc125488 100644
@@ -617,9 +617,9 @@ static int xe_eu_stall_data_buf_alloc(struct
 xe_eu_stall_data_stream *stream,


  size = stream->per_xecore_buf_size * last_xecore;

drivers/gpu/drm/xe/xe_eu_stall.c +0 -2
-	bo = xe_bo_create_pin_map_at_aligned(tile->xe, tile, NULL,
-					     size, ~0ull,

ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_eu_stall.c +0 -1
-					     XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_GGTT, SZ_64);

drivers/gpu/drm/xe/xe_eu_stall.c +1 -0
+	bo = xe_bo_create_pin_map_at_novm(tile->xe, tile, size,

~0ull, ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_eu_stall.c +1 -0
+					  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_GGTT, true,

drivers/gpu/drm/xe/xe_eu_stall.c +1 -0
+					  SZ_64, false);

  if (IS_ERR(bo)) {
  kfree(stream->xecore_buf);
  return PTR_ERR(bo);

Signature · 1 line
2.50.1
author maintainer
#16 lore raw

Re: [PATCH 05/15] drm/xe: Introduce an xe_validation wrapper around drm_exec

In reply to Matthew Brost
Quoted 177 lines · Matthew Brost
Quoted 140 lines · Thomas Hellström

Introduce a validation wrapper xe_validation_guard() as a helper intended to be used around drm_exec transactions what perform validations. Once TTM can handle exhaustive eviction we could remove this wrapper or make it mostly a NO-OP unless other functionality is added to it.

Currently the wrapper takes a read lock upon entry and if the transaction hits an OOM, all locks are released and the transaction is retried with a write-lock. If all other validations participate in this scheme, the transaction with the write lock will be the only transaction validating and should have access to all available non-pinned memory.

There is currently a problem in that TTM converts -EDEADLOCKS to

(quoted diff) +0 -2
-ENOMEM, and with ww_mutex slowpath error injections, we can hit
-ENOMEMs without having actually ran out of memory. We abuse

ww_mutex internals to detect such situations until TTM is fixes to not convert the error code. In the meantime, injecting ww_mutex slowpath -EDEADLOCKs is a good way to test the implementation in the absence of real OOMs.

Just introduce the wrapper in this commit. It will be hooked up to the driver in following commits.

---
 drivers/gpu/drm/xe/xe_validation.c | 199
+++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_validation.h | 107 ++++++++++++++++

 2 files changed, 306 insertions(+)
drivers/gpu/drm/xe/xe_validation.c +1 -0
index cc0684d24e02..cd1424f04237 100644
@@ -5,6 +5,7 @@
  #include "xe_bo.h"
  #include <drm/drm_exec.h>
  #include <drm/drm_gem.h>
+#include <drm/drm_gpuvm.h>


 #include "xe_assert.h"
 #include "xe_validation.h"

drivers/gpu/drm/xe/xe_validation.c +0 -0
@@ -47,3 +48,201 @@ void xe_validation_assert_exec(const struct
 xe_device *xe,
  	}
  }

 #endif

drivers/gpu/drm/xe/xe_validation.c +43 -0
+
+static int xe_validation_lock(struct xe_validation_ctx *ctx)
+{
+	struct xe_validation_device *val = ctx->val;
+	int ret = 0;
+
+	if (ctx->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) {
+		if (ctx->request_exclusive)
+			ret = down_write_killable(&val->lock);
+		else
+			ret = down_read_interruptible(&val->lock);
+	} else {
+		if (ctx->request_exclusive)
+			down_write(&val->lock);
+		else
+			down_read(&val->lock);
+	}
+
+	if (!ret) {
+		ctx->lock_held = true;
+		ctx->lock_held_exclusive = ctx->request_exclusive;
+	}
+
+	return ret;
+}
+
+static void xe_validation_unlock(struct xe_validation_ctx *ctx)
+{
+	if (!ctx->lock_held)
+		return;
+
+	if (ctx->lock_held_exclusive)
+		up_write(&ctx->val->lock);
+	else
+		up_read(&ctx->val->lock);
+
+	ctx->lock_held = false;
+}
+
+/**
+ * xe_validation_ctx_init() - Initialize an xe_validation_ctx
+ * @ctx: The xe_validation_ctx to initialize.
+ * @val: The xe_validation_device representing the validation

domain.
+ * @exec: The struct drm_exec to use for the transaction.
+ * @flags: The flags to use for drm_exec initialization.
+ * @nr: The number of anticipated buffer object locks. Forwarded to
+ * drm_exec initialization.
+ * @exclusive: Whether to use exclusive locking already on first validation.
+ *
+ * Initialize and lock a an xe_validation transaction using the validation domain
+ * represented by @val. Also initialize the drm_exec object forwarding
+ * @flags and @nr to the drm_exec initialization. The @exclusive parameter should
+ * typically be set to false to avoid locking out other validators from the
+ * domain until an OOM is hit. For testing- or final attempt purposes it can,
+ * however, be set to true.
+ *
+ * Return: %0 on success, %-EINTR if interruptible initial locking failed with a
+ * signal pending.
+ */
+int xe_validation_ctx_init(struct xe_validation_ctx *ctx, struct xe_validation_device *val,

drivers/gpu/drm/xe/xe_validation.c +1 -0
+			   struct drm_exec *exec, u32 flags,

unsigned int nr,

drivers/gpu/drm/xe/xe_validation.c +19 -0
+			   bool exclusive)
+{
+	int ret;
+
+	ctx->exec = exec;
+	ctx->val = val;
+	ctx->lock_held = false;
+	ctx->lock_held_exclusive = false;
+	ctx->request_exclusive = exclusive;
+	ctx->flags = flags;
+	ctx->nr = nr;
+
+	ret = xe_validation_lock(ctx);
+	if (ret)
+		return ret;
+
+	drm_exec_init(exec, flags, nr);
+
+	return 0;

drivers/gpu/drm/xe/xe_validation.c void xe_validation_assert_exec(const struct xe_device *xe, · line 140

Quoted 10 lines
drivers/gpu/drm/xe/xe_validation.c +10 -0
+}
+
+#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
+/*
+ * This abuses both drm_exec and ww_mutex internals and should be
+ * replaced by checking for -EDEADLK when we can make TTM
+ * stop converting -EDEADLK to -ENOMEM.
+ * An alternative is to not have exhaustive eviction with
+ * CONFIG_DEBUG_WW_MUTEX_SLOWPATH until that happens.
+ */

I vote to keep this the way you have it and live with the abuse until TTM is updated.

Quoted 22 lines

+static bool xe_validation_contention_injected(struct drm_exec *exec)

drivers/gpu/drm/xe/xe_validation.c +7 -0
+{
+	return !!exec->ticket.contending_lock;
+}
+
+#else
+
+static bool xe_validation_contention_injected(struct drm_exec

*exec)

drivers/gpu/drm/xe/xe_validation.c +7 -0
+{
+	return false;
+}
+
+#endif
+
+static bool __xe_validation_should_retry(struct xe_validation_ctx

*ctx, int ret)

drivers/gpu/drm/xe/xe_validation.c +4 -0
+{
+	if (ret == -ENOMEM &&
+	    ((ctx->request_exclusive &&
+	      xe_validation_contention_injected(ctx->exec)) ||
Quoted 8 lines

drivers/gpu/drm/xe/xe_validation.c void xe_validation_assert_exec(const struct xe_device *xe, · line 161

Quoted 2 lines
drivers/gpu/drm/xe/xe_validation.c +2 -0
+	     !ctx->request_exclusive)) {
+		ctx->request_exclusive = true;

Can the locking across multiple GPUs fall when request_exclusive is held
and the GPUs are sharing dma-buffers? I suppose we'd need true WW locking throughout the stack (TTM) and a ticketed retry to guarantee foward progress.

Yes. Either that or widen the validation domain to be per driver rather than per device.
With the drm_exec retry, in addition dma-buf needs to support passing a drm_exec, but that otoh would be a layering violation. So I have historically advocated for abstracting the ww mutex transaction with a dma-buf locking context with callbacks rather than a drm_exec. Then drm_exec could derive from that and keep its functionality for gem objects.

Quoted 234 lines
Quoted 221 lines
drivers/gpu/drm/xe/xe_validation.c +8 -0
+		return true;
+	}
+
+	return false;
+}
+
+/**
+ * xe_validation_exec_lock() - Perform drm_gpuvm_exec_lock within

a validation
+ * transaction.
+ * @ctx: An uninitialized xe_validation_ctx.
+ * @vm_exec: An initialized struct vm_exec.
+ * @val: The validation domain.
+ *
+ * The drm_gpuvm_exec_lock() function internally initializes its drm_exec
+ * transaction and therefore doesn't lend itself very well to be using
+ * xe_validation_ctx_init(). Provide a helper that takes an uninitialized
+ * xe_validation_ctx and calls drm_gpuvm_exec_lock() with OOM retry.
+ *
+ * Return: %0 on success, negative error code on failure.
+ */

drivers/gpu/drm/xe/xe_validation.c +30 -0
+int xe_validation_exec_lock(struct xe_validation_ctx *ctx,
+			    struct drm_gpuvm_exec *vm_exec,
+			    struct xe_validation_device *val)
+{
+	int ret;
+
+	memset(ctx, 0, sizeof(*ctx));
+	ctx->exec = &vm_exec->exec;
+	ctx->flags = vm_exec->flags;
+	ctx->val = val;
+retry:
+	ret = xe_validation_lock(ctx);
+	if (ret)
+		return ret;
+
+	ret = drm_gpuvm_exec_lock(vm_exec);
+	if (ret) {
+		xe_validation_unlock(ctx);
+		if (__xe_validation_should_retry(ctx, ret))
+			goto retry;
+	}
+
+	return ret;
+}
+
+/**
+ * xe_validation_ctx_fini() - Finalize a validation transaction
+ * @ctx: The Validation transaction to finalize.
+ *
+ * Finalize a validation transaction and its related drm_exec

transaction.
+ */

drivers/gpu/drm/xe/xe_validation.c +8 -0
+void xe_validation_ctx_fini(struct xe_validation_ctx *ctx)
+{
+	drm_exec_fini(ctx->exec);
+	xe_validation_unlock(ctx);
+}
+
+/**
+ * xe_validation_should_retry() - Determine if a validation

transaction should retry
+ * @ctx: The validation transaction.
+ * @ret: Pointer to a return value variable.
+ *
+ * Determines whether a validation transaction should retry based on the
+ * internal transaction state and the return value pointed to by @ret.
+ * If a validation should be retried, the transaction is prepared for that,
+ * and the validation locked might be re-locked in exclusive mode, and *@ret
+ * is set to %0. If the re-locking errors, typically due to interruptible
+ * locking with signal pending, *@ret is instead set to -EINTR and the
+ * function returns %false.
+ *
+ * Return: %true if validation should be retried, %false otherwise.
+ */
+bool xe_validation_should_retry(struct xe_validation_ctx *ctx, int *ret)

drivers/gpu/drm/xe/xe_validation.c +5 -0
+{
+	if (__xe_validation_should_retry(ctx, *ret)) {
+		drm_exec_fini(ctx->exec);
+		*ret = 0;
+		if (ctx->request_exclusive != ctx-
Quoted 1 line

lock_held_exclusive) {

drivers/gpu/drm/xe/xe_validation.c +9 -0
+			xe_validation_unlock(ctx);
+			*ret = xe_validation_lock(ctx);
+		}
+		drm_exec_init(ctx->exec, ctx->flags, ctx->nr);
+		return !*ret;
+	}
+
+	return false;
+}
drivers/gpu/drm/xe/xe_validation.h +0 -0
index db50feacad7a..a708c260cf18 100644
@@ -7,9 +7,11 @@


 #include <linux/dma-resv.h>
 #include <linux/types.h>
+#include <linux/rwsem.h>

 struct drm_exec;
 struct drm_gem_object;
+struct drm_gpuvm_exec;
 struct xe_device;

 #ifdef CONFIG_PROVE_LOCKING

drivers/gpu/drm/xe/xe_validation.h +0 -0
@@ -66,4 +68,109 @@ void xe_validation_assert_exec(const struct

xe_device *xe, const struct drm_exec
  } while (0)
 #endif

+/**
+ * struct xe_validation_device - The domain for exhaustive eviction
+ * @lock: The lock used to exclude other processes from allocating graphics memory
+ *
+ * The struct xe_validation_device represents the domain for which we want to use
+ * exhaustive eviction. The @lock is typically grabbed in read mode for allocations
+ * but when graphics memory allocation fails, it is retried with the write mode held.
+ */

drivers/gpu/drm/xe/xe_validation.h +6 -0
+struct xe_validation_device {
+	struct rw_semaphore lock;
+};
+
+/**
+ * struct xe_validation_ctx - A struct drm_exec subclass with

support for
+ * exhaustive eviction
+ * @exec: The drm_exec object base class. Note that we use a pointer instead of
+ * embedding to avoid diamond inheritance.
+ * @val: The exhaustive eviction domain.
+ * @lock_held: Whether The domain lock is currently held.
+ * @lock_held_exclusive: Whether the domain lock is held in exclusive mode.
+ * @request_exclusive: Whether to lock exclusively (write mode) the next time
+ * the domain lock is locked.
+ * @flags: The drm_exec flags used for drm_exec (re- )initialization.
+ * @nr: The drm_exec nr parameter used for drm_exec (re- )initializaiton.
+ */

drivers/gpu/drm/xe/xe_validation.h +11 -0
+struct xe_validation_ctx {
+	struct drm_exec *exec;
+	struct xe_validation_device *val;
+	bool lock_held;
+	bool lock_held_exclusive;
+	bool request_exclusive;
+	u32 flags;
+	unsigned int nr;
+};
+
+int xe_validation_ctx_init(struct xe_validation_ctx *ctx, struct

xe_validation_device *val,

drivers/gpu/drm/xe/xe_validation.h +1 -0
+			   struct drm_exec *exec, u32 flags,

unsigned int nr,

drivers/gpu/drm/xe/xe_validation.h +3 -0
+			   bool exclusive);
+
+int xe_validation_exec_lock(struct xe_validation_ctx *ctx, struct

drm_gpuvm_exec *vm_exec,

drivers/gpu/drm/xe/xe_validation.h +5 -0
+			    struct xe_validation_device *val);
+
+void xe_validation_ctx_fini(struct xe_validation_ctx *ctx);
+
+bool xe_validation_should_retry(struct xe_validation_ctx *ctx, int

*ret);

drivers/gpu/drm/xe/xe_validation.h +3 -0
+
+/**
+ * xe_validation_retry_on_oom() - Retry on oom in an xe_validaton

transaction
+ * @_ctx: Pointer to the xe_validation_ctx
+ * @_ret: The current error value possibly holding -ENOMEM
+ *
+ * Use this in way similar to drm_exec_retry_on_contention().
+ * If @_ret contains -ENOMEM the tranaction is restarted once in a way that
+ * blocks other transactions and allows exhastive eviction. If the transaction
+ * was already restarted once, Just return the -ENOMEM. May also set
+ * _ret to -EINTR if not retrying and waits are interruptible.
+ * May only be used within a drm_exec_until_all_locked() loop.
+ */
+#define xe_validation_retry_on_oom(_ctx,
_ret) \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+	do

{ \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+		if (xe_validation_should_retry(_ctx,

_ret)) \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+			goto

*__drm_exec_retry_ptr; \

drivers/gpu/drm/xe/xe_validation.h +4 -0
+	} while (0)
+
+/**
+ * xe_validation_device_init - Initialize a struct

xe_validation_device
+ * @val: The xe_validation_device to init.
+ */

drivers/gpu/drm/xe/xe_validation.h +3 -0
+static inline void
+xe_validation_device_init(struct xe_validation_device *val)
+{

drivers/gpu/drm/xe/xe_validation.h void xe_validation_assert_exec(const struct xe_device *xe, const struct drm_exec · line 151

Quoted 10 lines
drivers/gpu/drm/xe/xe_validation.h +10 -0
+	init_rwsem(&val->lock);
+}
+
+/*
+ * Make guard() and scoped_guard() work with xe_validation_ctx
+ * so that we can exit transactions without caring about the
+ * cleanup.
+ */
+DEFINE_CLASS(xe_validation, struct xe_validation_ctx *,
+	     if (!IS_ERR(_T)) xe_validation_ctx_fini(_T);,

I think this should be 'if (_T)', right?

Quoted 7 lines

drivers/gpu/drm/xe/xe_validation.h void xe_validation_assert_exec(const struct xe_device *xe, const struct drm_exec · line 153

Quoted 3 lines
drivers/gpu/drm/xe/xe_validation.h +1 -0
+	     ({_ret = xe_validation_ctx_init(_ctx, _val, _exec,

_flags, 0, _excl);

drivers/gpu/drm/xe/xe_validation.h +1 -0
+	       _ret ? NULL : _ctx; }),

Or here '_ret ? ERR_PTR(_ret) : _ctx;'

One or the other if I correctly understand how DEFINE_CLASS works.

Right. good catch.

Quoted 39 lines

Matt

Quoted 36 lines
drivers/gpu/drm/xe/xe_validation.h +1 -0
+	     struct xe_validation_ctx *_ctx, struct

xe_validation_device *_val,

drivers/gpu/drm/xe/xe_validation.h +1 -0
+	     struct drm_exec *_exec, u32 _flags, int _ret, bool

_excl);
+static inline void *class_xe_validation_lock_ptr(class_xe_validation_t *_T)

drivers/gpu/drm/xe/xe_validation.h +5 -0
+{return *_T; }
+#define class_xe_validation_is_conditional false
+
+/**
+ * xe_validation_guard() - An auto-cleanup xe_validation_ctx

transaction
+ * @_ctx: The xe_validation_ctx.
+ * @_val: The xe_validation_device.
+ * @_exec: The struct drm_exec object
+ * @_flags: Flags for the drm_exec transaction. See the struct drm_exec documention!
+ * @_ret: Return in / out parameter. May be set by this macro. Typicall 0 when called.
+ * @_excl: Whether to start in exclusive mode already in the first iteration.
+ *
+ * This macro is will initiate a drm_exec transaction with additional support for
+ * exhaustive eviction.
+ */
+#define xe_validation_guard(_ctx, _val, _exec, _flags, _ret,
_excl) \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+	scoped_guard(xe_validation, _ctx, _val, _exec, _flags,

_ret, _excl) \

drivers/gpu/drm/xe/xe_validation.h +2 -0
+	drm_exec_until_all_locked(_exec)
+

 #endif

Signature · 1 line
2.50.1
author maintainer
#41 lore raw

Re: [PATCH 11/15] drm/xe: Convert xe_dma_buf.c for exhaustive eviction

In reply to Matthew Brost
Quoted 50 lines · Matthew Brost
Quoted 49 lines · Thomas Hellström

Convert dma-buf migration to XE_PL_TT and dma-buf import to support exhaustive eviction, using xe_validation_guard(). It seems unlikely that the import would result in an -ENOMEM, but convert import anyway for completeness.

The dma-buf map_attachment() functionality unfortunately doesn't support passing a drm_exec, which means that foreign devices validating a dma-buf that we exported will not, unless they are xeKMD devices, participate in the exhaustive eviction scheme.

---
 drivers/gpu/drm/xe/xe_dma_buf.c | 59 +++++++++++++++++++++++------
----

 1 file changed, 42 insertions(+), 17 deletions(-)
drivers/gpu/drm/xe/xe_dma_buf.c +0 -0
index 78a827d4e726..56df1d84df21 100644
@@ -163,16 +163,27 @@ static int xe_dma_buf_begin_cpu_access(struct
 dma_buf *dma_buf,
  	struct xe_bo *bo = gem_to_xe_bo(obj);

  bool reads =  (direction == DMA_BIDIRECTIONAL || direction == DMA_FROM_DEVICE);

drivers/gpu/drm/xe/xe_dma_buf.c +3 -1
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
+	int ret = 0;


  if (!reads)
  return 0;

  /* Can we do interruptible lock here? */

drivers/gpu/drm/xe/xe_dma_buf.c +1 -4
-	xe_bo_lock(bo, false);
-	(void)xe_bo_migrate(bo, XE_PL_TT, exec);
-	xe_bo_unlock(bo);
-
+	xe_validation_guard(&ctx, &xe_bo_device(bo)->val, &exec,

0, ret, false) {

drivers/gpu/drm/xe/xe_dma_buf.c +8 -0
+		ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
+		drm_exec_retry_on_contention(&exec);
+		if (ret)
+			goto out;
+
+		ret = xe_bo_migrate(bo, XE_PL_TT, &exec);
+		drm_exec_retry_on_contention(&exec);
+		xe_validation_retry_on_oom(&ctx, &ret);
Quoted 8 lines

drivers/gpu/drm/xe/xe_dma_buf.c static int xe_dma_buf_begin_cpu_access(struct dma_buf *dma_buf, · line 186

Quoted 5 lines
drivers/gpu/drm/xe/xe_dma_buf.c +3 -0
+	}
+out:
+	/* If we failed, cpu-access takes place in current

placement. */

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
+	(void)ret;

Do you need the above line of code? I don't see this often in kernel code.

It's merely to annotate that we don't care about the returned value. But I can remove it.

/Thomas

Quoted 70 lines

Nit aside, patch LGTM.

Matt

Quoted 65 lines

  return 0;
 }

drivers/gpu/drm/xe/xe_dma_buf.c +3 -6
@@ -211,24 +222,38 @@ xe_dma_buf_init_obj(struct drm_device *dev,
 struct xe_bo *storage,
  {
  	struct dma_resv *resv = dma_buf->resv;
  	struct xe_device *xe = to_xe_device(dev);
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_validation_ctx ctx;
+	struct drm_gem_object *dummy_obj;
+	struct drm_exec exec;
  	struct xe_bo *bo;
-	int ret;
-
-	dma_resv_lock(resv, NULL);
-	bo = ___xe_bo_create_locked(xe, storage, NULL, resv, NULL,
 dma_buf->size,
-				    0, /* Will require 1way or

2way for vm_bind */

drivers/gpu/drm/xe/xe_dma_buf.c +0 -1
-				    ttm_bo_type_sg,

XE_BO_FLAG_SYSTEM, exec);

drivers/gpu/drm/xe/xe_dma_buf.c +8 -3
-	if (IS_ERR(bo)) {
-		ret = PTR_ERR(bo);
-		goto error;
+	int ret = 0;
+
+	dummy_obj = drm_gpuvm_resv_object_alloc(&xe->drm);
+	if (!dummy_obj)
+		return ERR_PTR(-ENOMEM);
+
+	dummy_obj->resv = resv;
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, ret, false)

{

drivers/gpu/drm/xe/xe_dma_buf.c +6 -0
+		ret = drm_exec_lock_obj(&exec, dummy_obj);
+		drm_exec_retry_on_contention(&exec);
+		if (ret)
+			goto error;
+
+		bo = ___xe_bo_create_locked(xe, storage, NULL,

resv, NULL, dma_buf->size,

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
+					    0, /* Will require

1way or 2way for vm_bind */

drivers/gpu/drm/xe/xe_dma_buf.c +1 -0
+					    ttm_bo_type_sg,

XE_BO_FLAG_SYSTEM, &exec);

drivers/gpu/drm/xe/xe_dma_buf.c +6 -0
+		drm_exec_retry_on_contention(&exec);
+		if (IS_ERR(bo)) {
+			ret = PTR_ERR(bo);
+			xe_validation_retry_on_oom(&ctx, &ret);
+			goto error;
+		}

  }

drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
-	dma_resv_unlock(resv);
+	drm_gem_object_put(dummy_obj);


  return &bo->ttm.base;

 error:

drivers/gpu/drm/xe/xe_dma_buf.c +0 -1
-	dma_resv_unlock(resv);

  return ERR_PTR(ret);
 }

Signature · 1 line
2.50.1
author maintainer
#34 lore raw

Re: [PATCH 09/15] drm/xe: Convert the CPU fault handler for exhaustive eviction

In reply to Matthew Brost
Quoted 32 lines · Matthew Brost
Quoted 31 lines · Thomas Hellström

The CPU fault handler may populate bos and migrate, and in doing so might interfere with other tasks validing.

Convert it for exhaustive eviction. To do this properly without potentially introducing stalls with the mmap lock held requires TTM work. In the meantime, let's live with those stalls that would typically happen on memory pressure.

---
 drivers/gpu/drm/xe/xe_bo.c | 11 ++++++++---

 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +2 -1
index 5e40b6cb8d2a..dd1e0e9957e0 100644
@@ -1720,14 +1720,18 @@ static vm_fault_t xe_gem_fault(struct
 vm_fault *vmf)
  	struct xe_device *xe = to_xe_device(ddev);
  	struct xe_bo *bo = ttm_to_xe_bo(tbo);
  	bool needs_rpm = bo->flags & XE_BO_FLAG_VRAM_MASK;
-	struct drm_exec *exec;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
  	vm_fault_t ret;
  	int idx;


  if (needs_rpm)
  xe_pm_runtime_get(xe);

Quoted 8 lines

drivers/gpu/drm/xe/xe_bo.c static vm_fault_t xe_gem_fault(struct vm_fault *vmf) · line 1733

Quoted 6 lines

drivers/gpu/drm/xe/xe_bo.c +2 -1
-	exec = XE_VALIDATION_UNIMPLEMENTED;
+	if (xe_validation_ctx_init(&ctx, &xe->val, &exec,
+				   DRM_EXEC_INTERRUPTIBLE_WAIT, 0,

false))

drivers/gpu/drm/xe/xe_bo.c +1 -0
+		return VM_FAULT_NOPAGE;

Any particular reason to not use xe_validation_guard here?

Well this is a bit complicated ATM.
We would need some serious TTM rework here to support drm_exec in these helpers, and ATM I think upon closer inspection we'd need an xe_validation_ctx_init that doesn't initialize a drm_exec.

ttm_bo_vm_reserve() might use a bo lock without a drm_exec and that will cause a lockdep splat if the drm_exec transaction has initialized the ww ctx, which happens in drm_exec_until_all_locked().

I should add a comment about that.

/Thomas

Quoted 31 lines

Matt

Quoted 28 lines

+
  ret = ttm_bo_vm_reserve(tbo, vmf);
  if (ret)
  goto out;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -1735,7 +1739,7 @@ static vm_fault_t xe_gem_fault(struct
 vm_fault *vmf)
  	if (drm_dev_enter(ddev, &idx)) {
  		trace_xe_bo_cpu_fault(bo);

drivers/gpu/drm/xe/xe_bo.c +1 -1
-		xe_validation_assert_exec(xe, exec, &tbo->base);
+		xe_validation_assert_exec(xe, &exec, &tbo->base);

  ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma-

Quoted 1 line

vm_page_prot,


TTM_BO_VM_NUM_PREFAULT);
  drm_dev_exit(idx);

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -1761,6 +1765,7 @@ static vm_fault_t xe_gem_fault(struct
 vm_fault *vmf)


  dma_resv_unlock(tbo->base.resv);
 out:

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	xe_validation_ctx_fini(&ctx);

  if (needs_rpm)
  xe_pm_runtime_put(xe);

Signature · 1 line
2.50.1
author maintainer
#19 lore raw

Re: [PATCH 05/15] drm/xe: Introduce an xe_validation wrapper around drm_exec

In reply to Matthew Brost
Quoted 98 lines · Matthew Brost
Quoted 97 lines · Thomas Hellström

Introduce a validation wrapper xe_validation_guard() as a helper intended to be used around drm_exec transactions what perform validations. Once TTM can handle exhaustive eviction we could remove this wrapper or make it mostly a NO-OP unless other functionality is added to it.

Currently the wrapper takes a read lock upon entry and if the transaction hits an OOM, all locks are released and the transaction is retried with a write-lock. If all other validations participate in this scheme, the transaction with the write lock will be the only transaction validating and should have access to all available non-pinned memory.

There is currently a problem in that TTM converts -EDEADLOCKS to

(quoted diff) +0 -2
-ENOMEM, and with ww_mutex slowpath error injections, we can hit
-ENOMEMs without having actually ran out of memory. We abuse

ww_mutex internals to detect such situations until TTM is fixes to not convert the error code. In the meantime, injecting ww_mutex slowpath -EDEADLOCKs is a good way to test the implementation in the absence of real OOMs.

Just introduce the wrapper in this commit. It will be hooked up to the driver in following commits.

---
 drivers/gpu/drm/xe/xe_validation.c | 199
+++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_validation.h | 107 ++++++++++++++++

 2 files changed, 306 insertions(+)
drivers/gpu/drm/xe/xe_validation.c +1 -0
index cc0684d24e02..cd1424f04237 100644
@@ -5,6 +5,7 @@
  #include "xe_bo.h"
  #include <drm/drm_exec.h>
  #include <drm/drm_gem.h>
+#include <drm/drm_gpuvm.h>


 #include "xe_assert.h"
 #include "xe_validation.h"

drivers/gpu/drm/xe/xe_validation.c +0 -0
@@ -47,3 +48,201 @@ void xe_validation_assert_exec(const struct
 xe_device *xe,
  	}
  }

 #endif

drivers/gpu/drm/xe/xe_validation.c +43 -0
+
+static int xe_validation_lock(struct xe_validation_ctx *ctx)
+{
+	struct xe_validation_device *val = ctx->val;
+	int ret = 0;
+
+	if (ctx->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) {
+		if (ctx->request_exclusive)
+			ret = down_write_killable(&val->lock);
+		else
+			ret = down_read_interruptible(&val->lock);
+	} else {
+		if (ctx->request_exclusive)
+			down_write(&val->lock);
+		else
+			down_read(&val->lock);
+	}
+
+	if (!ret) {
+		ctx->lock_held = true;
+		ctx->lock_held_exclusive = ctx->request_exclusive;
+	}
+
+	return ret;
+}
+
+static void xe_validation_unlock(struct xe_validation_ctx *ctx)
+{
+	if (!ctx->lock_held)
+		return;
+
+	if (ctx->lock_held_exclusive)
+		up_write(&ctx->val->lock);
+	else
+		up_read(&ctx->val->lock);
+
+	ctx->lock_held = false;
+}
+
+/**
+ * xe_validation_ctx_init() - Initialize an xe_validation_ctx
+ * @ctx: The xe_validation_ctx to initialize.
+ * @val: The xe_validation_device representing the validation

domain.
+ * @exec: The struct drm_exec to use for the transaction.
+ * @flags: The flags to use for drm_exec initialization.
+ * @nr: The number of anticipated buffer object locks. Forwarded to

Quoted 8 lines
Quoted 3 lines

+ * drm_exec initialization.
+ * @exclusive: Whether to use exclusive locking already on first validation.

The last two parameters of this function are always passed as 0 and false in this series. Is it worth keeping them? I don’t see a case where
nr would ever be non-zero.

Right. I'll remove that from the interface but keep it in the struct so that if we ever need it, we can just change the interface.

Quoted 2 lines

exclusive is defensible, but it’s still
unused.

Actually I think with vf provisioning and in the pm notifier it makes sense to set it to true.

Quoted 6 lines

Maybe drop both and reserve a bit in flags for a driver-defined
“exclusive.” That would make the call sites more readable—long argument
lists make it easy to forget what each parameter means or to transpose
them.

Problem is that this is drm_exec flags. I'm not really keen on overloading driver defined flags there. We could add a separate const struct xe_validation_flags, though, but then we'd have a translation step?

Quoted 325 lines
Quoted 325 lines

+ *
+ * Initialize and lock a an xe_validation transaction using the validation domain
+ * represented by @val. Also initialize the drm_exec object forwarding
+ * @flags and @nr to the drm_exec initialization. The @exclusive parameter should
+ * typically be set to false to avoid locking out other validators from the
+ * domain until an OOM is hit. For testing- or final attempt purposes it can,
+ * however, be set to true.
+ *
+ * Return: %0 on success, %-EINTR if interruptible initial locking failed with a
+ * signal pending.
+ */
+int xe_validation_ctx_init(struct xe_validation_ctx *ctx, struct xe_validation_device *val,

drivers/gpu/drm/xe/xe_validation.c +1 -0
+			   struct drm_exec *exec, u32 flags,

unsigned int nr,

drivers/gpu/drm/xe/xe_validation.c +30 -0
+			   bool exclusive)
+{
+	int ret;
+
+	ctx->exec = exec;
+	ctx->val = val;
+	ctx->lock_held = false;
+	ctx->lock_held_exclusive = false;
+	ctx->request_exclusive = exclusive;
+	ctx->flags = flags;
+	ctx->nr = nr;
+
+	ret = xe_validation_lock(ctx);
+	if (ret)
+		return ret;
+
+	drm_exec_init(exec, flags, nr);
+
+	return 0;
+}
+
+#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
+/*
+ * This abuses both drm_exec and ww_mutex internals and should be
+ * replaced by checking for -EDEADLK when we can make TTM
+ * stop converting -EDEADLK to -ENOMEM.
+ * An alternative is to not have exhaustive eviction with
+ * CONFIG_DEBUG_WW_MUTEX_SLOWPATH until that happens.
+ */
+static bool xe_validation_contention_injected(struct drm_exec

*exec)

drivers/gpu/drm/xe/xe_validation.c +7 -0
+{
+	return !!exec->ticket.contending_lock;
+}
+
+#else
+
+static bool xe_validation_contention_injected(struct drm_exec

*exec)

drivers/gpu/drm/xe/xe_validation.c +7 -0
+{
+	return false;
+}
+
+#endif
+
+static bool __xe_validation_should_retry(struct xe_validation_ctx

*ctx, int ret)

drivers/gpu/drm/xe/xe_validation.c +14 -0
+{
+	if (ret == -ENOMEM &&
+	    ((ctx->request_exclusive &&
+	      xe_validation_contention_injected(ctx->exec)) ||
+	     !ctx->request_exclusive)) {
+		ctx->request_exclusive = true;
+		return true;
+	}
+
+	return false;
+}
+
+/**
+ * xe_validation_exec_lock() - Perform drm_gpuvm_exec_lock within

a validation
+ * transaction.
+ * @ctx: An uninitialized xe_validation_ctx.
+ * @vm_exec: An initialized struct vm_exec.
+ * @val: The validation domain.
+ *
+ * The drm_gpuvm_exec_lock() function internally initializes its drm_exec
+ * transaction and therefore doesn't lend itself very well to be using
+ * xe_validation_ctx_init(). Provide a helper that takes an uninitialized
+ * xe_validation_ctx and calls drm_gpuvm_exec_lock() with OOM retry.
+ *
+ * Return: %0 on success, negative error code on failure.
+ */

drivers/gpu/drm/xe/xe_validation.c +30 -0
+int xe_validation_exec_lock(struct xe_validation_ctx *ctx,
+			    struct drm_gpuvm_exec *vm_exec,
+			    struct xe_validation_device *val)
+{
+	int ret;
+
+	memset(ctx, 0, sizeof(*ctx));
+	ctx->exec = &vm_exec->exec;
+	ctx->flags = vm_exec->flags;
+	ctx->val = val;
+retry:
+	ret = xe_validation_lock(ctx);
+	if (ret)
+		return ret;
+
+	ret = drm_gpuvm_exec_lock(vm_exec);
+	if (ret) {
+		xe_validation_unlock(ctx);
+		if (__xe_validation_should_retry(ctx, ret))
+			goto retry;
+	}
+
+	return ret;
+}
+
+/**
+ * xe_validation_ctx_fini() - Finalize a validation transaction
+ * @ctx: The Validation transaction to finalize.
+ *
+ * Finalize a validation transaction and its related drm_exec

transaction.
+ */

drivers/gpu/drm/xe/xe_validation.c +8 -0
+void xe_validation_ctx_fini(struct xe_validation_ctx *ctx)
+{
+	drm_exec_fini(ctx->exec);
+	xe_validation_unlock(ctx);
+}
+
+/**
+ * xe_validation_should_retry() - Determine if a validation

transaction should retry
+ * @ctx: The validation transaction.
+ * @ret: Pointer to a return value variable.
+ *
+ * Determines whether a validation transaction should retry based on the
+ * internal transaction state and the return value pointed to by @ret.
+ * If a validation should be retried, the transaction is prepared for that,
+ * and the validation locked might be re-locked in exclusive mode, and *@ret
+ * is set to %0. If the re-locking errors, typically due to interruptible
+ * locking with signal pending, *@ret is instead set to -EINTR and the
+ * function returns %false.
+ *
+ * Return: %true if validation should be retried, %false otherwise.
+ */
+bool xe_validation_should_retry(struct xe_validation_ctx *ctx, int *ret)

drivers/gpu/drm/xe/xe_validation.c +5 -0
+{
+	if (__xe_validation_should_retry(ctx, *ret)) {
+		drm_exec_fini(ctx->exec);
+		*ret = 0;
+		if (ctx->request_exclusive != ctx-
Quoted 1 line

lock_held_exclusive) {

drivers/gpu/drm/xe/xe_validation.c +9 -0
+			xe_validation_unlock(ctx);
+			*ret = xe_validation_lock(ctx);
+		}
+		drm_exec_init(ctx->exec, ctx->flags, ctx->nr);
+		return !*ret;
+	}
+
+	return false;
+}
drivers/gpu/drm/xe/xe_validation.h +0 -0
index db50feacad7a..a708c260cf18 100644
@@ -7,9 +7,11 @@


 #include <linux/dma-resv.h>
 #include <linux/types.h>
+#include <linux/rwsem.h>

 struct drm_exec;
 struct drm_gem_object;
+struct drm_gpuvm_exec;
 struct xe_device;

 #ifdef CONFIG_PROVE_LOCKING

drivers/gpu/drm/xe/xe_validation.h +0 -0
@@ -66,4 +68,109 @@ void xe_validation_assert_exec(const struct

xe_device *xe, const struct drm_exec
  } while (0)
 #endif

+/**
+ * struct xe_validation_device - The domain for exhaustive eviction
+ * @lock: The lock used to exclude other processes from allocating graphics memory
+ *
+ * The struct xe_validation_device represents the domain for which we want to use
+ * exhaustive eviction. The @lock is typically grabbed in read mode for allocations
+ * but when graphics memory allocation fails, it is retried with the write mode held.
+ */

drivers/gpu/drm/xe/xe_validation.h +6 -0
+struct xe_validation_device {
+	struct rw_semaphore lock;
+};
+
+/**
+ * struct xe_validation_ctx - A struct drm_exec subclass with

support for
+ * exhaustive eviction
+ * @exec: The drm_exec object base class. Note that we use a pointer instead of
+ * embedding to avoid diamond inheritance.
+ * @val: The exhaustive eviction domain.
+ * @lock_held: Whether The domain lock is currently held.
+ * @lock_held_exclusive: Whether the domain lock is held in exclusive mode.
+ * @request_exclusive: Whether to lock exclusively (write mode) the next time
+ * the domain lock is locked.
+ * @flags: The drm_exec flags used for drm_exec (re- )initialization.
+ * @nr: The drm_exec nr parameter used for drm_exec (re- )initializaiton.
+ */

drivers/gpu/drm/xe/xe_validation.h +11 -0
+struct xe_validation_ctx {
+	struct drm_exec *exec;
+	struct xe_validation_device *val;
+	bool lock_held;
+	bool lock_held_exclusive;
+	bool request_exclusive;
+	u32 flags;
+	unsigned int nr;
+};
+
+int xe_validation_ctx_init(struct xe_validation_ctx *ctx, struct

xe_validation_device *val,

drivers/gpu/drm/xe/xe_validation.h +1 -0
+			   struct drm_exec *exec, u32 flags,

unsigned int nr,

drivers/gpu/drm/xe/xe_validation.h +3 -0
+			   bool exclusive);
+
+int xe_validation_exec_lock(struct xe_validation_ctx *ctx, struct

drm_gpuvm_exec *vm_exec,

drivers/gpu/drm/xe/xe_validation.h +5 -0
+			    struct xe_validation_device *val);
+
+void xe_validation_ctx_fini(struct xe_validation_ctx *ctx);
+
+bool xe_validation_should_retry(struct xe_validation_ctx *ctx, int

*ret);

drivers/gpu/drm/xe/xe_validation.h +3 -0
+
+/**
+ * xe_validation_retry_on_oom() - Retry on oom in an xe_validaton

transaction
+ * @_ctx: Pointer to the xe_validation_ctx
+ * @_ret: The current error value possibly holding -ENOMEM
+ *
+ * Use this in way similar to drm_exec_retry_on_contention().
+ * If @_ret contains -ENOMEM the tranaction is restarted once in a way that
+ * blocks other transactions and allows exhastive eviction. If the transaction
+ * was already restarted once, Just return the -ENOMEM. May also set
+ * _ret to -EINTR if not retrying and waits are interruptible.
+ * May only be used within a drm_exec_until_all_locked() loop.
+ */
+#define xe_validation_retry_on_oom(_ctx,
_ret) \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+	do

{ \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+		if (xe_validation_should_retry(_ctx,

_ret)) \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+			goto

*__drm_exec_retry_ptr; \

drivers/gpu/drm/xe/xe_validation.h +4 -0
+	} while (0)
+
+/**
+ * xe_validation_device_init - Initialize a struct

xe_validation_device
+ * @val: The xe_validation_device to init.
+ */

drivers/gpu/drm/xe/xe_validation.h +14 -0
+static inline void
+xe_validation_device_init(struct xe_validation_device *val)
+{
+	init_rwsem(&val->lock);
+}
+
+/*
+ * Make guard() and scoped_guard() work with xe_validation_ctx
+ * so that we can exit transactions without caring about the
+ * cleanup.
+ */
+DEFINE_CLASS(xe_validation, struct xe_validation_ctx *,
+	     if (!IS_ERR(_T)) xe_validation_ctx_fini(_T);,
+	     ({_ret = xe_validation_ctx_init(_ctx, _val, _exec,

_flags, 0, _excl);

drivers/gpu/drm/xe/xe_validation.h +2 -0
+	       _ret ? NULL : _ctx; }),
+	     struct xe_validation_ctx *_ctx, struct

xe_validation_device *_val,

drivers/gpu/drm/xe/xe_validation.h +1 -0
+	     struct drm_exec *_exec, u32 _flags, int _ret, bool

_excl);
+static inline void *class_xe_validation_lock_ptr(class_xe_validation_t *_T)

drivers/gpu/drm/xe/xe_validation.h +5 -0
+{return *_T; }
+#define class_xe_validation_is_conditional false
+
+/**
+ * xe_validation_guard() - An auto-cleanup xe_validation_ctx

transaction
+ * @_ctx: The xe_validation_ctx.
+ * @_val: The xe_validation_device.
+ * @_exec: The struct drm_exec object
+ * @_flags: Flags for the drm_exec transaction. See the struct drm_exec documention!

Quoted 8 lines
Quoted 5 lines

+ * @_ret: Return in / out parameter. May be set by this macro. Typicall 0 when called.
+ * @_excl: Whether to start in exclusive mode already in the first iteration.
+ *

Same comment as above on function xe_validation_ctx_init wrt to arguments.

OK, let me know what you think given the above.

/Thomas

Quoted 17 lines

Matt

Quoted 14 lines

+ * This macro is will initiate a drm_exec transaction with additional support for
+ * exhaustive eviction.
+ */
+#define xe_validation_guard(_ctx, _val, _exec, _flags, _ret,
_excl) \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+	scoped_guard(xe_validation, _ctx, _val, _exec, _flags,

_ret, _excl) \

drivers/gpu/drm/xe/xe_validation.h +2 -0
+	drm_exec_until_all_locked(_exec)
+

 #endif

Signature · 1 line
2.50.1
author maintainer
#48 lore raw

Re: [PATCH 13/15] drm/xe: Convert xe_bo_create_pin_map_at() for exhaustive eviction

In reply to Matthew Brost
Quoted 287 lines · Matthew Brost
Quoted 275 lines · Thomas Hellström

Most users of xe_bo_create_pin_map_at() and xe_bo_create_pin_map_at_aligned() are not using the vm parameter, and that simplifies conversion. Introduce an xe_bo_create_pin_map_at_novm() function and make the _aligned() version static. Use xe_validation_guard() for conversion.

---
 .../compat-i915-headers/gem/i915_gem_stolen.h | 24 ++---- drivers/gpu/drm/xe/display/xe_fb_pin.c        | 45 +++++----- drivers/gpu/drm/xe/display/xe_plane_initial.c |  4 +- drivers/gpu/drm/xe/xe_bo.c                    | 83 ++++++++++++++-
----
 drivers/gpu/drm/xe/xe_bo.h                    | 13 +-- drivers/gpu/drm/xe/xe_eu_stall.c              |  6 +-

 6 files changed, 101 insertions(+), 74 deletions(-)
(quoted diff) +0 -0

headers/gem/i915_gem_stolen.h b/drivers/gpu/drm/xe/compat-i915- headers/gem/i915_gem_stolen.h
index 1ce1e9da975b..ab48635ddffa 100644

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -2
@@ -21,9 +21,7 @@ static inline int
 i915_gem_stolen_insert_node_in_range(struct xe_device *xe,
  						       u32 size,
 u32 align,
  						       u32 start,
 u32 end)
  {
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
  	struct xe_bo *bo;
-	int err;

  u32 flags = XE_BO_FLAG_PINNED | XE_BO_FLAG_STOLEN;
  if (start < SZ_4K)

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -0
@@ -34,25 +32,15 @@ static inline int
 i915_gem_stolen_insert_node_in_range(struct xe_device *xe,
  		start = ALIGN(start, align);
  	}

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -1
-	bo = xe_bo_create_locked_range(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -2
-				       NULL, size, start, end,
-				       ttm_bo_type_kernel, flags,

0, exec);

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -12
-	if (IS_ERR(bo)) {
-		err = PTR_ERR(bo);
-		bo = NULL;
-		return err;
-	}
-	err = xe_bo_pin(bo, exec);
-	xe_bo_unlock_vm_held(bo);
-
-	if (err) {
-		xe_bo_put(fb->bo);
-		bo = NULL;
-	}
+	bo = xe_bo_create_pin_map_at_novm(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -0
+					  size, start,

ttm_bo_type_kernel, flags,

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +3 -0
+					  false, 0, true);
+	if (IS_ERR(bo))
+		return PTR_ERR(bo);


  fb->bo = bo;

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -1
-	return err;
+	return 0;

 }

 static inline int i915_gem_stolen_insert_node(struct xe_device *xe,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -0
index 43c45344ea26..d46ff7ebb0a1 100644
@@ -102,29 +102,32 @@ static int __xe_pin_fb_vma_dpt(const struct
 intel_framebuffer *fb,
  				 XE_PAGE_SIZE);


  if (IS_DGFX(xe))

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0,

NULL,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						      dpt_size,

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_VRAM0 |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_GGTT |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -1
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size,

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						   true,
+						  

XE_BO_FLAG_VRAM0 |

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   alignment,

false);
  else

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0,

NULL,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						      dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_STOLEN |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_GGTT |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -1
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						   true,
+						  

XE_BO_FLAG_STOLEN |

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   alignment,

false);
  if (IS_ERR(dpt))

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0,

NULL,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						      dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_GGTT |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -1
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						   true,
+						  

XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   alignment,

false);
  if (IS_ERR(dpt))
  return PTR_ERR(dpt);

drivers/gpu/drm/xe/display/xe_plane_initial.c +0 -0
index 826ac3d578b7..79d00127caf4 100644
@@ -140,8 +140,8 @@ initial_plane_bo(struct xe_device *xe,
  			page_size);
  	size -= base;

drivers/gpu/drm/xe/display/xe_plane_initial.c +0 -1
-	bo = xe_bo_create_pin_map_at(xe, tile0, NULL, size,

phys_base,

drivers/gpu/drm/xe/display/xe_plane_initial.c +1 -1
-				     ttm_bo_type_kernel, flags);
+	bo = xe_bo_create_pin_map_at_novm(xe, tile0, size,

phys_base,

drivers/gpu/drm/xe/display/xe_plane_initial.c +1 -0
+					  ttm_bo_type_kernel,

flags, true, 0, false);
  if (IS_ERR(bo)) {
  drm_dbg(&xe->drm,
  "Failed to create bo phys_base=%pa size %u with flags %x: %li\n",

drivers/gpu/drm/xe/xe_bo.c +0 -0
index 23b28eeef59f..c9928d4ee5a0 100644
@@ -2253,29 +2253,20 @@ struct xe_bo *xe_bo_create_user(struct
 xe_device *xe,
  	return bo;
  }


-struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.c +0 -3
-				      struct xe_vm *vm,
-				      size_t size, u64 offset,
-				      enum ttm_bo_type type, u32

flags)

drivers/gpu/drm/xe/xe_bo.c +0 -2
-{
-	return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size,

offset,

drivers/gpu/drm/xe/xe_bo.c +0 -4
-					       type, flags, 0);
-}
-
-struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device

*xe,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-					      struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.c +0 -2
-					      struct xe_vm *vm,
-					      size_t size, u64

offset,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-					      enum ttm_bo_type

type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +1 -1
-					      u64 alignment)
+static struct xe_bo *xe_bo_create_pin_map_at_aligned(struct

xe_device *xe,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     struct

xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     struct xe_vm

*vm,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     size_t size,

u64 offset,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     enum

ttm_bo_type type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     bool vmap,

u64 alignment,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     struct

drm_exec *exec)
 {
  struct xe_bo *bo;
  int err;
  u64 start = offset == ~0ull ? 0 : offset; u64 end = offset == ~0ull ? offset : start + size;

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) :

XE_VALIDATION_UNIMPLEMENTED;

drivers/gpu/drm/xe/xe_bo.c +1 -1
-	if (flags & XE_BO_FLAG_STOLEN &&
+	if (flags & XE_BO_FLAG_STOLEN && vmap &&

      xe_ttm_stolen_cpu_access_needs_ggtt(xe)) flags |= XE_BO_FLAG_GGTT;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2289,9 +2280,11 @@ struct xe_bo
 *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
  	if (err)
  		goto err_put;

drivers/gpu/drm/xe/xe_bo.c +5 -3
-	err = xe_bo_vmap(bo);
-	if (err)
-		goto err_unpin;
+	if (vmap) {
+		err = xe_bo_vmap(bo);
+		if (err)
+			goto err_unpin;
+	}


  xe_bo_unlock_vm_held(bo);

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2305,11 +2298,59 @@ struct xe_bo
 *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
  	return ERR_PTR(err);
  }


+/**
+ * xe_bo_create_pin_map_at_novm() - Create pinned and mapped bo at optional VRAM offset
+ * @xe: The xe device.
+ * @tile: The tile to select for migration of this bo, and the

Quoted 8 lines

tile used for
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @size: The storage size to use for the bo.
+ * @offset: Optional VRAM offset or %0 for don't care.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @vmap: Whether to create a buffer object map.

Can we stick vmap into XE_BO_FLAG_?

Quoted 1 line

Also why do we need argument now when it what omitted previously.

It's for the single vf provisioning user that wants to pin without mapping.

But I'm open-coding that in v2 so dropping the vmap bool.

/Thomas

Quoted 133 lines

Matt

Quoted 130 lines

+ * @alignment: GGTT alignment.
+ * @intr: Whether to execut any waits for backing store interruptible.
+ *
+ * Create a pinned and optionally mapped bo with VRAM offset and GGTT alignment
+ * options. The bo will be external and not associated with a VM.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set
+ * to true on entry.
+ */

drivers/gpu/drm/xe/xe_bo.c +2 -0
+struct xe_bo *
+xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+			     size_t size, u64 offset, enum

ttm_bo_type type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +3 -0
+			     bool vmap, u64 alignment, bool intr)
+{
+	u32 drm_exec_flags = intr ? DRM_EXEC_INTERRUPTIBLE_WAIT :

0;

drivers/gpu/drm/xe/xe_bo.c +6 -0
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
+	struct xe_bo *bo;
+	int ret = 0;
+
+	xe_validation_guard(&ctx, &xe->val, &exec, drm_exec_flags,

ret, false) {

drivers/gpu/drm/xe/xe_bo.c +1 -0
+		bo = xe_bo_create_pin_map_at_aligned(xe, tile,

NULL, size, offset,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     type, flags,

vmap,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     alignment,

&exec);

drivers/gpu/drm/xe/xe_bo.c +10 -0
+		drm_exec_retry_on_contention(&exec);
+		if (IS_ERR(bo)) {
+			ret = PTR_ERR(bo);
+			xe_validation_retry_on_oom(&ctx, &ret);
+		}
+	}
+
+	return ret ? ERR_PTR(ret) : bo;
+}
+

 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     struct xe_vm *vm, size_t size, enum ttm_bo_type type, u32 flags)
 {

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	return xe_bo_create_pin_map_at(xe, tile, vm, size, ~0ull,

type, flags);

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) :

XE_VALIDATION_UNIMPLEMENTED;

drivers/gpu/drm/xe/xe_bo.c +2 -0
+
+	return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size,

~0ull, type, flags,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+					       true, 0, exec);

 }

 static void __xe_bo_unpin_map_no_vm(void *arg)

drivers/gpu/drm/xe/xe_bo.h +0 -0
index a625806deeb6..d06266af9662 100644
@@ -109,15 +109,10 @@ struct xe_bo *xe_bo_create_user(struct

xe_device *xe, struct xe_vm *vm, size_t s
 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     struct xe_vm *vm, size_t size, enum ttm_bo_type type, u32 flags);
-struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-				      struct xe_vm *vm, size_t

size, u64 offset,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-				      enum ttm_bo_type type, u32

flags);
-struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-					      struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.h +0 -2
-					      struct xe_vm *vm,
-					      size_t size, u64

offset,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-					      enum ttm_bo_type

type, u32 flags,

drivers/gpu/drm/xe/xe_bo.h +2 -1
-					      u64 alignment);
+struct xe_bo *
+xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.h +1 -0
+			     size_t size, u64 offset, enum

ttm_bo_type type,

drivers/gpu/drm/xe/xe_bo.h +1 -0
+			     u32 flags, bool vmap, u64 alignment,

bool intr);
 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     size_t size, u32 flags);
 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_eu_stall.c +0 -0
index fdd514fec5ef..afabfc125488 100644
@@ -617,9 +617,9 @@ static int xe_eu_stall_data_buf_alloc(struct
 xe_eu_stall_data_stream *stream,


  size = stream->per_xecore_buf_size * last_xecore;

drivers/gpu/drm/xe/xe_eu_stall.c +0 -2
-	bo = xe_bo_create_pin_map_at_aligned(tile->xe, tile, NULL,
-					     size, ~0ull,

ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_eu_stall.c +0 -1
-					     XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_GGTT, SZ_64);

drivers/gpu/drm/xe/xe_eu_stall.c +1 -0
+	bo = xe_bo_create_pin_map_at_novm(tile->xe, tile, size,

~0ull, ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_eu_stall.c +1 -0
+					  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_GGTT, true,

drivers/gpu/drm/xe/xe_eu_stall.c +1 -0
+					  SZ_64, false);

  if (IS_ERR(bo)) {
  kfree(stream->xecore_buf);
  return PTR_ERR(bo);

Signature · 1 line
2.50.1
author maintainer
#50 lore raw

Re: [PATCH 13/15] drm/xe: Convert xe_bo_create_pin_map_at() for exhaustive eviction

In reply to Matthew Brost
Quoted 135 lines · Matthew Brost
Quoted 108 lines · Thomas Hellström

Most users of xe_bo_create_pin_map_at() and xe_bo_create_pin_map_at_aligned() are not using the vm parameter, and that simplifies conversion. Introduce an xe_bo_create_pin_map_at_novm() function and make the _aligned() version static. Use xe_validation_guard() for conversion.

---
 .../compat-i915-headers/gem/i915_gem_stolen.h | 24 ++---- drivers/gpu/drm/xe/display/xe_fb_pin.c        | 45 +++++----- drivers/gpu/drm/xe/display/xe_plane_initial.c |  4 +- drivers/gpu/drm/xe/xe_bo.c                    | 83 ++++++++++++++-
----
 drivers/gpu/drm/xe/xe_bo.h                    | 13 +-- drivers/gpu/drm/xe/xe_eu_stall.c              |  6 +-

 6 files changed, 101 insertions(+), 74 deletions(-)
(quoted diff) +0 -0

headers/gem/i915_gem_stolen.h b/drivers/gpu/drm/xe/compat-i915- headers/gem/i915_gem_stolen.h
index 1ce1e9da975b..ab48635ddffa 100644

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -2
@@ -21,9 +21,7 @@ static inline int
 i915_gem_stolen_insert_node_in_range(struct xe_device *xe,
  						       u32 size,
 u32 align,
  						       u32 start,
 u32 end)
  {
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
  	struct xe_bo *bo;
-	int err;

  u32 flags = XE_BO_FLAG_PINNED | XE_BO_FLAG_STOLEN;
  if (start < SZ_4K)

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -0
@@ -34,25 +32,15 @@ static inline int
 i915_gem_stolen_insert_node_in_range(struct xe_device *xe,
  		start = ALIGN(start, align);
  	}

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -1
-	bo = xe_bo_create_locked_range(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +0 -2
-				       NULL, size, start, end,
-				       ttm_bo_type_kernel, flags,

0, exec);

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -12
-	if (IS_ERR(bo)) {
-		err = PTR_ERR(bo);
-		bo = NULL;
-		return err;
-	}
-	err = xe_bo_pin(bo, exec);
-	xe_bo_unlock_vm_held(bo);
-
-	if (err) {
-		xe_bo_put(fb->bo);
-		bo = NULL;
-	}
+	bo = xe_bo_create_pin_map_at_novm(xe,

xe_device_get_root_tile(xe),

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -0
+					  size, start,

ttm_bo_type_kernel, flags,

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +3 -0
+					  false, 0, true);
+	if (IS_ERR(bo))
+		return PTR_ERR(bo);


  fb->bo = bo;

drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h +1 -1
-	return err;
+	return 0;

 }

 static inline int i915_gem_stolen_insert_node(struct xe_device *xe,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -0
index 43c45344ea26..d46ff7ebb0a1 100644
@@ -102,29 +102,32 @@ static int __xe_pin_fb_vma_dpt(const struct
 intel_framebuffer *fb,
  				 XE_PAGE_SIZE);


  if (IS_DGFX(xe))

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0,

NULL,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						      dpt_size,

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_VRAM0 |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_GGTT |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -1
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size,

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						   true,
+						  

XE_BO_FLAG_VRAM0 |

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  
Quoted 1 line

XE_BO_FLAG_PAGETABLE,

The flags and vmap arguments are swapped.

Quoted 22 lines
drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   alignment,

false);
  else

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0,

NULL,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						      dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_STOLEN |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_GGTT |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -1
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   true,
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

XE_BO_FLAG_STOLEN |

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

XE_BO_FLAG_PAGETABLE,

The flags and vmap arguments are swapped.

Right. This was fixed in the wrong patch (14), but will drop the vmap argument in v2.

/Thomas

Quoted 279 lines

Matt

Quoted 276 lines
drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   alignment,

false);
  if (IS_ERR(dpt))

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-		dpt = xe_bo_create_pin_map_at_aligned(xe, tile0,

NULL,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						      dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_GGTT |

drivers/gpu/drm/xe/display/xe_fb_pin.c +0 -1
-						     

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -1
-						      alignment);
+		dpt = xe_bo_create_pin_map_at_novm(xe, tile0,
+						   dpt_size, 

~0ull,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

ttm_bo_type_kernel,

drivers/gpu/drm/xe/display/xe_fb_pin.c +2 -0
+						   true,
+						  

XE_BO_FLAG_SYSTEM |

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   XE_BO_FLAG_GGTT

|

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						  

XE_BO_FLAG_PAGETABLE,

drivers/gpu/drm/xe/display/xe_fb_pin.c +1 -0
+						   alignment,

false);
  if (IS_ERR(dpt))
  return PTR_ERR(dpt);

drivers/gpu/drm/xe/display/xe_plane_initial.c +0 -0
index 826ac3d578b7..79d00127caf4 100644
@@ -140,8 +140,8 @@ initial_plane_bo(struct xe_device *xe,
  			page_size);
  	size -= base;

drivers/gpu/drm/xe/display/xe_plane_initial.c +0 -1
-	bo = xe_bo_create_pin_map_at(xe, tile0, NULL, size,

phys_base,

drivers/gpu/drm/xe/display/xe_plane_initial.c +1 -1
-				     ttm_bo_type_kernel, flags);
+	bo = xe_bo_create_pin_map_at_novm(xe, tile0, size,

phys_base,

drivers/gpu/drm/xe/display/xe_plane_initial.c +1 -0
+					  ttm_bo_type_kernel,

flags, true, 0, false);
  if (IS_ERR(bo)) {
  drm_dbg(&xe->drm,
  "Failed to create bo phys_base=%pa size %u with flags %x: %li\n",

drivers/gpu/drm/xe/xe_bo.c +0 -0
index 23b28eeef59f..c9928d4ee5a0 100644
@@ -2253,29 +2253,20 @@ struct xe_bo *xe_bo_create_user(struct
 xe_device *xe,
  	return bo;
  }


-struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.c +0 -3
-				      struct xe_vm *vm,
-				      size_t size, u64 offset,
-				      enum ttm_bo_type type, u32

flags)

drivers/gpu/drm/xe/xe_bo.c +0 -2
-{
-	return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size,

offset,

drivers/gpu/drm/xe/xe_bo.c +0 -4
-					       type, flags, 0);
-}
-
-struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device

*xe,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-					      struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.c +0 -2
-					      struct xe_vm *vm,
-					      size_t size, u64

offset,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-					      enum ttm_bo_type

type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +1 -1
-					      u64 alignment)
+static struct xe_bo *xe_bo_create_pin_map_at_aligned(struct

xe_device *xe,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     struct

xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     struct xe_vm

*vm,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     size_t size,

u64 offset,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     enum

ttm_bo_type type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     bool vmap,

u64 alignment,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     struct

drm_exec *exec)
 {
  struct xe_bo *bo;
  int err;
  u64 start = offset == ~0ull ? 0 : offset; u64 end = offset == ~0ull ? offset : start + size;

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) :

XE_VALIDATION_UNIMPLEMENTED;

drivers/gpu/drm/xe/xe_bo.c +1 -1
-	if (flags & XE_BO_FLAG_STOLEN &&
+	if (flags & XE_BO_FLAG_STOLEN && vmap &&

      xe_ttm_stolen_cpu_access_needs_ggtt(xe)) flags |= XE_BO_FLAG_GGTT;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2289,9 +2280,11 @@ struct xe_bo
 *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
  	if (err)
  		goto err_put;

drivers/gpu/drm/xe/xe_bo.c +5 -3
-	err = xe_bo_vmap(bo);
-	if (err)
-		goto err_unpin;
+	if (vmap) {
+		err = xe_bo_vmap(bo);
+		if (err)
+			goto err_unpin;
+	}


  xe_bo_unlock_vm_held(bo);

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -2305,11 +2298,59 @@ struct xe_bo
 *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
  	return ERR_PTR(err);
  }


+/**
+ * xe_bo_create_pin_map_at_novm() - Create pinned and mapped bo at optional VRAM offset
+ * @xe: The xe device.
+ * @tile: The tile to select for migration of this bo, and the tile used for
+ * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos.
+ * @size: The storage size to use for the bo.
+ * @offset: Optional VRAM offset or %0 for don't care.
+ * @type: The TTM buffer object type.
+ * @flags: XE_BO_FLAG_ flags.
+ * @vmap: Whether to create a buffer object map.
+ * @alignment: GGTT alignment.
+ * @intr: Whether to execut any waits for backing store interruptible.
+ *
+ * Create a pinned and optionally mapped bo with VRAM offset and GGTT alignment
+ * options. The bo will be external and not associated with a VM.
+ *
+ * Return: The buffer object on success. Negative error pointer on failure.
+ * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set
+ * to true on entry.
+ */

drivers/gpu/drm/xe/xe_bo.c +2 -0
+struct xe_bo *
+xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+			     size_t size, u64 offset, enum

ttm_bo_type type, u32 flags,

drivers/gpu/drm/xe/xe_bo.c +3 -0
+			     bool vmap, u64 alignment, bool intr)
+{
+	u32 drm_exec_flags = intr ? DRM_EXEC_INTERRUPTIBLE_WAIT :

0;

drivers/gpu/drm/xe/xe_bo.c +6 -0
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
+	struct xe_bo *bo;
+	int ret = 0;
+
+	xe_validation_guard(&ctx, &xe->val, &exec, drm_exec_flags,

ret, false) {

drivers/gpu/drm/xe/xe_bo.c +1 -0
+		bo = xe_bo_create_pin_map_at_aligned(xe, tile,

NULL, size, offset,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     type, flags,

vmap,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						     alignment,

&exec);

drivers/gpu/drm/xe/xe_bo.c +10 -0
+		drm_exec_retry_on_contention(&exec);
+		if (IS_ERR(bo)) {
+			ret = PTR_ERR(bo);
+			xe_validation_retry_on_oom(&ctx, &ret);
+		}
+	}
+
+	return ret ? ERR_PTR(ret) : bo;
+}
+

 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     struct xe_vm *vm, size_t size, enum ttm_bo_type type, u32 flags)
 {

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	return xe_bo_create_pin_map_at(xe, tile, vm, size, ~0ull,

type, flags);

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	struct drm_exec *exec = vm ? xe_vm_validation_exec(vm) :

XE_VALIDATION_UNIMPLEMENTED;

drivers/gpu/drm/xe/xe_bo.c +2 -0
+
+	return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size,

~0ull, type, flags,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+					       true, 0, exec);

 }

 static void __xe_bo_unpin_map_no_vm(void *arg)

drivers/gpu/drm/xe/xe_bo.h +0 -0
index a625806deeb6..d06266af9662 100644
@@ -109,15 +109,10 @@ struct xe_bo *xe_bo_create_user(struct

xe_device *xe, struct xe_vm *vm, size_t s
 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     struct xe_vm *vm, size_t size, enum ttm_bo_type type, u32 flags);
-struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-				      struct xe_vm *vm, size_t

size, u64 offset,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-				      enum ttm_bo_type type, u32

flags);
-struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-					      struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.h +0 -2
-					      struct xe_vm *vm,
-					      size_t size, u64

offset,

drivers/gpu/drm/xe/xe_bo.h +0 -1
-					      enum ttm_bo_type

type, u32 flags,

drivers/gpu/drm/xe/xe_bo.h +2 -1
-					      u64 alignment);
+struct xe_bo *
+xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile

*tile,

drivers/gpu/drm/xe/xe_bo.h +1 -0
+			     size_t size, u64 offset, enum

ttm_bo_type type,

drivers/gpu/drm/xe/xe_bo.h +1 -0
+			     u32 flags, bool vmap, u64 alignment,

bool intr);
 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
     size_t size, u32 flags);
 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,

drivers/gpu/drm/xe/xe_eu_stall.c +0 -0
index fdd514fec5ef..afabfc125488 100644
@@ -617,9 +617,9 @@ static int xe_eu_stall_data_buf_alloc(struct
 xe_eu_stall_data_stream *stream,


  size = stream->per_xecore_buf_size * last_xecore;

drivers/gpu/drm/xe/xe_eu_stall.c +0 -2
-	bo = xe_bo_create_pin_map_at_aligned(tile->xe, tile, NULL,
-					     size, ~0ull,

ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_eu_stall.c +0 -1
-					     XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_GGTT, SZ_64);

drivers/gpu/drm/xe/xe_eu_stall.c +1 -0
+	bo = xe_bo_create_pin_map_at_novm(tile->xe, tile, size,

~0ull, ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_eu_stall.c +1 -0
+					  XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_GGTT, true,

drivers/gpu/drm/xe/xe_eu_stall.c +1 -0
+					  SZ_64, false);

  if (IS_ERR(bo)) {
  kfree(stream->xecore_buf);
  return PTR_ERR(bo);

Signature · 1 line
2.50.1
author maintainer
#61 lore raw

Re: [PATCH 15/15] drm/xe: Convert pinned suspend eviction for exhaustive eviction

In reply to Matthew Brost
Quoted 299 lines · Matthew Brost
Quoted 290 lines · Thomas Hellström

Pinned suspend eviction and preparation for eviction validates system memory for eviction buffers. Do that under a validation exclusive lock to avoid interfering with other processes validating system graphics memory.

---
 drivers/gpu/drm/xe/xe_bo.c | 205 +++++++++++++++++++--------------
----

 1 file changed, 108 insertions(+), 97 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +2 -1
index 82bf158426ad..efb9c88b6aa7 100644
@@ -1139,43 +1139,47 @@ long xe_bo_shrink(struct ttm_operation_ctx
 *ctx, struct ttm_buffer_object *bo,
  int xe_bo_notifier_prepare_pinned(struct xe_bo *bo)
  {
  	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
  	struct xe_bo *backup;
  	int ret = 0;

drivers/gpu/drm/xe/xe_bo.c +5 -1
-	xe_bo_lock(bo, false);
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, ret, true) {
+		ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
+		drm_exec_retry_on_contention(&exec);
+		xe_assert(xe, !ret);
+		xe_assert(xe, !bo->backup_obj);

drivers/gpu/drm/xe/xe_bo.c +2 -1
-	xe_assert(xe, !bo->backup_obj);
+		/*
+		 * Since this is called from the PM notifier we

might have raced with

drivers/gpu/drm/xe/xe_bo.c +1 -0
+		 * someone unpinning this after we dropped the

pinned list lock and

drivers/gpu/drm/xe/xe_bo.c +4 -0
+		 * grabbing the above bo lock.
+		 */
+		if (!xe_bo_is_pinned(bo))
+			break;

drivers/gpu/drm/xe/xe_bo.c +0 -2
-	/*
-	 * Since this is called from the PM notifier we might have

raced with

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	 * someone unpinning this after we dropped the pinned list

lock and

drivers/gpu/drm/xe/xe_bo.c +2 -4
-	 * grabbing the above bo lock.
-	 */
-	if (!xe_bo_is_pinned(bo))
-		goto out_unlock_bo;
+		if (!xe_bo_is_vram(bo))
+			break;

drivers/gpu/drm/xe/xe_bo.c +2 -2
-	if (!xe_bo_is_vram(bo))
-		goto out_unlock_bo;
+		if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
+			break;

drivers/gpu/drm/xe/xe_bo.c +1 -2
-	if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
-		goto out_unlock_bo;
+		backup = xe_bo_init_locked(xe, NULL, NULL, bo-
Quoted 1 line

ttm.base.resv, NULL, xe_bo_size(bo),

drivers/gpu/drm/xe/xe_bo.c +1 -0
+					  

DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+					   XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_NEEDS_CPU_ACCESS |

drivers/gpu/drm/xe/xe_bo.c +1 -0
+					   XE_BO_FLAG_PINNED,

&exec);

drivers/gpu/drm/xe/xe_bo.c +6 -0
+		if (IS_ERR(backup)) {
+			drm_exec_retry_on_contention(&exec);
+			ret = PTR_ERR(backup);
+			xe_validation_retry_on_oom(&ctx, &ret);
+			break;
+		}

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	backup = xe_bo_init_locked(xe, NULL, NULL, bo-
Quoted 1 line

ttm.base.resv, NULL, xe_bo_size(bo),

drivers/gpu/drm/xe/xe_bo.c +0 -1
-				   DRM_XE_GEM_CPU_CACHING_WB,

ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-				   XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_NEEDS_CPU_ACCESS |

drivers/gpu/drm/xe/xe_bo.c +1 -4
-				   XE_BO_FLAG_PINNED, exec);
-	if (IS_ERR(backup)) {
-		ret = PTR_ERR(backup);
-		goto out_unlock_bo;
+		backup->parent_obj = xe_bo_get(bo); /* Released by

bo_destroy */

drivers/gpu/drm/xe/xe_bo.c +2 -0
+		ttm_bo_pin(&backup->ttm);
+		bo->backup_obj = backup;

  }

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	backup->parent_obj = xe_bo_get(bo); /* Released by

bo_destroy */

drivers/gpu/drm/xe/xe_bo.c +0 -5
-	ttm_bo_pin(&backup->ttm);
-	bo->backup_obj = backup;
-
-out_unlock_bo:
-	xe_bo_unlock(bo);

  return ret;
 }

drivers/gpu/drm/xe/xe_bo.c +2 -1
@@ -1215,99 +1219,106 @@ int xe_bo_notifier_unprepare_pinned(struct
 xe_bo *bo)
  int xe_bo_evict_pinned(struct xe_bo *bo)
  {
  	struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
-	struct drm_exec *exec = XE_VALIDATION_UNIMPLEMENTED;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
  	struct xe_bo *backup = bo->backup_obj;
  	bool backup_created = false;
  	bool unmap = false;
  	int ret = 0;

drivers/gpu/drm/xe/xe_bo.c +4 -1
-	xe_bo_lock(bo, false);
+	xe_validation_guard(&ctx, &xe->val, &exec, 0, ret, true) {
+		ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
+		drm_exec_retry_on_contention(&exec);
+		xe_assert(xe, !ret);

drivers/gpu/drm/xe/xe_bo.c +4 -4
-	if (WARN_ON(!bo->ttm.resource)) {
-		ret = -EINVAL;
-		goto out_unlock_bo;
-	}
+		if (WARN_ON(!bo->ttm.resource)) {
+			ret = -EINVAL;
+			break;
+		}

drivers/gpu/drm/xe/xe_bo.c +4 -4
-	if (WARN_ON(!xe_bo_is_pinned(bo))) {
-		ret = -EINVAL;
-		goto out_unlock_bo;
-	}
+		if (WARN_ON(!xe_bo_is_pinned(bo))) {
+			ret = -EINVAL;
+			break;
+		}

drivers/gpu/drm/xe/xe_bo.c +2 -2
-	if (!xe_bo_is_vram(bo))
-		goto out_unlock_bo;
+		if (!xe_bo_is_vram(bo))
+			break;

drivers/gpu/drm/xe/xe_bo.c +2 -2
-	if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
-		goto out_unlock_bo;
+		if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
+			break;

drivers/gpu/drm/xe/xe_bo.c +0 -2
-	if (!backup) {
-		backup = xe_bo_init_locked(xe, NULL, NULL, bo-
Quoted 1 line

ttm.base.resv, NULL, xe_bo_size(bo),

drivers/gpu/drm/xe/xe_bo.c +0 -1
-					  

DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-					   XE_BO_FLAG_SYSTEM |

XE_BO_FLAG_NEEDS_CPU_ACCESS |

drivers/gpu/drm/xe/xe_bo.c +0 -1
-					   XE_BO_FLAG_PINNED,

exec);

drivers/gpu/drm/xe/xe_bo.c +2 -3
-		if (IS_ERR(backup)) {
-			ret = PTR_ERR(backup);
-			goto out_unlock_bo;
+		if (!backup) {
+			backup = xe_bo_init_locked(xe, NULL, NULL,

bo->ttm.base.resv, NULL,

drivers/gpu/drm/xe/xe_bo.c +2 -0
+						   xe_bo_size(bo),
+						  

DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						  

XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS |

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						  

XE_BO_FLAG_PINNED, &exec);

drivers/gpu/drm/xe/xe_bo.c +2 -0
+			if (IS_ERR(backup)) {
+				drm_exec_retry_on_contention(&exec

);

drivers/gpu/drm/xe/xe_bo.c +2 -0
+				ret = PTR_ERR(backup);
+				xe_validation_retry_on_oom(&ctx,

&ret);

drivers/gpu/drm/xe/xe_bo.c +3 -0
+				break;
+			}
+			backup->parent_obj = xe_bo_get(bo); /*

Released by bo_destroy */

drivers/gpu/drm/xe/xe_bo.c +1 -0
+			backup_created = true;

  }

drivers/gpu/drm/xe/xe_bo.c +0 -1
-		backup->parent_obj = xe_bo_get(bo); /* Released by

bo_destroy */

drivers/gpu/drm/xe/xe_bo.c +0 -2
-		backup_created = true;
-	}

drivers/gpu/drm/xe/xe_bo.c +0 -1
-	if (xe_bo_is_user(bo) || (bo->flags &

XE_BO_FLAG_PINNED_LATE_RESTORE)) {

drivers/gpu/drm/xe/xe_bo.c +0 -7
-		struct xe_migrate *migrate;
-		struct dma_fence *fence;
-
-		if (bo->tile)
-			migrate = bo->tile->migrate;
-		else
-			migrate = mem_type_to_migrate(xe, bo-
Quoted 1 line

ttm.resource->mem_type);

drivers/gpu/drm/xe/xe_bo.c +1 -0
+		if (xe_bo_is_user(bo) || (bo->flags &

XE_BO_FLAG_PINNED_LATE_RESTORE)) {

drivers/gpu/drm/xe/xe_bo.c +2 -0
+			struct xe_migrate *migrate;
+			struct dma_fence *fence;

drivers/gpu/drm/xe/xe_bo.c +0 -1
-		ret = dma_resv_reserve_fences(bo->ttm.base.resv,

1);

drivers/gpu/drm/xe/xe_bo.c +4 -2
-		if (ret)
-			goto out_backup;
+			if (bo->tile)
+				migrate = bo->tile->migrate;
+			else
+				migrate = mem_type_to_migrate(xe,

bo->ttm.resource->mem_type);

drivers/gpu/drm/xe/xe_bo.c +0 -1
-		ret = dma_resv_reserve_fences(backup-
Quoted 1 line

ttm.base.resv, 1);

drivers/gpu/drm/xe/xe_bo.c +1 -2
-		if (ret)
-			goto out_backup;
+			ret = dma_resv_reserve_fences(bo-
Quoted 1 line

ttm.base.resv, 1);

drivers/gpu/drm/xe/xe_bo.c +2 -0
+			if (ret)
+				goto out_backup;

drivers/gpu/drm/xe/xe_bo.c +0 -1
-		fence = xe_migrate_copy(migrate, bo, backup, bo-
Quoted 1 line

ttm.resource,

drivers/gpu/drm/xe/xe_bo.c +0 -1
-					backup->ttm.resource,

false);

drivers/gpu/drm/xe/xe_bo.c +1 -4
-		if (IS_ERR(fence)) {
-			ret = PTR_ERR(fence);
-			goto out_backup;
-		}
+			ret = dma_resv_reserve_fences(backup-
Quoted 1 line

ttm.base.resv, 1);

drivers/gpu/drm/xe/xe_bo.c +2 -0
+			if (ret)
+				goto out_backup;

drivers/gpu/drm/xe/xe_bo.c +1 -9
-		dma_resv_add_fence(bo->ttm.base.resv, fence,
-				   DMA_RESV_USAGE_KERNEL);
-		dma_resv_add_fence(backup->ttm.base.resv, fence,
-				   DMA_RESV_USAGE_KERNEL);
-		dma_fence_put(fence);
-	} else {
-		ret = xe_bo_vmap(backup);
-		if (ret)
-			goto out_backup;
+			fence = xe_migrate_copy(migrate, bo,

backup, bo->ttm.resource,

drivers/gpu/drm/xe/xe_bo.c +1 -0
+						backup-
Quoted 1 line

ttm.resource, false);

drivers/gpu/drm/xe/xe_bo.c +4 -0
+			if (IS_ERR(fence)) {
+				ret = PTR_ERR(fence);
+				goto out_backup;
+			}

drivers/gpu/drm/xe/xe_bo.c +1 -2
-		if (iosys_map_is_null(&bo->vmap)) {
-			ret = xe_bo_vmap(bo);
+			dma_resv_add_fence(bo->ttm.base.resv,

fence,

drivers/gpu/drm/xe/xe_bo.c +2 -0
+					   DMA_RESV_USAGE_KERNEL);
+			dma_resv_add_fence(backup->ttm.base.resv,

fence,

drivers/gpu/drm/xe/xe_bo.c +4 -0
+					   DMA_RESV_USAGE_KERNEL);
+			dma_fence_put(fence);
+		} else {
+			ret = xe_bo_vmap(backup);

  if (ret)
  goto out_backup;

drivers/gpu/drm/xe/xe_bo.c +0 -2
-			unmap = true;
-		}

drivers/gpu/drm/xe/xe_bo.c +0 -1
-		xe_map_memcpy_from(xe, backup->vmap.vaddr, &bo-
Quoted 1 line

vmap, 0,

drivers/gpu/drm/xe/xe_bo.c +6 -2
-				   xe_bo_size(bo));
-	}
+			if (iosys_map_is_null(&bo->vmap)) {
+				ret = xe_bo_vmap(bo);
+				if (ret)
+					goto out_vunmap;
+				unmap = true;
+			}

drivers/gpu/drm/xe/xe_bo.c +1 -2
-	if (!bo->backup_obj)
-		bo->backup_obj = backup;
+			xe_map_memcpy_from(xe, backup->vmap.vaddr,

&bo->vmap, 0,

drivers/gpu/drm/xe/xe_bo.c +2 -0
+					   xe_bo_size(bo));
+		}

drivers/gpu/drm/xe/xe_bo.c int xe_bo_notifier_unprepare_pinned(struct xe_bo *bo) · line 1313

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+		if (!bo->backup_obj)
+			bo->backup_obj = backup;
+out_vunmap:

I just want to confirm that this is safe. The cleanup.h documentation discourages the use of goto because of scoping issues. I assume that since this label is within the scope of the guard, it is fine.

Quoted 5 lines

It might be worth adding a quick note in the validation guard’s kernel-doc mentioning that goto can be dangerous, explaining what is allowed, and perhaps referencing the cleanup.h documentation. I could see this being something developers might trip over.

Yes you are correct. I'll avoid the gotos in v2.

/Thomas

Quoted 25 lines

Patch LGTM, though.

Matt

Quoted 21 lines
drivers/gpu/drm/xe/xe_bo.c +1 -0
+		xe_bo_vunmap(backup);

 out_backup:

drivers/gpu/drm/xe/xe_bo.c +6 -7
-	xe_bo_vunmap(backup);
-	if (ret && backup_created)
-		xe_bo_put(backup);
-out_unlock_bo:
-	if (unmap)
-		xe_bo_vunmap(bo);
-	xe_bo_unlock(bo);
+		if (ret && backup_created)
+			xe_bo_put(backup);
+		if (unmap)
+			xe_bo_vunmap(bo);
+	}
+

  return ret;
 }

Signature · 1 line
2.50.1
maintainer
#20 lore raw

Re: [PATCH 05/15] drm/xe: Introduce an xe_validation wrapper around drm_exec

In reply to Thomas Hellström
Quoted 124 lines · Thomas Hellström
Quoted 98 lines · Matthew Brost
Quoted 97 lines · Thomas Hellström

Introduce a validation wrapper xe_validation_guard() as a helper intended to be used around drm_exec transactions what perform validations. Once TTM can handle exhaustive eviction we could remove this wrapper or make it mostly a NO-OP unless other functionality is added to it.

Currently the wrapper takes a read lock upon entry and if the transaction hits an OOM, all locks are released and the transaction is retried with a write-lock. If all other validations participate in this scheme, the transaction with the write lock will be the only transaction validating and should have access to all available non-pinned memory.

There is currently a problem in that TTM converts -EDEADLOCKS to

(quoted diff) +0 -2
-ENOMEM, and with ww_mutex slowpath error injections, we can hit
-ENOMEMs without having actually ran out of memory. We abuse

ww_mutex internals to detect such situations until TTM is fixes to not convert the error code. In the meantime, injecting ww_mutex slowpath -EDEADLOCKs is a good way to test the implementation in the absence of real OOMs.

Just introduce the wrapper in this commit. It will be hooked up to the driver in following commits.

---
 drivers/gpu/drm/xe/xe_validation.c | 199
+++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_validation.h | 107 ++++++++++++++++

 2 files changed, 306 insertions(+)
drivers/gpu/drm/xe/xe_validation.c +1 -0
index cc0684d24e02..cd1424f04237 100644
@@ -5,6 +5,7 @@
  #include "xe_bo.h"
  #include <drm/drm_exec.h>
  #include <drm/drm_gem.h>
+#include <drm/drm_gpuvm.h>


 #include "xe_assert.h"
 #include "xe_validation.h"

drivers/gpu/drm/xe/xe_validation.c +0 -0
@@ -47,3 +48,201 @@ void xe_validation_assert_exec(const struct
 xe_device *xe,
  	}
  }

 #endif

drivers/gpu/drm/xe/xe_validation.c +43 -0
+
+static int xe_validation_lock(struct xe_validation_ctx *ctx)
+{
+	struct xe_validation_device *val = ctx->val;
+	int ret = 0;
+
+	if (ctx->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) {
+		if (ctx->request_exclusive)
+			ret = down_write_killable(&val->lock);
+		else
+			ret = down_read_interruptible(&val->lock);
+	} else {
+		if (ctx->request_exclusive)
+			down_write(&val->lock);
+		else
+			down_read(&val->lock);
+	}
+
+	if (!ret) {
+		ctx->lock_held = true;
+		ctx->lock_held_exclusive = ctx->request_exclusive;
+	}
+
+	return ret;
+}
+
+static void xe_validation_unlock(struct xe_validation_ctx *ctx)
+{
+	if (!ctx->lock_held)
+		return;
+
+	if (ctx->lock_held_exclusive)
+		up_write(&ctx->val->lock);
+	else
+		up_read(&ctx->val->lock);
+
+	ctx->lock_held = false;
+}
+
+/**
+ * xe_validation_ctx_init() - Initialize an xe_validation_ctx
+ * @ctx: The xe_validation_ctx to initialize.
+ * @val: The xe_validation_device representing the validation

domain.
+ * @exec: The struct drm_exec to use for the transaction.
+ * @flags: The flags to use for drm_exec initialization.
+ * @nr: The number of anticipated buffer object locks. Forwarded to

Quoted 8 lines
Quoted 3 lines

+ * drm_exec initialization.
+ * @exclusive: Whether to use exclusive locking already on first validation.

The last two parameters of this function are always passed as 0 and false in this series. Is it worth keeping them? I don’t see a case where
nr would ever be non-zero.

Right. I'll remove that from the interface but keep it in the struct so that if we ever need it, we can just change the interface.

Quoted 2 lines

exclusive is defensible, but it’s still
unused.

Actually I think with vf provisioning and in the pm notifier it makes sense to set it to true.

Quoted 6 lines

Maybe drop both and reserve a bit in flags for a driver-defined
“exclusive.” That would make the call sites more readable—long argument
lists make it easy to forget what each parameter means or to transpose
them.

Quoted 2 lines

Problem is that this is drm_exec flags. I'm not really keen on overloading driver defined flags there. We could add a separate const

Yes, usually if you want to overload bits we have an explictly defined user bit value in the base flags which doesn't currently exist.

Quoted 2 lines

struct xe_validation_flags, though, but then we'd have a translation step?

xe_validation_flags would make it more clear at the caller what is doing rather than 'false/true'.

Matt

Quoted 358 lines
Quoted 326 lines
Quoted 326 lines

+ *
+ * Initialize and lock a an xe_validation transaction using the validation domain
+ * represented by @val. Also initialize the drm_exec object forwarding
+ * @flags and @nr to the drm_exec initialization. The @exclusive parameter should
+ * typically be set to false to avoid locking out other validators from the
+ * domain until an OOM is hit. For testing- or final attempt purposes it can,
+ * however, be set to true.
+ *
+ * Return: %0 on success, %-EINTR if interruptible initial locking failed with a
+ * signal pending.
+ */
+int xe_validation_ctx_init(struct xe_validation_ctx *ctx, struct xe_validation_device *val,

drivers/gpu/drm/xe/xe_validation.c +1 -0
+			   struct drm_exec *exec, u32 flags,

unsigned int nr,

drivers/gpu/drm/xe/xe_validation.c +30 -0
+			   bool exclusive)
+{
+	int ret;
+
+	ctx->exec = exec;
+	ctx->val = val;
+	ctx->lock_held = false;
+	ctx->lock_held_exclusive = false;
+	ctx->request_exclusive = exclusive;
+	ctx->flags = flags;
+	ctx->nr = nr;
+
+	ret = xe_validation_lock(ctx);
+	if (ret)
+		return ret;
+
+	drm_exec_init(exec, flags, nr);
+
+	return 0;
+}
+
+#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
+/*
+ * This abuses both drm_exec and ww_mutex internals and should be
+ * replaced by checking for -EDEADLK when we can make TTM
+ * stop converting -EDEADLK to -ENOMEM.
+ * An alternative is to not have exhaustive eviction with
+ * CONFIG_DEBUG_WW_MUTEX_SLOWPATH until that happens.
+ */
+static bool xe_validation_contention_injected(struct drm_exec

*exec)

drivers/gpu/drm/xe/xe_validation.c +7 -0
+{
+	return !!exec->ticket.contending_lock;
+}
+
+#else
+
+static bool xe_validation_contention_injected(struct drm_exec

*exec)

drivers/gpu/drm/xe/xe_validation.c +7 -0
+{
+	return false;
+}
+
+#endif
+
+static bool __xe_validation_should_retry(struct xe_validation_ctx

*ctx, int ret)

drivers/gpu/drm/xe/xe_validation.c +14 -0
+{
+	if (ret == -ENOMEM &&
+	    ((ctx->request_exclusive &&
+	      xe_validation_contention_injected(ctx->exec)) ||
+	     !ctx->request_exclusive)) {
+		ctx->request_exclusive = true;
+		return true;
+	}
+
+	return false;
+}
+
+/**
+ * xe_validation_exec_lock() - Perform drm_gpuvm_exec_lock within

a validation
+ * transaction.
+ * @ctx: An uninitialized xe_validation_ctx.
+ * @vm_exec: An initialized struct vm_exec.
+ * @val: The validation domain.
+ *
+ * The drm_gpuvm_exec_lock() function internally initializes its drm_exec
+ * transaction and therefore doesn't lend itself very well to be using
+ * xe_validation_ctx_init(). Provide a helper that takes an uninitialized
+ * xe_validation_ctx and calls drm_gpuvm_exec_lock() with OOM retry.
+ *
+ * Return: %0 on success, negative error code on failure.
+ */

drivers/gpu/drm/xe/xe_validation.c +30 -0
+int xe_validation_exec_lock(struct xe_validation_ctx *ctx,
+			    struct drm_gpuvm_exec *vm_exec,
+			    struct xe_validation_device *val)
+{
+	int ret;
+
+	memset(ctx, 0, sizeof(*ctx));
+	ctx->exec = &vm_exec->exec;
+	ctx->flags = vm_exec->flags;
+	ctx->val = val;
+retry:
+	ret = xe_validation_lock(ctx);
+	if (ret)
+		return ret;
+
+	ret = drm_gpuvm_exec_lock(vm_exec);
+	if (ret) {
+		xe_validation_unlock(ctx);
+		if (__xe_validation_should_retry(ctx, ret))
+			goto retry;
+	}
+
+	return ret;
+}
+
+/**
+ * xe_validation_ctx_fini() - Finalize a validation transaction
+ * @ctx: The Validation transaction to finalize.
+ *
+ * Finalize a validation transaction and its related drm_exec

transaction.
+ */

drivers/gpu/drm/xe/xe_validation.c +8 -0
+void xe_validation_ctx_fini(struct xe_validation_ctx *ctx)
+{
+	drm_exec_fini(ctx->exec);
+	xe_validation_unlock(ctx);
+}
+
+/**
+ * xe_validation_should_retry() - Determine if a validation

transaction should retry
+ * @ctx: The validation transaction.
+ * @ret: Pointer to a return value variable.
+ *
+ * Determines whether a validation transaction should retry based on the
+ * internal transaction state and the return value pointed to by @ret.
+ * If a validation should be retried, the transaction is prepared for that,
+ * and the validation locked might be re-locked in exclusive mode, and *@ret
+ * is set to %0. If the re-locking errors, typically due to interruptible
+ * locking with signal pending, *@ret is instead set to -EINTR and the
+ * function returns %false.
+ *
+ * Return: %true if validation should be retried, %false otherwise.
+ */
+bool xe_validation_should_retry(struct xe_validation_ctx *ctx, int *ret)

drivers/gpu/drm/xe/xe_validation.c +5 -0
+{
+	if (__xe_validation_should_retry(ctx, *ret)) {
+		drm_exec_fini(ctx->exec);
+		*ret = 0;
+		if (ctx->request_exclusive != ctx-
Quoted 1 line

lock_held_exclusive) {

drivers/gpu/drm/xe/xe_validation.c +9 -0
+			xe_validation_unlock(ctx);
+			*ret = xe_validation_lock(ctx);
+		}
+		drm_exec_init(ctx->exec, ctx->flags, ctx->nr);
+		return !*ret;
+	}
+
+	return false;
+}
drivers/gpu/drm/xe/xe_validation.h +0 -0
index db50feacad7a..a708c260cf18 100644
@@ -7,9 +7,11 @@


 #include <linux/dma-resv.h>
 #include <linux/types.h>
+#include <linux/rwsem.h>

 struct drm_exec;
 struct drm_gem_object;
+struct drm_gpuvm_exec;
 struct xe_device;

 #ifdef CONFIG_PROVE_LOCKING

drivers/gpu/drm/xe/xe_validation.h +0 -0
@@ -66,4 +68,109 @@ void xe_validation_assert_exec(const struct

xe_device *xe, const struct drm_exec
  } while (0)
 #endif

+/**
+ * struct xe_validation_device - The domain for exhaustive eviction
+ * @lock: The lock used to exclude other processes from allocating graphics memory
+ *
+ * The struct xe_validation_device represents the domain for which we want to use
+ * exhaustive eviction. The @lock is typically grabbed in read mode for allocations
+ * but when graphics memory allocation fails, it is retried with the write mode held.
+ */

drivers/gpu/drm/xe/xe_validation.h +6 -0
+struct xe_validation_device {
+	struct rw_semaphore lock;
+};
+
+/**
+ * struct xe_validation_ctx - A struct drm_exec subclass with

support for
+ * exhaustive eviction
+ * @exec: The drm_exec object base class. Note that we use a pointer instead of
+ * embedding to avoid diamond inheritance.
+ * @val: The exhaustive eviction domain.
+ * @lock_held: Whether The domain lock is currently held.
+ * @lock_held_exclusive: Whether the domain lock is held in exclusive mode.
+ * @request_exclusive: Whether to lock exclusively (write mode) the next time
+ * the domain lock is locked.
+ * @flags: The drm_exec flags used for drm_exec (re- )initialization.
+ * @nr: The drm_exec nr parameter used for drm_exec (re- )initializaiton.
+ */

drivers/gpu/drm/xe/xe_validation.h +11 -0
+struct xe_validation_ctx {
+	struct drm_exec *exec;
+	struct xe_validation_device *val;
+	bool lock_held;
+	bool lock_held_exclusive;
+	bool request_exclusive;
+	u32 flags;
+	unsigned int nr;
+};
+
+int xe_validation_ctx_init(struct xe_validation_ctx *ctx, struct

xe_validation_device *val,

drivers/gpu/drm/xe/xe_validation.h +1 -0
+			   struct drm_exec *exec, u32 flags,

unsigned int nr,

drivers/gpu/drm/xe/xe_validation.h +3 -0
+			   bool exclusive);
+
+int xe_validation_exec_lock(struct xe_validation_ctx *ctx, struct

drm_gpuvm_exec *vm_exec,

drivers/gpu/drm/xe/xe_validation.h +5 -0
+			    struct xe_validation_device *val);
+
+void xe_validation_ctx_fini(struct xe_validation_ctx *ctx);
+
+bool xe_validation_should_retry(struct xe_validation_ctx *ctx, int

*ret);

drivers/gpu/drm/xe/xe_validation.h +3 -0
+
+/**
+ * xe_validation_retry_on_oom() - Retry on oom in an xe_validaton

transaction
+ * @_ctx: Pointer to the xe_validation_ctx
+ * @_ret: The current error value possibly holding -ENOMEM
+ *
+ * Use this in way similar to drm_exec_retry_on_contention().
+ * If @_ret contains -ENOMEM the tranaction is restarted once in a way that
+ * blocks other transactions and allows exhastive eviction. If the transaction
+ * was already restarted once, Just return the -ENOMEM. May also set
+ * _ret to -EINTR if not retrying and waits are interruptible.
+ * May only be used within a drm_exec_until_all_locked() loop.
+ */
+#define xe_validation_retry_on_oom(_ctx,
_ret) \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+	do

{ \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+		if (xe_validation_should_retry(_ctx,

_ret)) \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+			goto

*__drm_exec_retry_ptr; \

drivers/gpu/drm/xe/xe_validation.h +4 -0
+	} while (0)
+
+/**
+ * xe_validation_device_init - Initialize a struct

xe_validation_device
+ * @val: The xe_validation_device to init.
+ */

drivers/gpu/drm/xe/xe_validation.h +14 -0
+static inline void
+xe_validation_device_init(struct xe_validation_device *val)
+{
+	init_rwsem(&val->lock);
+}
+
+/*
+ * Make guard() and scoped_guard() work with xe_validation_ctx
+ * so that we can exit transactions without caring about the
+ * cleanup.
+ */
+DEFINE_CLASS(xe_validation, struct xe_validation_ctx *,
+	     if (!IS_ERR(_T)) xe_validation_ctx_fini(_T);,
+	     ({_ret = xe_validation_ctx_init(_ctx, _val, _exec,

_flags, 0, _excl);

drivers/gpu/drm/xe/xe_validation.h +2 -0
+	       _ret ? NULL : _ctx; }),
+	     struct xe_validation_ctx *_ctx, struct

xe_validation_device *_val,

drivers/gpu/drm/xe/xe_validation.h +1 -0
+	     struct drm_exec *_exec, u32 _flags, int _ret, bool

_excl);
+static inline void *class_xe_validation_lock_ptr(class_xe_validation_t *_T)

drivers/gpu/drm/xe/xe_validation.h +5 -0
+{return *_T; }
+#define class_xe_validation_is_conditional false
+
+/**
+ * xe_validation_guard() - An auto-cleanup xe_validation_ctx

transaction
+ * @_ctx: The xe_validation_ctx.
+ * @_val: The xe_validation_device.
+ * @_exec: The struct drm_exec object
+ * @_flags: Flags for the drm_exec transaction. See the struct drm_exec documention!

Quoted 8 lines
Quoted 5 lines

+ * @_ret: Return in / out parameter. May be set by this macro. Typicall 0 when called.
+ * @_excl: Whether to start in exclusive mode already in the first iteration.
+ *

Same comment as above on function xe_validation_ctx_init wrt to arguments.

OK, let me know what you think given the above.

/Thomas

Quoted 17 lines

Matt

Quoted 14 lines

+ * This macro is will initiate a drm_exec transaction with additional support for
+ * exhaustive eviction.
+ */
+#define xe_validation_guard(_ctx, _val, _exec, _flags, _ret,
_excl) \

drivers/gpu/drm/xe/xe_validation.h +1 -0
+	scoped_guard(xe_validation, _ctx, _val, _exec, _flags,

_ret, _excl) \

drivers/gpu/drm/xe/xe_validation.h +2 -0
+	drm_exec_until_all_locked(_exec)
+

 #endif

Signature · 1 line
2.50.1
maintainer
#35 lore raw

Re: [PATCH 09/15] drm/xe: Convert the CPU fault handler for exhaustive eviction

In reply to Thomas Hellström
Quoted 42 lines · Thomas Hellström
Quoted 32 lines · Matthew Brost
Quoted 31 lines · Thomas Hellström

The CPU fault handler may populate bos and migrate, and in doing so might interfere with other tasks validing.

Convert it for exhaustive eviction. To do this properly without potentially introducing stalls with the mmap lock held requires TTM work. In the meantime, let's live with those stalls that would typically happen on memory pressure.

---
 drivers/gpu/drm/xe/xe_bo.c | 11 ++++++++---

 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +2 -1
index 5e40b6cb8d2a..dd1e0e9957e0 100644
@@ -1720,14 +1720,18 @@ static vm_fault_t xe_gem_fault(struct
 vm_fault *vmf)
  	struct xe_device *xe = to_xe_device(ddev);
  	struct xe_bo *bo = ttm_to_xe_bo(tbo);
  	bool needs_rpm = bo->flags & XE_BO_FLAG_VRAM_MASK;
-	struct drm_exec *exec;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
  	vm_fault_t ret;
  	int idx;


  if (needs_rpm)
  xe_pm_runtime_get(xe);

Quoted 8 lines

drivers/gpu/drm/xe/xe_bo.c static vm_fault_t xe_gem_fault(struct vm_fault *vmf) · line 1733

Quoted 6 lines

drivers/gpu/drm/xe/xe_bo.c +2 -1
-	exec = XE_VALIDATION_UNIMPLEMENTED;
+	if (xe_validation_ctx_init(&ctx, &xe->val, &exec,
+				   DRM_EXEC_INTERRUPTIBLE_WAIT, 0,

false))

drivers/gpu/drm/xe/xe_bo.c +1 -0
+		return VM_FAULT_NOPAGE;

Any particular reason to not use xe_validation_guard here?

Quoted 5 lines

Well this is a bit complicated ATM.
We would need some serious TTM rework here to support drm_exec in these helpers, and ATM I think upon closer inspection we'd need an xe_validation_ctx_init that doesn't initialize a drm_exec.

Right, so I think this is an unsupported case then.

Matt

Quoted 42 lines

ttm_bo_vm_reserve() might use a bo lock without a drm_exec and that will cause a lockdep splat if the drm_exec transaction has initialized the ww ctx, which happens in drm_exec_until_all_locked().

I should add a comment about that.

/Thomas

Quoted 31 lines

Matt

Quoted 28 lines

+
  ret = ttm_bo_vm_reserve(tbo, vmf);
  if (ret)
  goto out;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -1735,7 +1739,7 @@ static vm_fault_t xe_gem_fault(struct
 vm_fault *vmf)
  	if (drm_dev_enter(ddev, &idx)) {
  		trace_xe_bo_cpu_fault(bo);

drivers/gpu/drm/xe/xe_bo.c +1 -1
-		xe_validation_assert_exec(xe, exec, &tbo->base);
+		xe_validation_assert_exec(xe, &exec, &tbo->base);

  ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma-

Quoted 1 line

vm_page_prot,


TTM_BO_VM_NUM_PREFAULT);
  drm_dev_exit(idx);

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -1761,6 +1765,7 @@ static vm_fault_t xe_gem_fault(struct
 vm_fault *vmf)


  dma_resv_unlock(tbo->base.resv);
 out:

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	xe_validation_ctx_fini(&ctx);

  if (needs_rpm)
  xe_pm_runtime_put(xe);

Signature · 1 line
2.50.1
#21 lore raw

Re: [05/15] drm/xe: Introduce an xe_validation wrapper around drm_exec

In reply to Thomas Hellström

Hi,

Quoted 14 lines · Thomas Hellström
drivers/gpu/drm/xe/xe_validation.c +14 -0
+static int xe_validation_lock(struct xe_validation_ctx *ctx)
+{
+	struct xe_validation_device *val = ctx->val;
+	int ret = 0;
+
+	if (ctx->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) {
+		if (ctx->request_exclusive)
+			ret = down_write_killable(&val->lock);
+		else
+			ret = down_read_interruptible(&val->lock);
+	} else {
+		if (ctx->request_exclusive)
+			down_write(&val->lock);
+		else

drivers/gpu/drm/xe/xe_validation.c void xe_validation_assert_exec(const struct xe_device *xe, · line 75

Quoted 10 lines
drivers/gpu/drm/xe/xe_validation.c +10 -0
+			down_read(&val->lock);
+	}
+
+	if (!ret) {
+		ctx->lock_held = true;
+		ctx->lock_held_exclusive = ctx->request_exclusive;
+	}
+
+	return ret;
+}

This can fail if DRM_EXEC_INTERRUPTIBLE_WAIT is set, ...

Quoted 7 lines
drivers/gpu/drm/xe/xe_validation.c +7 -0
+int xe_validation_ctx_init(struct xe_validation_ctx *ctx, struct xe_validation_device *val,
+			   struct drm_exec *exec, u32 flags, unsigned int nr,
+			   bool exclusive)
+{
+	int ret;
+
+	ctx->exec = exec;

drivers/gpu/drm/xe/xe_validation.c void xe_validation_assert_exec(const struct xe_device *xe, · line 126

Quoted 10 lines
drivers/gpu/drm/xe/xe_validation.c +10 -0
+	ctx->val = val;
+	ctx->lock_held = false;
+	ctx->lock_held_exclusive = false;
+	ctx->request_exclusive = exclusive;
+	ctx->flags = flags;
+	ctx->nr = nr;
+
+	ret = xe_validation_lock(ctx);
+	if (ret)
+		return ret;

... causing an error to be returned here, which...

drivers/gpu/drm/xe/xe_validation.h void xe_validation_assert_exec(const struct xe_device *xe, const struct drm_exec · line 155

Quoted 6 lines
drivers/gpu/drm/xe/xe_validation.h +6 -0
+DEFINE_CLASS(xe_validation, struct xe_validation_ctx *,
+	     if (!IS_ERR(_T)) xe_validation_ctx_fini(_T);,
+	     ({_ret = xe_validation_ctx_init(_ctx, _val, _exec, _flags, 0, _excl);
+	       _ret ? NULL : _ctx; }),
+	     struct xe_validation_ctx *_ctx, struct xe_validation_device *_val,
+	     struct drm_exec *_exec, u32 _flags, int _ret, bool _excl);

... causes a NULL pointer to be recorded for the scoped guard here, which is then passed to xe_validation_ctx_fini on scope exit, causing

Kernel attempted to read user page (0) - exploit attempt? (uid: 1000)

BUG: Kernel NULL pointer dereference on read at 0x00000000
Faulting instruction address: 0xc008000014bcf2a8
Oops: Kernel access of bad area, sig: 11 [#1]
LE PAGE_SIZE=64K MMU=Radix  SMP NR_CPUS=2048 NUMA PowerNV
Modules linked in: xt_conntrack nft_chain_nat xt_MASQUERADE nf_nat nf_conntrack_netlink nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 xfrm_user xfrm_algo xt_addrtype nft_compat x_tables nf_tables nfnetlink br_netfilter bridge stp llc overlay binfmt_misc mei_gsc mei_me snd_hda_codec_intelhdmi mei snd_hda_codec_hdmi mtd_intel_dg xe joydev evdev drm_gpuvm drm_buddy gpu_sched drm_exec drm_suballoc_helper drm_ttm_helper ttm drm_display_helper hid_generic usbhid cec hid rc_core drm_client_lib drm_kms_helper snd_hda_intel snd_intel_dspcfg drm snd_hda_codec snd_hda_core aes_gcm_p10_crypto crypto_simd snd_hwdep cryptd xts snd_pcm drm_panel_orientation_quirks ghash_generic ofpart snd_timer vmx_crypto powernv_flash i2c_algo_bit snd ipmi_powernv gf128mul mtd ipmi_devintf configfs ipmi_msghandler opal_prd at24 soundcore regmap_i2c ext4 crc16 mbcache jbd2 dm_mod xhci_pci xhci_hcd nvme tg3 usbcore nvme_core libphy nvme_keyring nvme_auth mdio_bus usb_common
CPU: 24 UID: 0 PID: 2438 Comm: Xorg Not tainted 6.17.0-rc1+ #1 VOLUNTARY
Hardware name: T2P9D01 REV 1.01 POWER9 0x4e1202 opal:skiboot-9858186 PowerNV
NIP:  c008000014bcf2a8 LR: c008000014bd5724 CTR: c0080000141a0248
REGS: c00000000fa0f4a0 TRAP: 0300   Not tainted  (6.17.0-rc1+)
MSR:  900000000280b033 <SF,HV,VEC,VSX,EE,FP,ME,IR,DR,RI,LE>  CR: 88002844  XER: 00000

CFAR: c008000014bd5720 DAR: 0000000000000000 DSISR: 40000000 IRQMASK: 0
GPR00: c008000014bd5724 c00000000fa0f740 c008000014df9200 0000000000000000
GPR04: c000000023340000 c000000023340000 0000000000000031 fffffffffffe0000
GPR08: c00000002d0075a8 fffffffffffff000 0000000000000000 c008000014d8d3b8
GPR12: c0080000141a0248 c0000007fffcc800 0000000000000000 0000000000000000
GPR16: 0000000000000001 0000000000000000 0000000000000001 c0002000084dff40
GPR20: c000000019230308 fffffffffffff000 0000000000000000 fffffffffffff000
GPR24: 0000000000000000 0000000000000000 0000000000000001 c0002000134ab000
GPR28: 0000000000000000 c00020001333e818 0000000000000000 0000000000000000 NIP [c008000014bcf2a8] xe_validation_ctx_fini+0x20/0x90 [xe] LR [c008000014bd5724] new_vma+0x32c/0x400 [xe]

Call Trace:

[c00000000fa0f740] [0000000000000001] 0x1 (unreliable) [c00000000fa0f770] [c008000014bd5724] new_vma+0x32c/0x400 [xe] [c00000000fa0f860] [c008000014bd5abc] vm_bind_ioctl_ops_parse+0x2c4/0x9b0 [xe] [c00000000fa0f920] [c008000014bd9dac] xe_vm_bind_ioctl+0x1344/0x17a0 [xe] [c00000000fa0faf0] [c00800001349ca58] drm_ioctl_kernel+0x100/0x1a0 [drm] [c00000000fa0fb50] [c00800001349cd88] drm_ioctl+0x290/0x690 [drm] [c00000000fa0fcc0] [c008000014b1fcdc] xe_drm_ioctl+0x74/0xd0 [xe] [c00000000fa0fd10] [c0000000006ec654] sys_ioctl+0x594/0x1020 [c00000000fa0fe10] [c00000000002c020] system_call_exception+0x120/0x240 [c00000000fa0fe50] [c00000000000cfdc] system_call_vectored_common+0x15c/0x2ec

Simon

maintainer
#22 lore raw

Re: [05/15] drm/xe: Introduce an xe_validation wrapper around drm_exec

Quoted 51 lines · Simon Richter

Hi,

Quoted 14 lines · Thomas Hellström
drivers/gpu/drm/xe/xe_validation.c +14 -0
+static int xe_validation_lock(struct xe_validation_ctx *ctx)
+{
+	struct xe_validation_device *val = ctx->val;
+	int ret = 0;
+
+	if (ctx->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) {
+		if (ctx->request_exclusive)
+			ret = down_write_killable(&val->lock);
+		else
+			ret = down_read_interruptible(&val->lock);
+	} else {
+		if (ctx->request_exclusive)
+			down_write(&val->lock);
+		else

drivers/gpu/drm/xe/xe_validation.c void xe_validation_assert_exec(const struct xe_device *xe, · line 75

Quoted 10 lines
drivers/gpu/drm/xe/xe_validation.c +10 -0
+			down_read(&val->lock);
+	}
+
+	if (!ret) {
+		ctx->lock_held = true;
+		ctx->lock_held_exclusive = ctx->request_exclusive;
+	}
+
+	return ret;
+}

This can fail if DRM_EXEC_INTERRUPTIBLE_WAIT is set, ...

Quoted 7 lines
drivers/gpu/drm/xe/xe_validation.c +7 -0
+int xe_validation_ctx_init(struct xe_validation_ctx *ctx, struct xe_validation_device *val,
+			   struct drm_exec *exec, u32 flags, unsigned int nr,
+			   bool exclusive)
+{
+	int ret;
+
+	ctx->exec = exec;

drivers/gpu/drm/xe/xe_validation.c void xe_validation_assert_exec(const struct xe_device *xe, · line 126

Quoted 10 lines
drivers/gpu/drm/xe/xe_validation.c +10 -0
+	ctx->val = val;
+	ctx->lock_held = false;
+	ctx->lock_held_exclusive = false;
+	ctx->request_exclusive = exclusive;
+	ctx->flags = flags;
+	ctx->nr = nr;
+
+	ret = xe_validation_lock(ctx);
+	if (ret)
+		return ret;

... causing an error to be returned here, which...

Quoted 10 lines

drivers/gpu/drm/xe/xe_validation.h void xe_validation_assert_exec(const struct xe_device *xe, const struct drm_exec · line 155

Quoted 6 lines
drivers/gpu/drm/xe/xe_validation.h +6 -0
+DEFINE_CLASS(xe_validation, struct xe_validation_ctx *,
+	     if (!IS_ERR(_T)) xe_validation_ctx_fini(_T);,
+	     ({_ret = xe_validation_ctx_init(_ctx, _val, _exec, _flags, 0, _excl);
+	       _ret ? NULL : _ctx; }),
+	     struct xe_validation_ctx *_ctx, struct xe_validation_device *_val,
+	     struct drm_exec *_exec, u32 _flags, int _ret, bool _excl);

... causes a NULL pointer to be recorded for the scoped guard here, which is then passed to xe_validation_ctx_fini on scope exit, causing

Yep. Thomas and I agree [1] this is a bug.

Matt

[1] https://patchwork.freedesktop.org/patch/668295/?series=152882&rev=1#comment_1225697

Quoted 36 lines

Kernel attempted to read user page (0) - exploit attempt? (uid: 1000)

BUG: Kernel NULL pointer dereference on read at 0x00000000
Faulting instruction address: 0xc008000014bcf2a8
Oops: Kernel access of bad area, sig: 11 [#1]
LE PAGE_SIZE=64K MMU=Radix  SMP NR_CPUS=2048 NUMA PowerNV
Modules linked in: xt_conntrack nft_chain_nat xt_MASQUERADE nf_nat nf_conntrack_netlink nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 xfrm_user xfrm_algo xt_addrtype nft_compat x_tables nf_tables nfnetlink br_netfilter bridge stp llc overlay binfmt_misc mei_gsc mei_me snd_hda_codec_intelhdmi mei snd_hda_codec_hdmi mtd_intel_dg xe joydev evdev drm_gpuvm drm_buddy gpu_sched drm_exec drm_suballoc_helper drm_ttm_helper ttm drm_display_helper hid_generic usbhid cec hid rc_core drm_client_lib drm_kms_helper snd_hda_intel snd_intel_dspcfg drm snd_hda_codec snd_hda_core aes_gcm_p10_crypto crypto_simd snd_hwdep cryptd xts snd_pcm drm_panel_orientation_quirks ghash_generic ofpart snd_timer vmx_crypto powernv_flash i2c_algo_bit snd ipmi_powernv gf128mul mtd ipmi_devintf configfs ipmi_msghandler opal_prd at24 soundcore regmap_i2c ext4 crc16 mbcache jbd2 dm_mod xhci_pci xhci_hcd nvme tg3 usbcore nvme_core libphy nvme_keyring nvme_auth mdio_bus usb_common
CPU: 24 UID: 0 PID: 2438 Comm: Xorg Not tainted 6.17.0-rc1+ #1 VOLUNTARY
Hardware name: T2P9D01 REV 1.01 POWER9 0x4e1202 opal:skiboot-9858186 PowerNV
NIP:  c008000014bcf2a8 LR: c008000014bd5724 CTR: c0080000141a0248
REGS: c00000000fa0f4a0 TRAP: 0300   Not tainted  (6.17.0-rc1+)
MSR:  900000000280b033 <SF,HV,VEC,VSX,EE,FP,ME,IR,DR,RI,LE>  CR: 88002844  XER: 00000

CFAR: c008000014bd5720 DAR: 0000000000000000 DSISR: 40000000 IRQMASK: 0
GPR00: c008000014bd5724 c00000000fa0f740 c008000014df9200 0000000000000000
GPR04: c000000023340000 c000000023340000 0000000000000031 fffffffffffe0000
GPR08: c00000002d0075a8 fffffffffffff000 0000000000000000 c008000014d8d3b8
GPR12: c0080000141a0248 c0000007fffcc800 0000000000000000 0000000000000000
GPR16: 0000000000000001 0000000000000000 0000000000000001 c0002000084dff40
GPR20: c000000019230308 fffffffffffff000 0000000000000000 fffffffffffff000
GPR24: 0000000000000000 0000000000000000 0000000000000001 c0002000134ab000
GPR28: 0000000000000000 c00020001333e818 0000000000000000 0000000000000000 NIP [c008000014bcf2a8] xe_validation_ctx_fini+0x20/0x90 [xe] LR [c008000014bd5724] new_vma+0x32c/0x400 [xe]

Call Trace:

[c00000000fa0f740] [0000000000000001] 0x1 (unreliable) [c00000000fa0f770] [c008000014bd5724] new_vma+0x32c/0x400 [xe] [c00000000fa0f860] [c008000014bd5abc] vm_bind_ioctl_ops_parse+0x2c4/0x9b0 [xe] [c00000000fa0f920] [c008000014bd9dac] xe_vm_bind_ioctl+0x1344/0x17a0 [xe] [c00000000fa0faf0] [c00800001349ca58] drm_ioctl_kernel+0x100/0x1a0 [drm] [c00000000fa0fb50] [c00800001349cd88] drm_ioctl+0x290/0x690 [drm] [c00000000fa0fcc0] [c008000014b1fcdc] xe_drm_ioctl+0x74/0xd0 [xe] [c00000000fa0fd10] [c0000000006ec654] sys_ioctl+0x594/0x1020 [c00000000fa0fe10] [c00000000002c020] system_call_exception+0x120/0x240 [c00000000fa0fe50] [c00000000000cfdc] system_call_vectored_common+0x15c/0x2ec

Simon

#23 lore raw

Re: [05/15] drm/xe: Introduce an xe_validation wrapper around drm_exec

Hi,

Quoted 4 lines · Matthew Brost
Quoted 2 lines

... causes a NULL pointer to be recorded for the scoped guard here, which is then passed to xe_validation_ctx_fini on scope exit, causing

Yep. Thomas and I agree [1] this is a bug.

Ah, excellent.

It is also really easy to trigger this, happened after a few seconds in during a Piglit run for me, on a single GPU as a normal user, just a lot of clients starting and stopping in parallel.

Simon

author maintainer
#36 lore raw

Re: [PATCH 09/15] drm/xe: Convert the CPU fault handler for exhaustive eviction

In reply to Matthew Brost
Quoted 46 lines · Matthew Brost
Quoted 45 lines · Thomas Hellström
Quoted 43 lines · Matthew Brost
Quoted 39 lines · Thomas Hellström

The CPU fault handler may populate bos and migrate, and in doing
so might interfere with other tasks validing.

Convert it for exhaustive eviction. To do this properly without potentially introducing stalls with the mmap lock held requires TTM work. In the meantime, let's live with those stalls that would typically happen on memory pressure.

Signed-off-by: Thomas Hellström
<[email protected]>
---
 drivers/gpu/drm/xe/xe_bo.c | 11 ++++++++---

 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +2 -1
index 5e40b6cb8d2a..dd1e0e9957e0 100644
@@ -1720,14 +1720,18 @@ static vm_fault_t xe_gem_fault(struct
 vm_fault *vmf)
  	struct xe_device *xe = to_xe_device(ddev);
  	struct xe_bo *bo = ttm_to_xe_bo(tbo);
  	bool needs_rpm = bo->flags & XE_BO_FLAG_VRAM_MASK;
-	struct drm_exec *exec;
+	struct xe_validation_ctx ctx;
+	struct drm_exec exec;
  	vm_fault_t ret;
  	int idx;


  if (needs_rpm)
  xe_pm_runtime_get(xe);

drivers/gpu/drm/xe/xe_bo.c +2 -1
-	exec = XE_VALIDATION_UNIMPLEMENTED;
+	if (xe_validation_ctx_init(&ctx, &xe->val, &exec,
+				  

DRM_EXEC_INTERRUPTIBLE_WAIT, 0,
false))

drivers/gpu/drm/xe/xe_bo.c static vm_fault_t xe_gem_fault(struct vm_fault *vmf) · line 1733

Quoted 1 line
drivers/gpu/drm/xe/xe_bo.c +1 -0
+		return VM_FAULT_NOPAGE;

Any particular reason to not use xe_validation_guard here?

Quoted 8 lines
Quoted 6 lines

Well this is a bit complicated ATM.
We would need some serious TTM rework here to support drm_exec in these
helpers, and ATM I think upon closer inspection we'd need an xe_validation_ctx_init that doesn't initialize a drm_exec.

Right, so I think this is an unsupported case then.

We should be able to re-lock in write-mode, though. Let me have a look at this in v2.

Thanks,
Thomas

Quoted 48 lines

Matt

Quoted 45 lines

ttm_bo_vm_reserve() might use a bo lock without a drm_exec and that will cause a lockdep splat if the drm_exec transaction has initialized
the ww ctx, which happens in drm_exec_until_all_locked().

I should add a comment about that.

/Thomas

Quoted 33 lines

Matt

Quoted 30 lines

+
  ret = ttm_bo_vm_reserve(tbo, vmf);
  if (ret)
  goto out;

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -1735,7 +1739,7 @@ static vm_fault_t xe_gem_fault(struct
 vm_fault *vmf)
  	if (drm_dev_enter(ddev, &idx)) {
  		trace_xe_bo_cpu_fault(bo);

drivers/gpu/drm/xe/xe_bo.c +0 -1
-		xe_validation_assert_exec(xe, exec, &tbo-
Quoted 1 line

base);

drivers/gpu/drm/xe/xe_bo.c +1 -0
+		xe_validation_assert_exec(xe, &exec, &tbo-
Quoted 1 line

base);

  ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma-

Quoted 1 line

vm_page_prot,


TTM_BO_VM_NUM_PREFAULT);
  drm_dev_exit(idx);

drivers/gpu/drm/xe/xe_bo.c +0 -0
@@ -1761,6 +1765,7 @@ static vm_fault_t xe_gem_fault(struct
 vm_fault *vmf)


  dma_resv_unlock(tbo->base.resv);
 out:

drivers/gpu/drm/xe/xe_bo.c +1 -0
+	xe_validation_ctx_fini(&ctx);

  if (needs_rpm)
  xe_pm_runtime_put(xe);

Signature · 1 line
2.50.1
author maintainer
#24 lore raw

Re: [05/15] drm/xe: Introduce an xe_validation wrapper around drm_exec

In reply to Simon Richter
Quoted 122 lines · Simon Richter

Hi,

Quoted 14 lines · Thomas Hellström
drivers/gpu/drm/xe/xe_validation.c +14 -0
+static int xe_validation_lock(struct xe_validation_ctx *ctx)
+{
+	struct xe_validation_device *val = ctx->val;
+	int ret = 0;
+
+	if (ctx->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) {
+		if (ctx->request_exclusive)
+			ret = down_write_killable(&val->lock);
+		else
+			ret = down_read_interruptible(&val->lock);
+	} else {
+		if (ctx->request_exclusive)
+			down_write(&val->lock);
+		else

drivers/gpu/drm/xe/xe_validation.c void xe_validation_assert_exec(const struct xe_device *xe, · line 75

Quoted 10 lines
drivers/gpu/drm/xe/xe_validation.c +10 -0
+			down_read(&val->lock);
+	}
+
+	if (!ret) {
+		ctx->lock_held = true;
+		ctx->lock_held_exclusive = ctx->request_exclusive;
+	}
+
+	return ret;
+}

This can fail if DRM_EXEC_INTERRUPTIBLE_WAIT is set, ...

Quoted 9 lines

+int xe_validation_ctx_init(struct xe_validation_ctx *ctx, struct xe_validation_device *val,

(quoted diff) +1 -0
+			   struct drm_exec *exec, u32 flags,

unsigned int nr,

drivers/gpu/drm/xe/xe_validation.c +5 -0
+			   bool exclusive)
+{
+	int ret;
+
+	ctx->exec = exec;

drivers/gpu/drm/xe/xe_validation.c void xe_validation_assert_exec(const struct xe_device *xe, · line 126

Quoted 10 lines
drivers/gpu/drm/xe/xe_validation.c +10 -0
+	ctx->val = val;
+	ctx->lock_held = false;
+	ctx->lock_held_exclusive = false;
+	ctx->request_exclusive = exclusive;
+	ctx->flags = flags;
+	ctx->nr = nr;
+
+	ret = xe_validation_lock(ctx);
+	if (ret)
+		return ret;

... causing an error to be returned here, which...

Quoted 9 lines
drivers/gpu/drm/xe/xe_validation.h +3 -0
+DEFINE_CLASS(xe_validation, struct xe_validation_ctx *,
+	     if (!IS_ERR(_T)) xe_validation_ctx_fini(_T);,
+	     ({_ret = xe_validation_ctx_init(_ctx, _val, _exec,

_flags, 0, _excl);

(quoted diff) +2 -0
+	       _ret ? NULL : _ctx; }),
+	     struct xe_validation_ctx *_ctx, struct

xe_validation_device *_val,

(quoted diff) +1 -0
+	     struct drm_exec *_exec, u32 _flags, int _ret, bool

_excl);

... causes a NULL pointer to be recorded for the scoped guard here, which
is then passed to xe_validation_ctx_fini on scope exit, causing

Kernel attempted to read user page (0) - exploit attempt? (uid: 1000)

BUG: Kernel NULL pointer dereference on read at 0x00000000
Faulting instruction address: 0xc008000014bcf2a8
Oops: Kernel access of bad area, sig: 11 [#1]
LE PAGE_SIZE=64K MMU=Radix  SMP NR_CPUS=2048 NUMA PowerNV
Modules linked in: xt_conntrack nft_chain_nat xt_MASQUERADE nf_nat

nf_conntrack_netlink nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 xfrm_user xfrm_algo xt_addrtype nft_compat x_tables nf_tables nfnetlink br_netfilter bridge stp llc overlay binfmt_misc mei_gsc mei_me snd_hda_codec_intelhdmi mei snd_hda_codec_hdmi mtd_intel_dg xe joydev evdev drm_gpuvm drm_buddy gpu_sched drm_exec drm_suballoc_helper drm_ttm_helper ttm drm_display_helper hid_generic usbhid cec hid rc_core drm_client_lib drm_kms_helper snd_hda_intel snd_intel_dspcfg drm snd_hda_codec snd_hda_core aes_gcm_p10_crypto crypto_simd snd_hwdep cryptd xts snd_pcm drm_panel_orientation_quirks ghash_generic ofpart snd_timer vmx_crypto powernv_flash i2c_algo_bit snd ipmi_powernv gf128mul mtd ipmi_devintf configfs ipmi_msghandler opal_prd at24 soundcore regmap_i2c ext4 crc16 mbcache jbd2 dm_mod xhci_pci xhci_hcd nvme tg3 usbcore nvme_core libphy nvme_keyring nvme_auth mdio_bus usb_common

CPU: 24 UID: 0 PID: 2438 Comm: Xorg Not tainted 6.17.0-rc1+ #1
VOLUNTARY
Hardware name: T2P9D01 REV 1.01 POWER9 0x4e1202 opal:skiboot-9858186

PowerNV
NIP:  c008000014bcf2a8 LR: c008000014bd5724 CTR: c0080000141a0248
REGS: c00000000fa0f4a0 TRAP: 0300   Not tainted  (6.17.0-rc1+) MSR:  900000000280b033 <SF,HV,VEC,VSX,EE,FP,ME,IR,DR,RI,LE>  CR:
88002844  XER: 00000

CFAR: c008000014bd5720 DAR: 0000000000000000 DSISR: 40000000 IRQMASK:
0
GPR00: c008000014bd5724 c00000000fa0f740 c008000014df9200 0000000000000000
GPR04: c000000023340000 c000000023340000 0000000000000031 fffffffffffe0000
GPR08: c00000002d0075a8 fffffffffffff000 0000000000000000 c008000014d8d3b8
GPR12: c0080000141a0248 c0000007fffcc800 0000000000000000 0000000000000000
GPR16: 0000000000000001 0000000000000000 0000000000000001 c0002000084dff40
GPR20: c000000019230308 fffffffffffff000 0000000000000000 fffffffffffff000
GPR24: 0000000000000000 0000000000000000 0000000000000001 c0002000134ab000
GPR28: 0000000000000000 c00020001333e818 0000000000000000 0000000000000000
NIP [c008000014bcf2a8] xe_validation_ctx_fini+0x20/0x90 [xe] LR [c008000014bd5724] new_vma+0x32c/0x400 [xe]

Call Trace:
[c00000000fa0f740] [0000000000000001] 0x1 (unreliable)
[c00000000fa0f770] [c008000014bd5724] new_vma+0x32c/0x400 [xe]
[c00000000fa0f860] [c008000014bd5abc]
vm_bind_ioctl_ops_parse+0x2c4/0x9b0 [xe]
[c00000000fa0f920] [c008000014bd9dac] xe_vm_bind_ioctl+0x1344/0x17a0
Quoted 10 lines

[xe]
[c00000000fa0faf0] [c00800001349ca58] drm_ioctl_kernel+0x100/0x1a0 [drm]
[c00000000fa0fb50] [c00800001349cd88] drm_ioctl+0x290/0x690 [drm] [c00000000fa0fcc0] [c008000014b1fcdc] xe_drm_ioctl+0x74/0xd0 [xe] [c00000000fa0fd10] [c0000000006ec654] sys_ioctl+0x594/0x1020 [c00000000fa0fe10] [c00000000002c020] system_call_exception+0x120/0x240
[c00000000fa0fe50] [c00000000000cfdc] system_call_vectored_common+0x15c/0x2ec

Right, that's been pointed out also by Matt Brost's review. The idea here is that if the interruptible lock returns -EINTR, then we should not enter the code within xe_validation_guard() either so on top of the fix for this, it looks like I need to mark it as conditional.

Thanks,
Thomas

Quoted 2 lines

   Simon

author maintainer
#13 lore raw

Re: [PATCH 04/15] drm/xe: Pass down drm_exec context to validation

In reply to Matthew Brost
Quoted 7 lines · Matthew Brost
Quoted 7 lines

(quoted diff) +2 -0
+	/**
+	 * @validation: Validation data only valid with the vm

resv held.

(quoted diff) +1 -0
+	 * Note: This is really task state of the task holding the

vm resv,

drivers/gpu/drm/xe/xe_vm_types.h +1 -0
+	 * and moving forward we should
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm_types.h struct xe_vm { · line 320

Quoted 3 lines
(quoted diff) +1 -0
+	 * come up with a better way of passing this down the

call-

drivers/gpu/drm/xe/xe_vm_types.h +1 -0
+	 * chain.

I've already mentioned this, attaching the _exec xe_vma_ops might be good option as xe_vma_ops has lifetime of only existing for the bind (i.e., it is stack variable) so you'd only need to set it (i.e., no clear required).

That is true, although since this is really task state the correct thing would be to pass this as a parameter to the functions involved, but when I tried to implement that, it got quite complicated.

So figuring saving this for a future cleanup might make sense. I'll do that for v2 anyway, and we can discuss it further.

Thanks,
Thomas

Quoted 6 lines

I think patch largely makes sense.

Matt

Quoted 1 line

+