Skip to contentSkip to conversation

amdgpu/ttm: make sure exported objects are populated (v2)

PATCH In review Dave Airlie 3 participants 3 messages Sep 2, 2025

Subsystems (4)
lore
Share & tools
lore

Dave Airlie

author maintainer
#0 lore raw

PATCH 1/1 amdgpu/ttm: make sure exported objects are populated (v2)

From: Dave Airlie <[email protected]>

While discussing cgroups we noticed a problem where you could export a BO to a dma-buf without having it ever being backed or accounted for.

This meant in low memory situations or eventually with cgroups, a lower privledged process might cause the compositor to try and allocate a lot of memory on it's behalf and this could fail. At least make sure the exporter has managed to allocate the RAM at least once before exporting the object.

This only applies currently to TTM_PL_SYSTEM objects, because GTT objects get populated on first validate, and VRAM doesn't use TT.

TODO:
other drivers?
split this into two?

Cc Christian Koenig <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Simona Vetter <[email protected]>

---
v2: let the user choose interruptible (do we need the whole op ctx?) stop checking things outside the reservation, just populate always.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c |  5 +++++
 drivers/gpu/drm/ttm/ttm_bo.c                | 23 +++++++++++++++++++++
 include/drm/ttm/ttm_bo.h                    |  2 ++
 3 files changed, 30 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +5 -0
index ce27cb5bb05e..c83289ea3e8c 100644
@@ -343,11 +343,16 @@ struct dma_buf *amdgpu_gem_prime_export(struct drm_gem_object *gobj,
 {
 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
 	struct dma_buf *buf;
+	int ret;
 
 	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
 	    bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
 		return ERR_PTR(-EPERM);
 
+	ret = ttm_bo_setup_export(&bo->tbo, true);
+	if (ret)
+		return ERR_PTR(ret);
+
 	buf = drm_gem_prime_export(gobj, flags);
 	if (!IS_ERR(buf))
 		buf->ops = &amdgpu_dmabuf_ops;
drivers/gpu/drm/ttm/ttm_bo.c +23 -0
index 273757974b9f..3afb577cf327 100644
@@ -1284,3 +1284,26 @@ int ttm_bo_populate(struct ttm_buffer_object *bo,
 	return 0;
 }
 EXPORT_SYMBOL(ttm_bo_populate);
+
+int ttm_bo_setup_export(struct ttm_buffer_object *bo, bool interruptible)
+{
+	struct ttm_operation_ctx ctx = {
+		.interruptible = interruptible,
+		.no_wait_gpu = true,
+		/* We opt to avoid OOM on system pages allocations */
+		.gfp_retry_mayfail = true,
+		.allow_res_evict = false,
+	};
+
+	int ret;
+
+	ret = ttm_bo_reserve(bo, false, false, NULL);
+	if (ret != 0)
+		return ret;
+
+	ret = ttm_bo_populate(bo, bo->resource->placement & TTM_PL_FLAG_MEMCG, &ctx);
+ out:
+	ttm_bo_unreserve(bo);
+	return ret;
+}
+EXPORT_SYMBOL(ttm_bo_setup_export);
include/drm/ttm/ttm_bo.h +2 -0
index c33b3667ae76..91913cad879f 100644
@@ -473,6 +473,8 @@ void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
 int ttm_bo_populate(struct ttm_buffer_object *bo,
 		    bool memcg_account,
 		    struct ttm_operation_ctx *ctx);
+int ttm_bo_setup_export(struct ttm_buffer_object *bo,
+			bool interruptible);
 
 /* Driver LRU walk helpers initially targeted for shrinking. */
 
Signature · 1 line
2.50.1
maintainer
#1 lore raw
Quoted 11 lines · Dave Airlie

From: Dave Airlie <[email protected]>

While discussing cgroups we noticed a problem where you could export a BO to a dma-buf without having it ever being backed or accounted for.

This meant in low memory situations or eventually with cgroups, a lower privledged process might cause the compositor to try and allocate a lot of memory on it's behalf and this could fail. At least make sure the exporter has managed to allocate the RAM at least once before exporting the object.

Quoted 6 lines

This only applies currently to TTM_PL_SYSTEM objects, because GTT objects get populated on first validate, and VRAM doesn't use TT.

TODO:
other drivers?

XE and maybe Nouveau are a must have I think.

VMWGFX and hisilicon maybe, I don't know.

Radeon and QXL probably not.

Quoted 1 line

split this into two?

Potentially better, yes.

Quoted 5 lines
Cc Christian Koenig <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Simona Vetter <[email protected]>

Apart from splitting it up Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

Quoted 75 lines

---
v2: let the user choose interruptible (do we need the whole op ctx?) stop checking things outside the reservation, just populate always.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c |  5 +++++
 drivers/gpu/drm/ttm/ttm_bo.c                | 23 +++++++++++++++++++++
 include/drm/ttm/ttm_bo.h                    |  2 ++
 3 files changed, 30 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +5 -0
index ce27cb5bb05e..c83289ea3e8c 100644
@@ -343,11 +343,16 @@ struct dma_buf *amdgpu_gem_prime_export(struct drm_gem_object *gobj,
 {
 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
 	struct dma_buf *buf;
+	int ret;
 
 	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
 	    bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
 		return ERR_PTR(-EPERM);
 
+	ret = ttm_bo_setup_export(&bo->tbo, true);
+	if (ret)
+		return ERR_PTR(ret);
+
 	buf = drm_gem_prime_export(gobj, flags);
 	if (!IS_ERR(buf))
 		buf->ops = &amdgpu_dmabuf_ops;
drivers/gpu/drm/ttm/ttm_bo.c +23 -0
index 273757974b9f..3afb577cf327 100644
@@ -1284,3 +1284,26 @@ int ttm_bo_populate(struct ttm_buffer_object *bo,
 	return 0;
 }
 EXPORT_SYMBOL(ttm_bo_populate);
+
+int ttm_bo_setup_export(struct ttm_buffer_object *bo, bool interruptible)
+{
+	struct ttm_operation_ctx ctx = {
+		.interruptible = interruptible,
+		.no_wait_gpu = true,
+		/* We opt to avoid OOM on system pages allocations */
+		.gfp_retry_mayfail = true,
+		.allow_res_evict = false,
+	};
+
+	int ret;
+
+	ret = ttm_bo_reserve(bo, false, false, NULL);
+	if (ret != 0)
+		return ret;
+
+	ret = ttm_bo_populate(bo, bo->resource->placement & TTM_PL_FLAG_MEMCG, &ctx);
+ out:
+	ttm_bo_unreserve(bo);
+	return ret;
+}
+EXPORT_SYMBOL(ttm_bo_setup_export);
include/drm/ttm/ttm_bo.h +2 -0
index c33b3667ae76..91913cad879f 100644
@@ -473,6 +473,8 @@ void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
 int ttm_bo_populate(struct ttm_buffer_object *bo,
 		    bool memcg_account,
 		    struct ttm_operation_ctx *ctx);
+int ttm_bo_setup_export(struct ttm_buffer_object *bo,
+			bool interruptible);
 
 /* Driver LRU walk helpers initially targeted for shrinking. */
 
#2 lore raw
In reply to Dave Airlie

Hi, Dave,

Quoted 78 lines · Dave Airlie

From: Dave Airlie <[email protected]>

While discussing cgroups we noticed a problem where you could export a BO to a dma-buf without having it ever being backed or accounted for.

This meant in low memory situations or eventually with cgroups, a lower privledged process might cause the compositor to try and allocate
a lot of memory on it's behalf and this could fail. At least make sure the exporter has managed to allocate the RAM at least once before exporting the object.

This only applies currently to TTM_PL_SYSTEM objects, because GTT objects get populated on first validate, and VRAM doesn't use TT.

TODO:
other drivers?
split this into two?

Cc Christian Koenig <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Simona Vetter <[email protected]>

---
v2: let the user choose interruptible (do we need the whole op ctx?) stop checking things outside the reservation, just populate always.
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c |  5 +++++ drivers/gpu/drm/ttm/ttm_bo.c                | 23
+++++++++++++++++++++
 include/drm/ttm/ttm_bo.h                    |  2 ++

 3 files changed, 30 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -0
index ce27cb5bb05e..c83289ea3e8c 100644
@@ -343,11 +343,16 @@ struct dma_buf *amdgpu_gem_prime_export(struct
 drm_gem_object *gobj,
  {
  	struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
  	struct dma_buf *buf;
+	int ret;


  if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) || bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) return ERR_PTR(-EPERM);

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +4 -0
+	ret = ttm_bo_setup_export(&bo->tbo, true);
+	if (ret)
+		return ERR_PTR(ret);
+

  buf = drm_gem_prime_export(gobj, flags);
  if (!IS_ERR(buf))
  buf->ops = &amdgpu_dmabuf_ops;

drivers/gpu/drm/ttm/ttm_bo.c +0 -0
index 273757974b9f..3afb577cf327 100644
@@ -1284,3 +1284,26 @@ int ttm_bo_populate(struct ttm_buffer_object
 *bo,
  	return 0;
  }

 EXPORT_SYMBOL(ttm_bo_populate);

drivers/gpu/drm/ttm/ttm_bo.c +2 -0
+
+int ttm_bo_setup_export(struct ttm_buffer_object *bo, bool

interruptible)

drivers/gpu/drm/ttm/ttm_bo.c +5 -0
+{
+	struct ttm_operation_ctx ctx = {
+		.interruptible = interruptible,
+		.no_wait_gpu = true,
+		/* We opt to avoid OOM on system pages allocations

*/

drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_populate(struct ttm_buffer_object *bo, · line 1296

Quoted 3 lines
drivers/gpu/drm/ttm/ttm_bo.c +3 -0
+		.gfp_retry_mayfail = true,
+		.allow_res_evict = false,
+	};

I still think the ttm_operation_ctx needs to originate from the driver. It's really the driver's way of telling TTM how backing store allocation should be done.

Otherwise patch LGTM.

Thanks,
Thomas

Quoted 28 lines
drivers/gpu/drm/ttm/ttm_bo.c +8 -0
+
+	int ret;
+
+	ret = ttm_bo_reserve(bo, false, false, NULL);
+	if (ret != 0)
+		return ret;
+
+	ret = ttm_bo_populate(bo, bo->resource->placement &

TTM_PL_FLAG_MEMCG, &ctx);
+ out:

drivers/gpu/drm/ttm/ttm_bo.c +4 -0
+	ttm_bo_unreserve(bo);
+	return ret;
+}
+EXPORT_SYMBOL(ttm_bo_setup_export);
include/drm/ttm/ttm_bo.h +2 -0
index c33b3667ae76..91913cad879f 100644
@@ -473,6 +473,8 @@ void ttm_bo_tt_destroy(struct ttm_buffer_object
 *bo);
  int ttm_bo_populate(struct ttm_buffer_object *bo,
  		    bool memcg_account,
  		    struct ttm_operation_ctx *ctx);
+int ttm_bo_setup_export(struct ttm_buffer_object *bo,
+			bool interruptible);


 /* Driver LRU walk helpers initially targeted for shrinking. */