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

Yifan Zhang <[email protected]> Tue, 4 Aug 2026 21:31:10 +0800
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 | 31 +++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
index 6a0699746fbc..d1ffc4e2f501 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
@@ -726,6 +726,30 @@ 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 (!drm_gem_is_imported(&bo->tbo.base))
+		return 0;
+
+	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
  *
@@ -941,6 +965,13 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 	if (r)
 		goto error;
 
+	if (abo && (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