amdgpu/ttm: make sure exported objects are populated
Subsystems (4)
Dave Airlie
maintainerPATCH 1/1 amdgpu/ttm: make sure exported objects are populated
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?
--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 5 ++++ drivers/gpu/drm/ttm/ttm_bo.c | 28 +++++++++++++++++++++ include/drm/ttm/ttm_bo.h | 1 + 3 files changed, 34 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +5 -0
@@ -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); +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 +28 -0
@@ -1284,3 +1284,31 @@ 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 = { + .interruptible = false, + .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->ttm) +return 0 ; + +if (bo->ttm &&ttm_tt_is_populated (bo->ttm)) +return 0 ; + + 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 +1 -0
@@ -473,6 +473,7 @@ 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);/* Driver LRU walk helpers initially targeted for shrinking. */
Signature · 1 line
2.50.1
Thomas Hellström
reviewerHi Dave,
Quoted 6 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 6 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.
With a shmem analogy, let's say process A creates a shmem object and doesn't populate it. It's then shared with process B which faults in the pages or otherwise causes it to be populated. Will this patch ensure we behave the same WRT memory usage accounting?
Also some concerns below.
Quoted 61 lines
This only applies currently to
TTM_PL_SYSTEMobjects, 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]>Signed-off-by Dave Airlie <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 5 ++++ drivers/gpu/drm/ttm/ttm_bo.c | 28
+++++++++++++++++++++
include/drm/ttm/ttm_bo.h | 1 +3 files changed, 34 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1
-0@@ -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) returnERR_PTR(-EPERM);drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +4
-0+ ret =ttm_bo_setup_export (&bo->tbo); +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@@ -1284,3 +1284,31 @@ int ttm_bo_populate(struct ttm_buffer_object *bo, return 0; }EXPORT_SYMBOL(ttm_bo_populate);
drivers/gpu/drm/ttm/ttm_bo.c +7
-0+ +int ttm_bo_setup_export (struct ttm_buffer_object *bo) +{ +struct ttm_operation_ctx ctx = { + .interruptible = false, + .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 think we'd want to have the caller (driver) provide the ttm_operation_ctx. The driver may want to subclass or call interruptible.
drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_populate(struct ttm_buffer_object *bo, · line 1305
Quoted 9 lines
drivers/gpu/drm/ttm/ttm_bo.c +9
-0+ +int ret; + +if (!bo->ttm) +return 0 ; + +if (bo->ttm &&ttm_tt_is_populated (bo->ttm)) +return 0 ; +
bo->ttm is not safe to reference without the bo lock. Nor is the ttm_tt_is_populated stable without the bo lock since you may race with a swapout / shrink.
Thanks,
Thomas
Quoted 23 lines
drivers/gpu/drm/ttm/ttm_bo.c +5
-0+ 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 +1
-0@@ -473,6 +473,7 @@ 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);
/* Driver LRU walk helpers initially targeted for shrinking. */
Quoted 17 lines · Thomas Hellström
Hi Dave,
Quoted 6 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 6 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.
Quoted 5 lines
With a shmem analogy, let's say process A creates a shmem object and doesn't populate it. It's then shared with process B which faults in the pages or otherwise causes it to be populated. Will this patch ensure we behave the same WRT memory usage accounting?
I wandered down a few holes, but with cgroup v2, owner pays seems to be how it works.
They use the inode which records the cgroup owner who created it.
cgroups v1 did it the other way, but I think we should ignore that.
Quoted 5 lines
Also some concerns below.
I think we'd want to have the caller (driver) provide the
ttm_operation_ctx. The driver may want to subclass or call interruptible.
Indeed, might make more sense, though I think we should only be calling this after creating an object in most cases,
Quoted 15 lines
drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_populate(struct ttm_buffer_object *bo, · line 1305
Quoted 11 lines
drivers/gpu/drm/ttm/ttm_bo.c +9
-0+ +int ret; + +if (!bo->ttm) +return 0 ; + +if (bo->ttm &&ttm_tt_is_populated (bo->ttm)) +return 0 ; +
bo->ttmis not safe to reference without the bo lock. Nor is thettm_tt_is_populatedstable without the bo lock since you may race with a swapout / shrink.
Indeed, I was probably being a bit hacky here, I'll always reserve first.
Dave.
Quoted 29 lines
Thanks,
ThomasQuoted 23 lines
drivers/gpu/drm/ttm/ttm_bo.c +5
-0+ 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 +1
-0@@ -473,6 +473,7 @@ 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);/* Driver LRU walk helpers initially targeted for shrinking. */
Quoted 26 lines · David Airlie
Quoted 17 lines · Thomas Hellström
Hi Dave,
Quoted 6 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 6 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.Quoted 5 lines
With a shmem analogy, let's say process A creates a shmem object and doesn't populate it. It's then shared with process B which faults in the pages or otherwise causes it to be populated. Will this patch ensure we behave the same WRT memory usage accounting?
Quoted 7 lines
I wandered down a few holes, but with cgroup v2, owner pays seems to be how it works.
They use the inode which records the cgroup owner who created it.
cgroups v1 did it the other way, but I think we should ignore that.
Also it does the allocations at fault time, however since we share our buffers with hw and higher privilege apps like compositors, allowing one process to ENOMEM those seems like a bad plan.
This doesn't fully stop it, but at least makes sure the originating process can allocate the backing store.
Dave.
Quoted 57 lines
Quoted 6 lines
Also some concerns below.
I think we'd want to have the caller (driver) provide the
ttm_operation_ctx. The driver may want to subclass or call interruptible.Indeed, might make more sense, though I think we should only be calling this after creating an object in most cases,
Quoted 15 lines
drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_populate(struct ttm_buffer_object *bo, · line 1305
Quoted 11 lines
drivers/gpu/drm/ttm/ttm_bo.c +9
-0+ +int ret; + +if (!bo->ttm) +return 0 ; + +if (bo->ttm &&ttm_tt_is_populated (bo->ttm)) +return 0 ; +
bo->ttmis not safe to reference without the bo lock. Nor is thettm_tt_is_populatedstable without the bo lock since you may race with a swapout / shrink.Indeed, I was probably being a bit hacky here, I'll always reserve first.
Dave.
Quoted 29 lines
Thanks,
ThomasQuoted 23 lines
drivers/gpu/drm/ttm/ttm_bo.c +5
-0+ 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 +1
-0@@ -473,6 +473,7 @@ 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);/* Driver LRU walk helpers initially targeted for shrinking. */
Christian König
maintainerQuoted 19 lines · David Airlie
Quoted 17 lines · Thomas Hellström
Hi Dave,
Quoted 12 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
Quoted 5 lines
With a shmem analogy, let's say process A creates a shmem object and doesn't populate it. It's then shared with process B which faults in the pages or otherwise causes it to be populated. Will this patch ensure we behave the same WRT memory usage accounting?
I wandered down a few holes, but with cgroup v2, owner pays seems to be how it works.
Which is pretty much the correct approach as far as I can see. The only problem is that the ENOMEM is delivered to the wrong process.
In other words the one using it for the first time gets the ENOMEM and not the owner.
This is usually solved by making the allocation or at least the reservation of resources happen when they transit a domain boundary, but Linux cgroups doesn't have any of those concepts.
Quoted 3 lines
They use the inode which records the cgroup owner who created it.
cgroups v1 did it the other way, but I think we should ignore that.
Yeah agree, cgroups v1 is not something we need to take into account.
Apart from that with the technical problems pointed out by Thomas fixed the general approach looks sane to me.
Regards,
Christian.
Quoted 59 lines
Quoted 7 lines
Also some concerns below.
I think we'd want to have the caller (driver) provide the
ttm_operation_ctx. The driver may want to subclass or call interruptible.Indeed, might make more sense, though I think we should only be calling this after creating an object in most cases,
Quoted 15 lines
drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_populate(struct ttm_buffer_object *bo, · line 1305
Quoted 11 lines
drivers/gpu/drm/ttm/ttm_bo.c +9
-0+ +int ret; + +if (!bo->ttm) +return 0 ; + +if (bo->ttm &&ttm_tt_is_populated (bo->ttm)) +return 0 ; +
bo->ttmis not safe to reference without the bo lock. Nor is thettm_tt_is_populatedstable without the bo lock since you may race with a swapout / shrink.Indeed, I was probably being a bit hacky here, I'll always reserve first.
Dave.
Quoted 29 lines
Thanks,
ThomasQuoted 23 lines
drivers/gpu/drm/ttm/ttm_bo.c +5
-0+ 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 +1
-0@@ -473,6 +473,7 @@ 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);/* Driver LRU walk helpers initially targeted for shrinking. */