Skip to contentSkip to conversation

ttm/bo: add an API to populate a bo before exporting.

PATCH In review Dave Airlie 5 participants 10 messages Sep 4, 2025 – Sep 9, 2025

Subsystems (6)
lore
Share & tools
lore

Dave Airlie

author maintainer
#0 lore raw

PATCH 1/4 ttm/bo: add an API to populate a bo before exporting.

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.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 15 +++++++++++++++
 include/drm/ttm/ttm_bo.h     |  2 ++
 2 files changed, 17 insertions(+)
drivers/gpu/drm/ttm/ttm_bo.c +15 -0
index 273757974b9f..a815c7478d3f 100644
@@ -1284,3 +1284,18 @@ 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,
+			struct ttm_operation_ctx *ctx)
+{
+	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);
+	ttm_bo_unreserve(bo);
+	return ret;
+}
+EXPORT_SYMBOL(ttm_bo_setup_export);
include/drm/ttm/ttm_bo.h +2 -0
index c33b3667ae76..cdc9f5d1b420 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,
+			struct ttm_operation_ctx *ctx);
 
 /* Driver LRU walk helpers initially targeted for shrinking. */
 
Signature · 1 line
2.50.1
author maintainer
#1 lore raw

PATCH 2/4 amdgpu: populate buffers before exporting them.

From: Dave Airlie <[email protected]>

Before exporting a buffer, make sure it has been populated with pages at least once.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +12 -0
index ce27cb5bb05e..8561ad7f6180 100644
@@ -343,11 +343,23 @@ 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;
+	struct ttm_operation_ctx ctx = {
+		.interruptible = true,
+		.no_wait_gpu = true,
+		/* We opt to avoid OOM on system pages allocations */
+		.gfp_retry_mayfail = true,
+		.allow_res_evict = false,
+	};
+	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, &ctx);
+	if (ret)
+		return ERR_PTR(ret);
+
 	buf = drm_gem_prime_export(gobj, flags);
 	if (!IS_ERR(buf))
 		buf->ops = &amdgpu_dmabuf_ops;
Signature · 1 line
2.50.1
author maintainer
#2 lore raw

PATCH 3/4 nouveau: populate buffers before exporting them.

In reply to Dave Airlie

From: Dave Airlie <[email protected]>

Before exporting a buffer, make sure it has been populated with pages at least once.

---
 drivers/gpu/drm/nouveau/nouveau_prime.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpu/drm/nouveau/nouveau_prime.c +12 -0
index cd95446d6851..caab60fc62f6 100644
@@ -108,9 +108,21 @@ struct dma_buf *nouveau_gem_prime_export(struct drm_gem_object *gobj,
 					 int flags)
 {
 	struct nouveau_bo *nvbo = nouveau_gem_object(gobj);
+	struct ttm_operation_ctx ctx = {
+		.interruptible = true,
+		.no_wait_gpu = true,
+		/* We opt to avoid OOM on system pages allocations */
+		.gfp_retry_mayfail = true,
+		.allow_res_evict = false,
+	};
+	int ret;
 
 	if (nvbo->no_share)
 		return ERR_PTR(-EPERM);
 
+	ret = ttm_bo_setup_export(&nvbo->bo, &ctx);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return drm_gem_prime_export(gobj, flags);
 }
Signature · 1 line
2.50.1
author maintainer
#4 lore raw

PATCH 4/4 xe: populate buffers before exporting them.

In reply to Dave Airlie

From: Dave Airlie <[email protected]>

Before exporting a buffer, make sure it has been populated with pages at least once.

Cc Thomas Hellström <[email protected]>
---
 drivers/gpu/drm/xe/xe_dma_buf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpu/drm/xe/xe_dma_buf.c +12 -0
index 346f857f3837..71b70e17bddd 100644
@@ -191,10 +191,22 @@ struct dma_buf *xe_gem_prime_export(struct drm_gem_object *obj, int flags)
 {
 	struct xe_bo *bo = gem_to_xe_bo(obj);
 	struct dma_buf *buf;
+	struct ttm_operation_ctx ctx = {
+		.interruptible = true,
+		.no_wait_gpu = true,
+		/* We opt to avoid OOM on system pages allocations */
+		.gfp_retry_mayfail = true,
+		.allow_res_evict = false,
+	};
+	int ret;
 
 	if (bo->vm)
 		return ERR_PTR(-EPERM);
 
+	ret = ttm_bo_setup_export(&bo->ttm, &ctx);
+	if (ret)
+		return ERR_PTR(ret);
+
 	buf = drm_gem_prime_export(obj, flags);
 	if (!IS_ERR(buf))
 		buf->ops = &xe_dmabuf_ops;
Signature · 1 line
2.50.1
maintainer Reviewed-by on v1 1/4
#6 lore raw
In reply to Dave Airlie
Quoted 13 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 8 lines

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

Quoted 45 lines

---
 drivers/gpu/drm/ttm/ttm_bo.c | 15 +++++++++++++++ include/drm/ttm/ttm_bo.h     |  2 ++

 2 files changed, 17 insertions(+)
drivers/gpu/drm/ttm/ttm_bo.c +0 -0
index 273757974b9f..a815c7478d3f 100644
@@ -1284,3 +1284,18 @@ int ttm_bo_populate(struct ttm_buffer_object
 *bo,
  	return 0;
  }

 EXPORT_SYMBOL(ttm_bo_populate);

drivers/gpu/drm/ttm/ttm_bo.c +11 -0
+
+int ttm_bo_setup_export(struct ttm_buffer_object *bo,
+			struct ttm_operation_ctx *ctx)
+{
+	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);

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..cdc9f5d1b420 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,
+			struct ttm_operation_ctx *ctx);


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

maintainer Reviewed-by on v1 4/4
#5 lore raw

Re: [PATCH 4/4] xe: populate buffers before exporting them.

In reply to Dave Airlie
Quoted 7 lines · Dave Airlie

From: Dave Airlie <[email protected]>

Before exporting a buffer, make sure it has been populated with pages at least once.

Cc Thomas Hellström <[email protected]>
Quoted 34 lines

---
 drivers/gpu/drm/xe/xe_dma_buf.c | 12 ++++++++++++

 1 file changed, 12 insertions(+)
drivers/gpu/drm/xe/xe_dma_buf.c +8 -0
index 346f857f3837..71b70e17bddd 100644
@@ -191,10 +191,22 @@ struct dma_buf *xe_gem_prime_export(struct
 drm_gem_object *obj, int flags)
  {
  	struct xe_bo *bo = gem_to_xe_bo(obj);
  	struct dma_buf *buf;
+	struct ttm_operation_ctx ctx = {
+		.interruptible = true,
+		.no_wait_gpu = true,
+		/* We opt to avoid OOM on system pages allocations
 */
+		.gfp_retry_mayfail = true,
+		.allow_res_evict = false,
+	};
+	int ret;


  if (bo->vm)
  return ERR_PTR(-EPERM);

drivers/gpu/drm/xe/xe_dma_buf.c +4 -0
+	ret = ttm_bo_setup_export(&bo->ttm, &ctx);
+	if (ret)
+		return ERR_PTR(ret);
+

  buf = drm_gem_prime_export(obj, flags);
  if (!IS_ERR(buf))
  buf->ops = &xe_dmabuf_ops;

maintainer
#7 lore raw
In reply to Dave Airlie
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 8 lines

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

Patches #1 and #2 Reviewed-by: Christian König <[email protected]>

I don't see patches #3 and #4 in my inbox, but I guess it's the same for other drivers.

Regards,
Christian.

Quoted 41 lines
---
 drivers/gpu/drm/ttm/ttm_bo.c | 15 +++++++++++++++
 include/drm/ttm/ttm_bo.h     |  2 ++
 2 files changed, 17 insertions(+)
drivers/gpu/drm/ttm/ttm_bo.c +15 -0
index 273757974b9f..a815c7478d3f 100644
@@ -1284,3 +1284,18 @@ 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,
+			struct ttm_operation_ctx *ctx)
+{
+	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);
+	ttm_bo_unreserve(bo);
+	return ret;
+}
+EXPORT_SYMBOL(ttm_bo_setup_export);
include/drm/ttm/ttm_bo.h +2 -0
index c33b3667ae76..cdc9f5d1b420 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,
+			struct ttm_operation_ctx *ctx);
 
 /* Driver LRU walk helpers initially targeted for shrinking. */
 
maintainer
#8 lore raw
In reply to Dave Airlie
Quoted 10 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.

The below use of TTM_PL_FLAG_MEMCG suggests that this goes on top of your cgroup patch series. However, wouldn't a similar change make sense regardless?

maintainer Acked-by on v1 3/4
#3 lore raw

Re: [PATCH 3/4] nouveau: populate buffers before exporting them.

In reply to Dave Airlie
Quoted 4 lines · Dave Airlie

From: Dave Airlie <[email protected]>

Before exporting a buffer, make sure it has been populated with pages at least once.

NIT: I'd add the rationale of why that's needed from patch 1 to the driver patches as well.

Quoted 1 line
#9 lore raw
In reply to Danilo Krummrich
Quoted 7 lines · Danilo Krummrich
Quoted 5 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.

Quoted 8 lines
Quoted 5 lines

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.

The below use of TTM_PL_FLAG_MEMCG suggests that this goes on top of your cgroup patch series. However, wouldn't a similar change make sense regardless?

Indeed, I just rebased it onto misc-next and dropped that parameter today.

Dave.

Quoted 1 line