All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] drm/amdgpu: bind imported BOs before mapping them into a VM
@ 2026-08-11  8:07 Yifan Zhang
  2026-08-11 12:27 ` Christian König
  0 siblings, 1 reply; 3+ messages in thread
From: Yifan Zhang @ 2026-08-11  8:07 UTC (permalink / raw)
  To: amd-gfx
  Cc: Alexander.Deucher, Christian.Koenig, Perry.Yuan, Prerona Ghosh,
	Yifan Zhang

From: Prerona Ghosh <Prerona.Ghosh@amd.com>

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 <yifan1.zhang@amd.com>
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


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

* Re: [PATCH v2] drm/amdgpu: bind imported BOs before mapping them into a VM
  2026-08-11  8:07 [PATCH v2] drm/amdgpu: bind imported BOs before mapping them into a VM Yifan Zhang
@ 2026-08-11 12:27 ` Christian König
  2026-08-12  7:16   ` Zhang, Yifan
  0 siblings, 1 reply; 3+ messages in thread
From: Christian König @ 2026-08-11 12:27 UTC (permalink / raw)
  To: Yifan Zhang, amd-gfx; +Cc: Alexander.Deucher, Perry.Yuan, Prerona Ghosh

On 8/11/26 10:07, Yifan Zhang wrote:
> From: Prerona Ghosh <Prerona.Ghosh@amd.com>
> 
> 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.

Well that is a good catch, but clear NAK to this hacky workaround.

I suggested a long time ago already to change this behavior and validate inside amdgpu_gem_object_open(), IIRC we even had patches for that on the mailing list.

Did we accidentally dropped those?

Regards,
Christian.

> 
> 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 <yifan1.zhang@amd.com>
> 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,


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

* RE: [PATCH v2] drm/amdgpu: bind imported BOs before mapping them into a VM
  2026-08-11 12:27 ` Christian König
@ 2026-08-12  7:16   ` Zhang, Yifan
  0 siblings, 0 replies; 3+ messages in thread
From: Zhang, Yifan @ 2026-08-12  7:16 UTC (permalink / raw)
  To: Koenig, Christian, amd-gfx@lists.freedesktop.org
  Cc: Deucher, Alexander, Yuan, Perry, Ghosh, Prerona

AMD General

Hi Christian,

v3 moves the validation into amdgpu_gem_object_open() as you suggested,so imports are bound once at open time and graphics and compute VMs follow the same rule.

I couldn't find the earlier patches you mentioned on the list, so this is a fresh implementation. Please let me know if I missed them.

Best Regards,
Yifan

-----Original Message-----
From: Koenig, Christian <Christian.Koenig@amd.com>
Sent: Tuesday, August 11, 2026 8:28 PM
To: Zhang, Yifan <Yifan1.Zhang@amd.com>; amd-gfx@lists.freedesktop.org
Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Yuan, Perry <Perry.Yuan@amd.com>; Ghosh, Prerona <Prerona.Ghosh@amd.com>
Subject: Re: [PATCH v2] drm/amdgpu: bind imported BOs before mapping them into a VM

On 8/11/26 10:07, Yifan Zhang wrote:
> From: Prerona Ghosh <Prerona.Ghosh@amd.com>
>
> 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.

Well that is a good catch, but clear NAK to this hacky workaround.

I suggested a long time ago already to change this behavior and validate inside amdgpu_gem_object_open(), IIRC we even had patches for that on the mailing list.

Did we accidentally dropped those?

Regards,
Christian.

>
> 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 <yifan1.zhang@amd.com>
> 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,


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

end of thread, other threads:[~2026-08-12  7:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-11  8:07 [PATCH v2] drm/amdgpu: bind imported BOs before mapping them into a VM Yifan Zhang
2026-08-11 12:27 ` Christian König
2026-08-12  7:16   ` Zhang, Yifan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.