[PATCH v2] drm/amdgpu: bind imported BOs before mapping them into a VM

Yifan Zhang <[email protected]>
Newsgroups org.freedesktop.lists.amd-gfx
Message-ID <[email protected]>
From: Prerona Ghosh <[email protected]>

An imported dma-buf with a dynamic attachment is not bound to GTT until
it is validated. In a VM that is not a KFD compute context nothing does
that: amdgpu_gem_object_open() only validates and fences imports for
compute VMs, and clients submitting through HW queues never go through
amdgpu_cs, so amdgpu_vm_validate() does not run either.

AMDGPU_GEM_VA then maps the BO while its resource is still
TTM_PL_SYSTEM. amdgpu_ttm_tt_pde_flags() drops AMDGPU_PTE_VALID and
AMDGPU_PTE_SYSTEM for that memory type, so the range is programmed with
PTE flags 0x60 (readable and writeable only) and the first GPU access to
it faults:

 amdgpu 0000:26:00.0: [gfxhub0] retry page fault (src_id:0 ring:0 vmid:3 pasid:46)
 amdgpu 0000:26:00.0:   in page starting at address 0x00007f142d6d8000 from IH client 0x1b (UTCL2)
 amdgpu 0000:26:00.0: VM_L2_PROTECTION_FAULT_STATUS:0x00301011
 amdgpu 0000:26:00.0:      Faulty UTCL2 client ID: TCP (0x8)
 amdgpu 0000:26:00.0:      PERMISSION_FAULTS: 0x1

Validate imported BOs into their allowed domains before MAP and REPLACE
so that the mapping is always created from a bound resource.

Signed-off-by: Yifan Zhang <[email protected]>
Assisted-by: Claude:opus-5
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 29 +++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
index f754a4a3a1c2..214ae2a95da2 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
@@ -747,6 +747,27 @@ int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
 	return r;
 }
 
+/**
+ * amdgpu_gem_va_make_resident - bind an imported BO before it gets mapped
+ *
+ * @bo: the BO about to be mapped into a VM
+ *
+ * Imported dma-bufs with a dynamic attachment stay unbound until they are
+ * validated. Mapping one while it is still in TTM_PL_SYSTEM would program
+ * PTEs without AMDGPU_PTE_VALID and any GPU access to them faults.
+ */
+static int amdgpu_gem_va_make_resident(struct amdgpu_bo *bo)
+{
+	struct ttm_operation_ctx ctx = { true, false };
+
+	if (bo->tbo.resource &&
+	    bo->tbo.resource->mem_type != TTM_PL_SYSTEM)
+		return 0;
+
+	amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
+	return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+}
+
 /**
  * amdgpu_gem_va_update_vm -update the bo_va in its VM
  *
@@ -962,6 +983,14 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 	if (r)
 		goto error;
 
+	if (abo && drm_gem_is_imported(&abo->tbo.base) &&
+	    (args->operation == AMDGPU_VA_OP_MAP ||
+	     args->operation == AMDGPU_VA_OP_REPLACE)) {
+		r = amdgpu_gem_va_make_resident(abo);
+		if (r)
+			goto error;
+	}
+
 	switch (args->operation) {
 	case AMDGPU_VA_OP_MAP:
 		r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.