drm/ttm: fix scheduling balance
Subsystems (3)
RFC PATCH 1/1 drm/ttm: fix scheduling balance
there is a scheduling balance issue about get node like:
a. process A allocates full memory and use it for submission.
b. process B tries to allocates memory, will wait for process A BO idle in eviction.
c. process A completes the job, process B eviction will put process A BO node,
but in the meantime, process C is comming to allocate BO, whill directly get node successfully, and do submission, process B will again wait for process C BO idle.
d. repeat the above setps, process B could be delayed much more.
add a mutex to gerantee the allocation sequence for same domain. But there is a possibility that visible vram could be evicted to invisilbe, the tricky is they are same domain manager, so which needs a special handling.
--- drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++- include/drm/ttm/ttm_bo_api.h | 2 ++ include/drm/ttm/ttm_bo_driver.h | 1 + 3 files changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo.c +15 -1
@@ -969,7 +969,11 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,if (mem_type ==TTM_PL_SYSTEM )break ; +if (ctx->man != man) +mutex_lock (&man->node_mutex); ret = (*man->func->get_node)(man, bo, place, mem); +if (ctx->man != man) +mutex_unlock (&man->node_mutex);if (unlikely (ret))return ret;
@@ -991,6 +995,7 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,for (i =0 ; i < placement->num_busy_placement; ++i) {const struct ttm_place *place = &placement->busy_placement[i]; +struct ttm_operation_ctx busy_ctx; ret =ttm_mem_type_from_place (place, &mem_type);if (ret)
@@ -1018,7 +1023,15 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,return 0 ; } - ret =ttm_bo_mem_force_space (bo, mem_type, place, mem, ctx); +memcpy (&busy_ctx, ctx,sizeof (busy_ctx)); +if (ctx->man != man) { +mutex_lock (&man->node_mutex); + busy_ctx.man = man; + } + ret =ttm_bo_mem_force_space (bo, mem_type, place, mem, + &busy_ctx); +if (ctx->man != man) +mutex_unlock (&man->node_mutex);if (ret ==0 && mem->mm_node) { mem->placement = cur_flags;return 0 ;
@@ -1449,6 +1462,7 @@ int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, man->io_reserve_fastpath = true; man->use_io_reserve_lru = false;mutex_init (&man->io_reserve_mutex); +mutex_init (&man->node_mutex);spin_lock_init (&man->move_lock);INIT_LIST_HEAD (&man->io_reserve_lru);
include/drm/ttm/ttm_bo_api.h +2 -0
@@ -265,6 +265,7 @@ struct ttm_bo_kmap_obj {* @no_wait_gpu: Return immediately if the GPU is busy. * @allow_reserved_eviction: Allow eviction of reserved BOs. * @resv: Reservation object to allow reserved evictions with. +* @man: record ctx is under man->node_mutex. * * Context for TTM operations like changing buffer placement or general memory * allocation.
@@ -275,6 +276,7 @@ struct ttm_operation_ctx {bool allow_reserved_eviction;struct reservation_object *resv;uint64_t bytes_moved; +struct ttm_mem_type_manager *man; };/**
include/drm/ttm/ttm_bo_driver.h +1 -0
@@ -288,6 +288,7 @@ struct ttm_mem_type_manager {uint32_t default_caching;const struct ttm_mem_type_manager_func *func;void *priv; +struct mutex node_mutex;struct mutex io_reserve_mutex;bool use_io_reserve_lru;bool io_reserve_fastpath;
Signature · 6 lines
2.14.1 _______________________________________________ amd-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Christian König
NAKAm 25.01.2018 um 10:59 schrieb Chunming Zhou:
Quoted 7 lines
there is a scheduling balance issue about get node like:
a. process A allocates full memory and use it for submission.
b. process B tries to allocates memory, will wait for process A BO idle in eviction.
c. process A completes the job, process B eviction will put process A BO node,
but in the meantime, process C is comming to allocate BO, whill directly get node successfully, and do submission, process B will again wait for process C BO idle.
d. repeat the above setps, process B could be delayed much more.
NAK, this is intended behavior please don't change it. If you start to use a per domain mutex like this no concurrent allocation is possible any more.
In other words when process B needs a lot of memory and blocks for that memory to be evicted it is intended that process C which doesn't needs so much memory can jump in and execute before B.
Quoted 2 lines
add a mutex to gerantee the allocation sequence for same domain. But there is a possibility that visible vram could be evicted to invisilbe, the tricky is they are same domain manager, so which needs a special handling.
To avoid lock inversion you would need a global mutex here, otherwise you can run into the following issue:
Eviction: VRAM domain mutex locked first then GTT mutex. Swapping in: GTT mutex locked first, then VRAM mutex.
Regards,
Christian.
Quoted 83 lines
Change-Id I260e8eb704f7b4788b071d3f641f21b242912256Signed-off-by Chunming Zhou <[email protected]>--- drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++- include/drm/ttm/ttm_bo_api.h | 2 ++ include/drm/ttm/ttm_bo_driver.h | 1 + 3 files changed, 18 insertions(+), 1 deletion(-)drivers/gpu/drm/ttm/ttm_bo.c +15
-1@@ -969,7 +969,11 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,if (mem_type ==TTM_PL_SYSTEM )break ; +if (ctx->man != man) +mutex_lock (&man->node_mutex); ret = (*man->func->get_node)(man, bo, place, mem); +if (ctx->man != man) +mutex_unlock (&man->node_mutex);if (unlikely (ret))return ret;@@ -991,6 +995,7 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,for (i =0 ; i < placement->num_busy_placement; ++i) {const struct ttm_place *place = &placement->busy_placement[i]; +struct ttm_operation_ctx busy_ctx; ret =ttm_mem_type_from_place (place, &mem_type);if (ret)@@ -1018,7 +1023,15 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,return 0 ; } - ret =ttm_bo_mem_force_space (bo, mem_type, place, mem, ctx); +memcpy (&busy_ctx, ctx,sizeof (busy_ctx)); +if (ctx->man != man) { +mutex_lock (&man->node_mutex); + busy_ctx.man = man; + } + ret =ttm_bo_mem_force_space (bo, mem_type, place, mem, + &busy_ctx); +if (ctx->man != man) +mutex_unlock (&man->node_mutex);if (ret ==0 && mem->mm_node) { mem->placement = cur_flags;return 0 ;@@ -1449,6 +1462,7 @@ int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, man->io_reserve_fastpath = true; man->use_io_reserve_lru = false;mutex_init (&man->io_reserve_mutex); +mutex_init (&man->node_mutex);spin_lock_init (&man->move_lock);INIT_LIST_HEAD (&man->io_reserve_lru);include/drm/ttm/ttm_bo_api.h +2
-0@@ -265,6 +265,7 @@ struct ttm_bo_kmap_obj {* @no_wait_gpu: Return immediately if the GPU is busy. * @allow_reserved_eviction: Allow eviction of reserved BOs. * @resv: Reservation object to allow reserved evictions with. +* @man: record ctx is under man->node_mutex. * * Context for TTM operations like changing buffer placement or general memory * allocation. @@ -275,6 +276,7 @@ struct ttm_operation_ctx {bool allow_reserved_eviction;struct reservation_object *resv;uint64_t bytes_moved; +struct ttm_mem_type_manager *man; };/** include/drm/ttm/ttm_bo_driver.h +0
-0
include/drm/ttm/ttm_bo_driver.h struct ttm_mem_type_manager { · line 294
Quoted 7 lines
include/drm/ttm/ttm_bo_driver.h +1
-0@@ -288,6 +288,7 @@ struct ttm_mem_type_manager {uint32_t default_caching;const struct ttm_mem_type_manager_func *func;void *priv; +struct mutex node_mutex;struct mutex io_reserve_mutex;bool use_io_reserve_lru;bool io_reserve_fastpath;
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 8 lines · Chunming Zhou
there is a scheduling balance issue about get node like:
a. process A allocates full memory and use it for submission.
b. process B tries to allocates memory, will wait for process A BO idle in eviction.
c. process A completes the job, process B eviction will put process A BO node,
but in the meantime, process C is comming to allocate BO, whill directly get node successfully, and do submission, process B will again wait for process C BO idle.
d. repeat the above setps, process B could be delayed much more.
Quoted 5 lines
add a mutex to gerantee the allocation sequence for same domain. But there is a possibility that visible vram could be evicted to invisilbe, the tricky is they are same domain manager, so which needs a special handling.
Change-Id I260e8eb704f7b4788b071d3f641f21b242912256Signed-off-by Chunming Zhou <[email protected]>
I think this is a good approach, however there are two things that IMO needs fixing.
1) You must lock mutex interruptible if requested. It's important for fast delivery of Xserver mouse signals, and also to avoid unkillable processes on GPU hang.
2) How do we resolve contradictary eviction order between buffers? You'd probably get lockdep warnings if that should happen. Now I'm not sure there are drivers that do this, and that would probably be a bug if they did. In principle you'd have to either establish a manager priority and document that drivers have to do that, or use ww_mutexes.
/Thomas
Quoted 80 lines
--- drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++- include/drm/ttm/ttm_bo_api.h | 2 ++ include/drm/ttm/ttm_bo_driver.h | 1 + 3 files changed, 18 insertions(+), 1 deletion(-)drivers/gpu/drm/ttm/ttm_bo.c +15
-1@@ -969,7 +969,11 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,if (mem_type ==TTM_PL_SYSTEM )break ; +if (ctx->man != man) +mutex_lock (&man->node_mutex); ret = (*man->func->get_node)(man, bo, place, mem); +if (ctx->man != man) +mutex_unlock (&man->node_mutex);if (unlikely (ret))return ret;@@ -991,6 +995,7 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,for (i =0 ; i < placement->num_busy_placement; ++i) {const struct ttm_place *place = &placement->busy_placement[i]; +struct ttm_operation_ctx busy_ctx; ret =ttm_mem_type_from_place (place, &mem_type);if (ret)@@ -1018,7 +1023,15 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,return 0 ; } - ret =ttm_bo_mem_force_space (bo, mem_type, place, mem, ctx); +memcpy (&busy_ctx, ctx,sizeof (busy_ctx)); +if (ctx->man != man) { +mutex_lock (&man->node_mutex); + busy_ctx.man = man; + } + ret =ttm_bo_mem_force_space (bo, mem_type, place, mem, + &busy_ctx); +if (ctx->man != man) +mutex_unlock (&man->node_mutex);if (ret ==0 && mem->mm_node) { mem->placement = cur_flags;return 0 ;@@ -1449,6 +1462,7 @@ int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, man->io_reserve_fastpath = true; man->use_io_reserve_lru = false;mutex_init (&man->io_reserve_mutex); +mutex_init (&man->node_mutex);spin_lock_init (&man->move_lock);INIT_LIST_HEAD (&man->io_reserve_lru);include/drm/ttm/ttm_bo_api.h +2
-0@@ -265,6 +265,7 @@ struct ttm_bo_kmap_obj {* @no_wait_gpu: Return immediately if the GPU is busy. * @allow_reserved_eviction: Allow eviction of reserved BOs. * @resv: Reservation object to allow reserved evictions with. +* @man: record ctx is under man->node_mutex. * * Context for TTM operations like changing buffer placement or general memory * allocation. @@ -275,6 +276,7 @@ struct ttm_operation_ctx {bool allow_reserved_eviction;struct reservation_object *resv;uint64_t bytes_moved; +struct ttm_mem_type_manager *man; };/** include/drm/ttm/ttm_bo_driver.h +0
-0
include/drm/ttm/ttm_bo_driver.h struct ttm_mem_type_manager { · line 294
Quoted 7 lines
include/drm/ttm/ttm_bo_driver.h +1
-0@@ -288,6 +288,7 @@ struct ttm_mem_type_manager {uint32_t default_caching;const struct ttm_mem_type_manager_func *func;void *priv; +struct mutex node_mutex;struct mutex io_reserve_mutex;bool use_io_reserve_lru;bool io_reserve_fastpath;
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 9 lines · Thomas Hellstrom
Quoted 8 lines · Chunming Zhou
there is a scheduling balance issue about get node like:
a. process A allocates full memory and use it for submission.
b. process B tries to allocates memory, will wait for process A BO idle in eviction.
c. process A completes the job, process B eviction will put process A BO node,
but in the meantime, process C is comming to allocate BO, whill directly get node successfully, and do submission, process B will again wait for process C BO idle.
d. repeat the above setps, process B could be delayed much more.
Quoted 7 lines
Quoted 5 lines
add a mutex to gerantee the allocation sequence for same domain. But there is a possibility that visible vram could be evicted to invisilbe, the tricky is they are same domain manager, so which needs a special handling.
Change-Id I260e8eb704f7b4788b071d3f641f21b242912256Signed-off-by Chunming Zhou <[email protected]>I think this is a good approach, however there are two things that IMO needs fixing. [...]
Thinking a bit more about this, the end result would be that typical "C" processes would get an unfair amount of GPU scheduling. Isn't it actually a scheduler's task outside of TTM to mitigate this?
Further, TTM has had a design principle of avoiding locks held while waiting for GPU, with the exception of buffer object reservations, I think this would be the first violator, but a fairly harmless one.
I can see the use for it though. It would also allow scanning the LRU lists for a suitable set of buffer objects to evict, rather than evicting in strict LRU order...
/Thomas
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
A message that is not in the archive (sent off-list or not yet received): 1073265e-dfeb-d0c3-956e-abe62a2381a7-4+hqylr40dJg9hUCZPvPmw@public.gmane.org
Am 25.01.2018 um 17:47 schrieb Thomas Hellstrom:
Quoted 13 lines · Thomas Hellstrom
Quoted 12 lines · Chunming Zhou
Quoted 11 lines
there is a scheduling balance issue about get node like:
a. process A allocates full memory and use it for submission.
b. process B tries to allocates memory, will wait for process A BO idle in eviction.
c. process A completes the job, process B eviction will put process A BO node,
but in the meantime, process C is comming to allocate BO, whill directly get node successfully, and do submission, process B will again wait for process C BO idle.
d. repeat the above setps, process B could be delayed much more.add a mutex to gerantee the allocation sequence for same domain. But there is a possibility that visible vram could be evicted to invisilbe, the tricky is they are same domain manager, so which needs a special handling.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Change-Id I260e8eb704f7b4788b071d3f641f21b242912256Signed-off-by Chunming Zhou <[email protected]>I think this is a good approach, however there are two things that IMO needs fixing. [...]
Thinking a bit more about this, the end result would be that typical "C" processes would get an unfair amount of GPU scheduling. Isn't it actually a scheduler's task outside of TTM to mitigate this?
Yes, exactly the reason why I rejected this. I actually considered moving the whole evicting to a background workitem.
Quoted 2 lines
Further, TTM has had a design principle of avoiding locks held while waiting for GPU, with the exception of buffer object reservations, I think this would be the first violator, but a fairly harmless one.
At least amdgpu normally doesn't block for any GPU operation to finish (with a few exception), but yes I see the problem as well.
Quoted 2 lines
I can see the use for it though. It would also allow scanning the LRU lists for a suitable set of buffer objects to evict, rather than evicting in strict LRU order...
At least for amdgpu that won't be possible even then, cause we don't tell TTM everything about buffer placement. E.g. BOs are not necessary composed from contiguous allocations.
Christian.
Quoted 3 lines
/Thomas
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
A message that is not in the archive (sent off-list or not yet received): f7b44bcf-0ee3-f67d-50be-b922caae9a70-5C7GfCeVMHo@public.gmane.org
Hi,
Quoted 17 lines · Christian König
Am 25.01.2018 um 17:47 schrieb Thomas Hellstrom:
Quoted 16 lines · Thomas Hellstrom
Quoted 15 lines · Chunming Zhou
Quoted 13 lines
there is a scheduling balance issue about get node like:
a. process A allocates full memory and use it for submission.
b. process B tries to allocates memory, will wait for process A BO idle in eviction.
c. process A completes the job, process B eviction will put process A BO node,
but in the meantime, process C is comming to allocate BO, whill directly get node successfully, and do submission, process B will again wait for process C BO idle.
d. repeat the above setps, process B could be delayed much more.add a mutex to gerantee the allocation sequence for same domain. But there is a possibility that visible vram could be evicted to invisilbe, the tricky is they are same domain manager, so which needs a special handling.
Change-Id I260e8eb704f7b4788b071d3f641f21b242912256Signed-off-by Chunming Zhou <[email protected]>
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
I think this is a good approach, however there are two things that IMO needs fixing. [...]
Thinking a bit more about this, the end result would be that typical "C" processes would get an unfair amount of GPU scheduling. Isn't it actually a scheduler's task outside of TTM to mitigate this?
Yes, exactly the reason why I rejected this. I actually considered moving the whole evicting to a background workitem.
Ah,I should've read up on the following emails, Sorry about that.
Thanks,
Thomas
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx