ttm/bo: add an API to populate a bo before exporting.
Subsystems (6)
Dave Airlie
maintainerPATCH 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
@@ -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
@@ -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
Dave Airlie
maintainerPATCH 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
@@ -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
Dave Airlie
maintainerPATCH 3/4 nouveau: populate buffers before exporting them.
In reply to Dave AirlieFrom: 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
@@ -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
Re: [PATCH 3/4] nouveau: populate buffers before exporting them.
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
Signed-off-by Dave Airlie <[email protected]>
Dave Airlie
maintainerPATCH 4/4 xe: populate buffers before exporting them.
In reply to Dave AirlieFrom: Dave Airlie <[email protected]>
Before exporting a buffer, make sure it has been populated with pages at least once.
--- drivers/gpu/drm/xe/xe_dma_buf.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/gpu/drm/xe/xe_dma_buf.c +12 -0
@@ -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
Re: [PATCH 4/4] xe: populate buffers before exporting them.
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]>Signed-off-by Dave Airlie <[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@@ -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)
returnERR_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;
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_SYSTEMobjects, because GTT objects get populated on first validate, and VRAM doesn't use TT.Reviewed-by Christian Koenig <[email protected]>Cc Thomas Hellström <[email protected]>Cc Simona Vetter <[email protected]>Signed-off-by Dave Airlie <[email protected]>
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@@ -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@@ -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. */
Christian König
maintainerQuoted 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_SYSTEMobjects, because GTT objects get populated on first validate, and VRAM doesn't use TT.Reviewed-by Christian Koenig <[email protected]>Cc Thomas Hellström <[email protected]>Cc Simona Vetter <[email protected]>Signed-off-by Dave Airlie <[email protected]>
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@@ -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@@ -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. */
Danilo Krummrich
maintainerQuoted 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?
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_MEMCGsuggests 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.