drm/amdgpu: fix amdgpu_amdkfd_remove_eviction_fence
Subsystems (3)
PATCH 1/1 drm/amdgpu: fix amdgpu_amdkfd_remove_eviction_fence
Fix quite a number of bugs here. Unfortunately only compile tested.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 103 ++++++++++------------- 1 file changed, 46 insertions(+), 57 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +46 -57
@@ -206,11 +206,9 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo,struct amdgpu_amdkfd_fence ***ef_list, unsigned int *ef_count) { - struct reservation_object_list *fobj; -struct reservation_object *resv; -unsigned int i =0 , j =0 , k =0 , shared_count; -unsigned int count =0 ; -struct amdgpu_amdkfd_fence **fence_list; + struct reservation_object *resv = bo->tbo.resv; +struct reservation_object_list *old, *new; +unsigned int i, j, k;if (!ef && !ef_list)return -EINVAL ;
@@ -220,76 +218,67 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, *ef_count =0 ; } - resv = bo->tbo.resv; - fobj =reservation_object_get_list (resv); - -if (!fobj) + old =reservation_object_get_list (resv); +if (!old)return 0 ; -preempt_disable (); -write_seqcount_begin (&resv->seq); + new = kmalloc(offsetof(typeof(*new), shared[old->shared_max]), + GFP_KERNEL); +if (!new) +return -ENOMEM ; -/* Go through all the shared fences in the resevation object. If -* ef is specified and it exists in the list, remove it and reduce the -* count. If ef is not specified, then get the count of eviction fences -* present. +/* Go through all the shared fences in the resevation object and sort +* the interesting ones to the end of the list. */ - shared_count = fobj->shared_count; -for (i =0 ; i < shared_count; ++i) { +for (i =0 , j = old->shared_count, k =0 ; i < old->shared_count; ++i) {struct dma_fence *f; - f =rcu_dereference_protected (fobj->shared[i], + f =rcu_dereference_protected (old->shared[i],reservation_object_held (resv)); -if (ef) { -if (f->context == ef->base.context) { -dma_fence_put (f); - fobj->shared_count--; - }else { -RCU_INIT_POINTER (fobj->shared[j++], f); - } - }else if (to_amdgpu_amdkfd_fence (f)) - count++; +if ((ef && f->context == ef->base.context) || + (!ef &&to_amdgpu_amdkfd_fence (f))) +RCU_INIT_POINTER (new->shared[--j], f); +else +RCU_INIT_POINTER (new->shared[k++], f); } -write_seqcount_end (&resv->seq); -preempt_enable (); + new->shared_max = old->shared_max; + new->shared_count = k; -if (ef || !count) -return 0 ; +if (!ef) { +unsigned int count = old->shared_count - j; -/* Alloc memory for count number of eviction fence pointers. Fill the -* ef_list array and ef_count -*/ - fence_list =kcalloc (count,sizeof (struct amdgpu_amdkfd_fence *), -GFP_KERNEL ); -if (!fence_list) -return -ENOMEM ; +/* Alloc memory for count number of eviction fence pointers. Fill the +* ef_list array and ef_count +*/ + *ef_list =kcalloc (count,sizeof (**ef_list),GFP_KERNEL ); + *ef_count = count; +if (!*ef_list) { +kfree (new); +return -ENOMEM ; + } + } + +/* Install the new fence list, seqcount provides the barriers */ +preempt_disable (); +write_seqcount_begin (&resv->seq); +RCU_INIT_POINTER (resv->fence, new);preempt_disable ();write_seqcount_begin (&resv->seq); - j =0 ; -for (i =0 ; i < shared_count; ++i) { +/* Drop the references to the removed fences or move them to ef_list */ +for (i = j, k =0 ; i < old->shared_count; ++i) {struct dma_fence *f; -struct amdgpu_amdkfd_fence *efence; - f =rcu_dereference_protected (fobj->shared[i], -reservation_object_held (resv)); - - efence =to_amdgpu_amdkfd_fence (f); -if (efence) { - fence_list[k++] = efence; - fobj->shared_count--; - }else { -RCU_INIT_POINTER (fobj->shared[j++], f); - } + f =rcu_dereference_protected (new->shared[i], +reservation_object_held (resv)); +if (!ef) + (*ef_list)[k++] =to_amdgpu_amdkfd_fence (f); +else +dma_fence_put (f); } - -write_seqcount_end (&resv->seq); -preempt_enable (); - - *ef_list = fence_list; - *ef_count = k; +kfree_rcu (old, rcu);return 0 ; }
Signature · 6 lines
2.14.1 _______________________________________________ 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): 20180814073954.3238-1-christian.koenig-5C7GfCeVMHo@public.gmane.org
[+Harish]
I think this looks good for the most part. See one comment inline below.
But bear with me while I'm trying to understand what was wrong with the old code. Please correct me if I get this wrong or point out anything I'm missing.
The reservation_object_list looks to be protected by a combination of three mechanism:
* Holding the reservation object
* RCU
* seqcount
Updating the fence list requires holding the reservation object. But there are some readers that can be called without holding that lock (reservation_object_copy_fences, reservation_object_get_fences_rcu, reservation_object_wait_timeout_rcu, reservation_object_test_signaled_rcu). They rely on RCU to work on a copy and seqcount to make sure they had the most up-to-date information. So any function updating the fence lists needs to do RCU and seqcount correctly to prevent breaking those readers.
As I understand it, RCU with seqcount retry just means that readers will spin retrying while there are writers, and writers are never blocked by readers. Writers are blocked by each other, because they need to hold the reservation.
The code you added looks a lot like reservation_object_add_shared_replace, which removes fences that have signalled, and atomically replaces obj->fences with a new reservation_fence_list. That atomicity is important because each pointer in the obj->fences->shared array is separately protected by RCU, but not the array as a whole or the shared_count.
See one comment inline.
Regards,
Felix
Quoted 101 lines · Christian König
Fix quite a number of bugs here. Unfortunately only compile tested.
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 103 ++++++++++------------- 1 file changed, 46 insertions(+), 57 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +29
-38@@ -206,11 +206,9 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo,struct amdgpu_amdkfd_fence ***ef_list, unsigned int *ef_count) { - struct reservation_object_list *fobj; -struct reservation_object *resv; -unsigned int i =0 , j =0 , k =0 , shared_count; -unsigned int count =0 ; -struct amdgpu_amdkfd_fence **fence_list; + struct reservation_object *resv = bo->tbo.resv; +struct reservation_object_list *old, *new; +unsigned int i, j, k;if (!ef && !ef_list)return -EINVAL ;@@ -220,76 +218,67 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, *ef_count =0 ; } - resv = bo->tbo.resv; - fobj =reservation_object_get_list (resv); - -if (!fobj) + old =reservation_object_get_list (resv); +if (!old)return 0 ; -preempt_disable (); -write_seqcount_begin (&resv->seq); + new = kmalloc(offsetof(typeof(*new), shared[old->shared_max]), + GFP_KERNEL); +if (!new) +return -ENOMEM ; -/* Go through all the shared fences in the resevation object. If -* ef is specified and it exists in the list, remove it and reduce the -* count. If ef is not specified, then get the count of eviction fences -* present. +/* Go through all the shared fences in the resevation object and sort +* the interesting ones to the end of the list. */ - shared_count = fobj->shared_count; -for (i =0 ; i < shared_count; ++i) { +for (i =0 , j = old->shared_count, k =0 ; i < old->shared_count; ++i) {struct dma_fence *f; - f =rcu_dereference_protected (fobj->shared[i], + f =rcu_dereference_protected (old->shared[i],reservation_object_held (resv)); -if (ef) { -if (f->context == ef->base.context) { -dma_fence_put (f); - fobj->shared_count--; - }else { -RCU_INIT_POINTER (fobj->shared[j++], f); - } - }else if (to_amdgpu_amdkfd_fence (f)) - count++; +if ((ef && f->context == ef->base.context) || + (!ef &&to_amdgpu_amdkfd_fence (f))) +RCU_INIT_POINTER (new->shared[--j], f); +else +RCU_INIT_POINTER (new->shared[k++], f); } -write_seqcount_end (&resv->seq); -preempt_enable (); + new->shared_max = old->shared_max; + new->shared_count = k; -if (ef || !count) -return 0 ; +if (!ef) { +unsigned int count = old->shared_count - j; -/* Alloc memory for count number of eviction fence pointers. Fill the -* ef_list array and ef_count -*/ - fence_list =kcalloc (count,sizeof (struct amdgpu_amdkfd_fence *), -GFP_KERNEL ); -if (!fence_list) -return -ENOMEM ; +/* Alloc memory for count number of eviction fence pointers. Fill the +* ef_list array and ef_count +*/ + *ef_list =kcalloc (count,sizeof (**ef_list),GFP_KERNEL ); + *ef_count = count; +if (!*ef_list) { +kfree (new);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, · line 268
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +8
-0+return -ENOMEM ; + } + } + +/* Install the new fence list, seqcount provides the barriers */ +preempt_disable (); +write_seqcount_begin (&resv->seq); +RCU_INIT_POINTER (resv->fence, new);preempt_disable ();write_seqcount_begin (&resv->seq);
You're disabling preemption and calling write_seqcount_begin twice. I think this is meant to be
write_seqcount_end(&resv->seq); preempt_enable();
Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +8
-13- j =0 ; -for (i =0 ; i < shared_count; ++i) { +/* Drop the references to the removed fences or move them to ef_list */ +for (i = j, k =0 ; i < old->shared_count; ++i) {struct dma_fence *f; -struct amdgpu_amdkfd_fence *efence; - f =rcu_dereference_protected (fobj->shared[i], -reservation_object_held (resv)); - - efence =to_amdgpu_amdkfd_fence (f); -if (efence) { - fence_list[k++] = efence; - fobj->shared_count--; - }else { -RCU_INIT_POINTER (fobj->shared[j++], f); - } + f =rcu_dereference_protected (new->shared[i], +reservation_object_held (resv)); +if (!ef) + (*ef_list)[k++] =to_amdgpu_amdkfd_fence (f); +else +dma_fence_put (f); }
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, · line 284
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-6- -write_seqcount_end (&resv->seq); -preempt_enable (); - - *ef_list = fence_list; - *ef_count = k; +kfree_rcu (old, rcu);return 0 ; }
_______________________________________________
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): bf98edfe-5bd8-9f8e-f688-4f37e80cd65d-5C7GfCeVMHo@public.gmane.org
Hi Felix,
yeah, you pretty much nailed it.
The problem is that the array itself is RCU protected. This means that you can only copy the whole structure when you want to update it.
The exception is reservation_object_add_shared() which only works because we replace an either signaled fence or a fence within the same context but a later sequence number.
This also explains why this is only a band aid and the whole approach of removing fences doesn't work in general. At any time somebody could have taken an RCU reference to the old array, created a copy of it and is now still using this one.
The only 100% correct solution would be to mark the existing fence as signaled and replace it everywhere else.
Going to fix the copy&paste error I made with the sequence number and send it out again.
Regards,
Christian.
Am 14.08.2018 um 22:17 schrieb Felix Kuehling:
Quoted 193 lines
[+Harish]
I think this looks good for the most part. See one comment inline below.
But bear with me while I'm trying to understand what was wrong with the old code. Please correct me if I get this wrong or point out anything I'm missing.
The
reservation_object_listlooks to be protected by a combination of three mechanism:* Holding the reservation object
* RCU
* seqcountUpdating the fence list requires holding the reservation object. But there are some readers that can be called without holding that lock (
reservation_object_copy_fences,reservation_object_get_fences_rcu,reservation_object_wait_timeout_rcu,reservation_object_test_signaled_rcu). They rely on RCU to work on a copy and seqcount to make sure they had the most up-to-date information. So any function updating the fence lists needs to do RCU and seqcount correctly to prevent breaking those readers.As I understand it, RCU with seqcount retry just means that readers will spin retrying while there are writers, and writers are never blocked by readers. Writers are blocked by each other, because they need to hold the reservation.
The code you added looks a lot like
reservation_object_add_shared_replace, which removes fences that have signalled, and atomically replacesobj->fenceswith a newreservation_fence_list. That atomicity is important because each pointer in theobj->fences->sharedarray is separately protected by RCU, but not the array as a whole or theshared_count.See one comment inline.
Regards,
FelixQuoted 101 lines · Christian König
Fix quite a number of bugs here. Unfortunately only compile tested.
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 103 ++++++++++------------- 1 file changed, 46 insertions(+), 57 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +29
-38@@ -206,11 +206,9 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo,struct amdgpu_amdkfd_fence ***ef_list, unsigned int *ef_count) { - struct reservation_object_list *fobj; -struct reservation_object *resv; -unsigned int i =0 , j =0 , k =0 , shared_count; -unsigned int count =0 ; -struct amdgpu_amdkfd_fence **fence_list; + struct reservation_object *resv = bo->tbo.resv; +struct reservation_object_list *old, *new; +unsigned int i, j, k;if (!ef && !ef_list)return -EINVAL ;@@ -220,76 +218,67 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, *ef_count =0 ; } - resv = bo->tbo.resv; - fobj =reservation_object_get_list (resv); - -if (!fobj) + old =reservation_object_get_list (resv); +if (!old)return 0 ; -preempt_disable (); -write_seqcount_begin (&resv->seq); + new = kmalloc(offsetof(typeof(*new), shared[old->shared_max]), + GFP_KERNEL); +if (!new) +return -ENOMEM ; -/* Go through all the shared fences in the resevation object. If -* ef is specified and it exists in the list, remove it and reduce the -* count. If ef is not specified, then get the count of eviction fences -* present. +/* Go through all the shared fences in the resevation object and sort +* the interesting ones to the end of the list. */ - shared_count = fobj->shared_count; -for (i =0 ; i < shared_count; ++i) { +for (i =0 , j = old->shared_count, k =0 ; i < old->shared_count; ++i) {struct dma_fence *f; - f =rcu_dereference_protected (fobj->shared[i], + f =rcu_dereference_protected (old->shared[i],reservation_object_held (resv)); -if (ef) { -if (f->context == ef->base.context) { -dma_fence_put (f); - fobj->shared_count--; - }else { -RCU_INIT_POINTER (fobj->shared[j++], f); - } - }else if (to_amdgpu_amdkfd_fence (f)) - count++; +if ((ef && f->context == ef->base.context) || + (!ef &&to_amdgpu_amdkfd_fence (f))) +RCU_INIT_POINTER (new->shared[--j], f); +else +RCU_INIT_POINTER (new->shared[k++], f); } -write_seqcount_end (&resv->seq); -preempt_enable (); + new->shared_max = old->shared_max; + new->shared_count = k; -if (ef || !count) -return 0 ; +if (!ef) { +unsigned int count = old->shared_count - j; -/* Alloc memory for count number of eviction fence pointers. Fill the -* ef_list array and ef_count -*/ - fence_list =kcalloc (count,sizeof (struct amdgpu_amdkfd_fence *), -GFP_KERNEL ); -if (!fence_list) -return -ENOMEM ; +/* Alloc memory for count number of eviction fence pointers. Fill the +* ef_list array and ef_count +*/ + *ef_list =kcalloc (count,sizeof (**ef_list),GFP_KERNEL ); + *ef_count = count; +if (!*ef_list) { +kfree (new);drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, · line 268
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +8
-0+return -ENOMEM ; + } + } + +/* Install the new fence list, seqcount provides the barriers */ +preempt_disable (); +write_seqcount_begin (&resv->seq); +RCU_INIT_POINTER (resv->fence, new);preempt_disable ();write_seqcount_begin (&resv->seq);You're disabling preemption and calling
write_seqcount_begintwice. I think this is meant to bewrite_seqcount_end(&resv->seq); preempt_enable();Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +9
-19- j =0 ; -for (i =0 ; i < shared_count; ++i) { +/* Drop the references to the removed fences or move them to ef_list */ +for (i = j, k =0 ; i < old->shared_count; ++i) {struct dma_fence *f; -struct amdgpu_amdkfd_fence *efence; - f =rcu_dereference_protected (fobj->shared[i], -reservation_object_held (resv)); - - efence =to_amdgpu_amdkfd_fence (f); -if (efence) { - fence_list[k++] = efence; - fobj->shared_count--; - }else { -RCU_INIT_POINTER (fobj->shared[j++], f); - } + f =rcu_dereference_protected (new->shared[i], +reservation_object_held (resv)); +if (!ef) + (*ef_list)[k++] =to_amdgpu_amdkfd_fence (f); +else +dma_fence_put (f); } - -write_seqcount_end (&resv->seq); -preempt_enable (); - - *ef_list = fence_list; - *ef_count = k; +kfree_rcu (old, rcu);
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, · line 283
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0
-0return 0 ; }
_______________________________________________
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): 382cdd76-5461-8c80-1de6-28b01317d961-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Quoted 11 lines · Christian König
Hi Felix,
yeah, you pretty much nailed it.
The problem is that the array itself is RCU protected. This means that you can only copy the whole structure when you want to update it.
The exception is
reservation_object_add_shared()which only works because we replace an either signaled fence or a fence within the same context but a later sequence number.
Quoted 7 lines
This also explains why this is only a band aid and the whole approach of removing fences doesn't work in general. At any time somebody could have taken an RCU reference to the old array, created a copy of it and is now still using this one.
The only 100% correct solution would be to mark the existing fence as signaled and replace it everywhere else.
Depends what you're trying to achieve. I think the problem you see is, that some reader may still be using the old reservation_object_list copy with the fence still in it. But, the remaining lifetime of the reservation_object_list copy is limited. If we wanted to be sure no more copies with the old fence exist, all we'd need to do is call synchronize_rcu. Maybe we need to do that before releasing the fence references, or release the fence reference in an RCU callback to be safe.
Regards,
Felix
Quoted 199 lines
Going to fix the copy&paste error I made with the sequence number and send it out again.
Regards,
Christian.Am 14.08.2018 um 22:17 schrieb Felix Kuehling:
Quoted 191 lines
[+Harish]
I think this looks good for the most part. See one comment inline below.
But bear with me while I'm trying to understand what was wrong with the old code. Please correct me if I get this wrong or point out anything I'm missing.
The
reservation_object_listlooks to be protected by a combination of three mechanism:* Holding the reservation object
* RCU
* seqcountUpdating the fence list requires holding the reservation object. But there are some readers that can be called without holding that lock (
reservation_object_copy_fences,reservation_object_get_fences_rcu,reservation_object_wait_timeout_rcu,reservation_object_test_signaled_rcu). They rely on RCU to work on a copy and seqcount to make sure they had the most up-to-date information. So any function updating the fence lists needs to do RCU and seqcount correctly to prevent breaking those readers.As I understand it, RCU with seqcount retry just means that readers will spin retrying while there are writers, and writers are never blocked by readers. Writers are blocked by each other, because they need to hold the reservation.
The code you added looks a lot like
reservation_object_add_shared_replace, which removes fences that have signalled, and atomically replacesobj->fenceswith a newreservation_fence_list. That atomicity is important because each pointer in theobj->fences->sharedarray is separately protected by RCU, but not the array as a whole or theshared_count.See one comment inline.
Regards,
FelixQuoted 104 lines · Christian König
Fix quite a number of bugs here. Unfortunately only compile tested.
Signed-off-by Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c| 103
++++++++++-------------1 file changed, 46 insertions(+), 57 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +9
-9@@ -206,11 +206,9 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, struct amdgpu_amdkfd_fence ***ef_list, unsigned int *ef_count) { - struct reservation_object_list *fobj; - struct reservation_object *resv; - unsigned int i = 0, j = 0, k = 0, shared_count; - unsigned int count = 0; - struct amdgpu_amdkfd_fence **fence_list; + struct reservation_object *resv = bo->tbo.resv; + struct reservation_object_list *old, *new; + unsigned int i, j, k; if (!ef && !ef_list) return -EINVAL;@@ -220,76 +218,67 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, *ef_count = 0; } - resv = bo->tbo.resv; - fobj = reservation_object_get_list(resv); - - if (!fobj) + old = reservation_object_get_list(resv); + if (!old) return 0; - preempt_disable(); - write_seqcount_begin(&resv->seq); + new = kmalloc(offsetof(typeof(*new), shared[old->shared_max]), + GFP_KERNEL); + if (!new) + return -ENOMEM;- /* Go through all the shared fences in the resevation object. If - * ef is specified and it exists in the list, remove it and reduce the
- * count. If ef is not specified, then get the count of eviction fences
- * present.
+ /* Go through all the shared fences in the resevation object and sort
+ * the interesting ones to the end of the list.
*/
-shared_count=fobj->shared_count;
- for (i = 0; i <shared_count; ++i) {
+ for (i = 0, j =old->shared_count, k = 0; i <old->shared_count; ++i) {
structdma_fence*f;
- f = rcu_dereference_protected(fobj->shared[i],
+ f =rcu_dereference_protected(old->shared[i], reservation_object_held(resv)); - if (ef) {
- if (f->context==ef->base.context) {
-dma_fence_put(f);
- fobj->shared_count--;
- } else {
-RCU_INIT_POINTER(fobj->shared[j++], f);
- }
- } else if (to_amdgpu_amdkfd_fence(f))
- count++;
+ if ((ef &&f->context==ef->base.context) ||
+ (!ef &&to_amdgpu_amdkfd_fence(f)))
+RCU_INIT_POINTER(new->shared[--j], f);
+ else
+RCU_INIT_POINTER(new->shared[k++], f);
}
-write_seqcount_end(&resv->seq);
-preempt_enable();
+new->shared_max=old->shared_max;
+new->shared_count= k;
- if (ef || !count)
- return 0;
+ if (!ef) {
+ unsigned int count =old->shared_count- j;
- /* Alloc memory for count number of eviction fence pointers. Fill the
- *ef_listarray andef_count
- */
-fence_list=kcalloc(count, sizeof(struct amdgpu_amdkfd_fence *), - GFP_KERNEL);
- if (!fence_list)
- return-ENOMEM;
+ /* Alloc memory for count number of eviction fence pointers. Fill the
+ *ef_listarray andef_count
+ */
+*ef_list=kcalloc(count, sizeof(**ef_list), GFP_KERNEL); +*ef_count= count;
+ if (!*ef_list) {
+ kfree(new);
+ return-ENOMEM;
+ }Quoted 8 lines
+ }
+
+ /* Install the new fence list, seqcount provides the barriers */ +preempt_disable();
+write_seqcount_begin(&resv->seq);
+RCU_INIT_POINTER(resv->fence, new);preempt_disable();
write_seqcount_begin(&resv->seq);You're disabling preemption and calling
write_seqcount_begintwice. I think this is meant to bewrite_seqcount_end(&resv->seq);
preempt_enable();Quoted 30 lines
- j = 0;
- for (i = 0; i <shared_count; ++i) {
+ /* Drop the references to the removed fences or move them toef_list*/
+ for (i = j, k = 0; i <old->shared_count; ++i) { structdma_fence*f;
- structamdgpu_amdkfd_fence*efence;
- f =rcu_dereference_protected(fobj->shared[i], - reservation_object_held(resv));
-
- efence =to_amdgpu_amdkfd_fence(f);
- if (efence) {
- fence_list[k++] = efence;
- fobj->shared_count--;
- } else {
-RCU_INIT_POINTER(fobj->shared[j++], f);
- }
+ f =rcu_dereference_protected(new->shared[i], + reservation_object_held(resv)); + if (!ef)
+ (*ef_list)[k++] =to_amdgpu_amdkfd_fence(f); + else
+dma_fence_put(f);
}drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0
-6- - write_seqcount_end(&resv->seq); - preempt_enable(); - - *ef_list = fence_list; - *ef_count = k;
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
+
kfree_rcu(old, rcu);
return 0;
}_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
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): 4f6bdaf0-d30b-9ea0-2b51-883c5fd63319-5C7GfCeVMHo@public.gmane.org
Am 15.08.2018 um 20:17 schrieb Felix Kuehling:
Quoted 19 lines · Christian König
Quoted 11 lines
Hi Felix,
yeah, you pretty much nailed it.
The problem is that the array itself is RCU protected. This means that you can only copy the whole structure when you want to update it.
The exception is
reservation_object_add_shared()which only works because we replace an either signaled fence or a fence within the same context but a later sequence number.Quoted 7 lines
This also explains why this is only a band aid and the whole approach of removing fences doesn't work in general. At any time somebody could have taken an RCU reference to the old array, created a copy of it and is now still using this one.
The only 100% correct solution would be to mark the existing fence as signaled and replace it everywhere else.
Quoted 7 lines
Depends what you're trying to achieve. I think the problem you see is, that some reader may still be using the old
reservation_object_listcopy with the fence still in it. But, the remaining lifetime of thereservation_object_listcopy is limited. If we wanted to be sure no more copies with the old fence exist, all we'd need to do is callsynchronize_rcu. Maybe we need to do that before releasing the fence references, or release the fence reference in an RCU callback to be safe.
The assumption that the fence would die with the array is what is incorrect here.
The lifetime of RCUed array object is limit, but there is absolutely no guarantee that somebody didn't made a copy of the fences.
E.g. somebody could have called reservation_object_get_fences_rcu(), reservation_object_copy_fences() or a concurrent reservation_object_wait_timeout_rcu() is underway.
That's also the reason why fences live for much longer than their signaling, e.g. somebody can have a reference to the fence object even hours after it is signaled.
Regards,
Christian.
Quoted 209 lines
Regards,
FelixQuoted 205 lines
Going to fix the copy&paste error I made with the sequence number and send it out again.
Regards,
Christian.Am 14.08.2018 um 22:17 schrieb Felix Kuehling:
Quoted 186 lines
[+Harish]
I think this looks good for the most part. See one comment inline below.
But bear with me while I'm trying to understand what was wrong with the old code. Please correct me if I get this wrong or point out anything I'm missing.
The
reservation_object_listlooks to be protected by a combination of three mechanism:* Holding the reservation object
* RCU
* seqcountUpdating the fence list requires holding the reservation object. But there are some readers that can be called without holding that lock (
reservation_object_copy_fences,reservation_object_get_fences_rcu,reservation_object_wait_timeout_rcu,reservation_object_test_signaled_rcu). They rely on RCU to work on a copy and seqcount to make sure they had the most up-to-date information. So any function updating the fence lists needs to do RCU and seqcount correctly to prevent breaking those readers.As I understand it, RCU with seqcount retry just means that readers will spin retrying while there are writers, and writers are never blocked by readers. Writers are blocked by each other, because they need to hold the reservation.
The code you added looks a lot like
reservation_object_add_shared_replace, which removes fences that have signalled, and atomically replacesobj->fenceswith a newreservation_fence_list. That atomicity is important because each pointer in theobj->fences->sharedarray is separately protected by RCU, but not the array as a whole or theshared_count.See one comment inline.
Regards,
FelixQuoted 104 lines · Christian König
Fix quite a number of bugs here. Unfortunately only compile tested.
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 103 ++++++++++------------- 1 file changed, 46 insertions(+), 57 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +9
-10@@ -206,11 +206,9 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, struct amdgpu_amdkfd_fence ***ef_list, unsigned int *ef_count) { - struct reservation_object_list *fobj; - struct reservation_object *resv; - unsigned int i = 0, j = 0, k = 0, shared_count; - unsigned int count = 0; - struct amdgpu_amdkfd_fence **fence_list; + struct reservation_object *resv = bo->tbo.resv; + struct reservation_object_list *old, *new; + unsigned int i, j, k; if (!ef && !ef_list) return -EINVAL;@@ -220,76 +218,67 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, *ef_count = 0; } - resv = bo->tbo.resv; - fobj = reservation_object_get_list(resv); - - if (!fobj) + old = reservation_object_get_list(resv); + if (!old) return 0; - preempt_disable(); - write_seqcount_begin(&resv->seq); + new = kmalloc(offsetof(typeof(*new), shared[old->shared_max]), + GFP_KERNEL); + if (!new) + return -ENOMEM; - /* Go through all the shared fences in the resevation object. If -* ef is specified and it exists in the list, remove it and reduce the
- * count. If ef is not specified, then get the count of eviction fences
- * present.
+ /* Go through all the shared fences in the resevation object and sort
+ * the interesting ones to the end of the list.
*/
-shared_count=fobj->shared_count;
- for (i = 0; i <shared_count; ++i) {
+ for (i = 0, j =old->shared_count, k = 0; i <old->shared_count; ++i) {
structdma_fence*f;
- f = rcu_dereference_protected(fobj->shared[i],
+ f = rcu_dereference_protected(old->shared[i],
reservation_object_held(resv)); - if (ef) {
- if (f->context==ef->base.context) {
-dma_fence_put(f);
- fobj->shared_count--;
- } else {
-RCU_INIT_POINTER(fobj->shared[j++], f);
- }
- } else if (to_amdgpu_amdkfd_fence(f))
- count++;
+ if ((ef &&f->context==ef->base.context) ||
+ (!ef &&to_amdgpu_amdkfd_fence(f)))
+RCU_INIT_POINTER(new->shared[--j], f);
+ else
+RCU_INIT_POINTER(new->shared[k++], f);
}
-write_seqcount_end(&resv->seq);
-preempt_enable();
+new->shared_max=old->shared_max;
+new->shared_count= k;
- if (ef || !count)
- return 0;
+ if (!ef) {
+ unsigned int count =old->shared_count- j;
- /* Alloc memory for count number of eviction fence pointers.
Fill the
- *ef_listarray andef_count
- */
-fence_list=kcalloc(count, sizeof(struct amdgpu_amdkfd_fence *), - GFP_KERNEL);
- if (!fence_list)
- return-ENOMEM;
+ /* Alloc memory for count number of eviction fence pointers. Fill the
+ *ef_listarray andef_count
+ */
+*ef_list=kcalloc(count, sizeof(**ef_list), GFP_KERNEL); +*ef_count= count;
+ if (!*ef_list) {
+ kfree(new);
+ return-ENOMEM;
+ }Quoted 8 lines
+ }
+
+ /* Install the new fence list, seqcount provides the barriers */ +preempt_disable();
+write_seqcount_begin(&resv->seq);
+RCU_INIT_POINTER(resv->fence, new);
preempt_disable();write_seqcount_begin(&resv->seq);You're disabling preemption and calling
write_seqcount_begintwice. I think this is meant to bewrite_seqcount_end(&resv->seq);
preempt_enable();Quoted 25 lines
- j = 0;
- for (i = 0; i <shared_count; ++i) {
+ /* Drop the references to the removed fences or move them toef_list*/
+ for (i = j, k = 0; i <old->shared_count; ++i) {
structdma_fence*f;
- structamdgpu_amdkfd_fence*efence;
- f = rcu_dereference_protected(fobj->shared[i],
-reservation_object_held(resv));drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +6
-9- - efence = to_amdgpu_amdkfd_fence(f); - if (efence) { - fence_list[k++] = efence; - fobj->shared_count--; - } else { - RCU_INIT_POINTER(fobj->shared[j++], f); - } + f = rcu_dereference_protected(new->shared[i], + reservation_object_held(resv)); + if (!ef) + (*ef_list)[k++] = to_amdgpu_amdkfd_fence(f); + else + dma_fence_put(f); } -Quoted 8 lines
Quoted 8 lines
-
write_seqcount_end(&resv->seq);
-preempt_enable();
-
-*ef_list=fence_list;
-*ef_count= k;
+kfree_rcu(old, rcu);
return 0;
}_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
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): 796370e3-f77a-2b3b-01a3-4d7f55c22d20-5C7GfCeVMHo@public.gmane.org
Quoted 29 lines · Christian König
Am 15.08.2018 um 20:17 schrieb Felix Kuehling:
Quoted 19 lines · Christian König
Quoted 11 lines
Hi Felix,
yeah, you pretty much nailed it.
The problem is that the array itself is RCU protected. This means that you can only copy the whole structure when you want to update it.
The exception is
reservation_object_add_shared()which only works because we replace an either signaled fence or a fence within the same context but a later sequence number.Quoted 7 lines
This also explains why this is only a band aid and the whole approach of removing fences doesn't work in general. At any time somebody could have taken an RCU reference to the old array, created a copy of it and is now still using this one.
The only 100% correct solution would be to mark the existing fence as signaled and replace it everywhere else.
Quoted 8 lines
Depends what you're trying to achieve. I think the problem you see is, that some reader may still be using the old
reservation_object_listcopy with the fence still in it. But, the remaining lifetime of thereservation_object_listcopy is limited. If we wanted to be sure no more copies with the old fence exist, all we'd need to do is callsynchronize_rcu. Maybe we need to do that before releasing the fence references, or release the fence reference in an RCU callback to be safe.
Quoted 2 lines
The assumption that the fence would die with the array is what is incorrect here.
I'm making no such assumption. The point is not to destroy the fence. Only to remove the fence reference from the reservation object. That is, we want any BO with this reservation object to stop checking or waiting for our eviction fence.
Quoted 7 lines
The lifetime of RCUed array object is limit, but there is absolutely no guarantee that somebody didn't made a copy of the fences.
E.g. somebody could have called
reservation_object_get_fences_rcu(),reservation_object_copy_fences()or a concurrentreservation_object_wait_timeout_rcu()is underway.
That's fine. Then there will be additional fence references. When we remove a fence from a reservation object, we don't know and don't care who else is holding a reference to the fence anyway. This is no different.
Regards,
Felix
Quoted 227 lines
That's also the reason why fences live for much longer than their signaling, e.g. somebody can have a reference to the fence object even hours after it is signaled.
Regards,
Christian.Quoted 218 lines
Regards,
FelixQuoted 214 lines
Going to fix the copy&paste error I made with the sequence number and send it out again.
Regards,
Christian.Am 14.08.2018 um 22:17 schrieb Felix Kuehling:
Quoted 195 lines
[+Harish]
I think this looks good for the most part. See one comment inline below.
But bear with me while I'm trying to understand what was wrong with the
old code. Please correct me if I get this wrong or point out anything I'm missing.The
reservation_object_listlooks to be protected by a combination of three mechanism:* Holding the reservation object
* RCU
* seqcountUpdating the fence list requires holding the reservation object. But there are some readers that can be called without holding that lock (
reservation_object_copy_fences,reservation_object_get_fences_rcu,reservation_object_wait_timeout_rcu,reservation_object_test_signaled_rcu). They rely on RCU to work on a copy and seqcount to make sure they had the most up-to-date information.
So any function updating the fence lists needs to do RCU and seqcount correctly to prevent breaking those readers.As I understand it, RCU with seqcount retry just means that readers will
spin retrying while there are writers, and writers are never blocked by
readers. Writers are blocked by each other, because they need to hold the reservation.The code you added looks a lot like
reservation_object_add_shared_replace, which removes fences that have signalled, and atomically replacesobj->fenceswith a newreservation_fence_list. That atomicity is important because each pointer
in theobj->fences->sharedarray is separately protected by RCU, but not
the array as a whole or theshared_count.See one comment inline.
Regards,
FelixQuoted 106 lines · Christian König
Fix quite a number of bugs here. Unfortunately only compile tested.
Signed-off-by Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c| 103
++++++++++-------------1 file changed, 46 insertions(+), 57 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +9
-9@@ -206,11 +206,9 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, struct amdgpu_amdkfd_fence ***ef_list, unsigned int *ef_count) { - struct reservation_object_list *fobj; - struct reservation_object *resv; - unsigned int i = 0, j = 0, k = 0, shared_count; - unsigned int count = 0; - struct amdgpu_amdkfd_fence **fence_list; + struct reservation_object *resv = bo->tbo.resv; + struct reservation_object_list *old, *new; + unsigned int i, j, k; if (!ef && !ef_list) return -EINVAL;@@ -220,76 +218,67 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, *ef_count = 0; } - resv = bo->tbo.resv; - fobj = reservation_object_get_list(resv); - - if (!fobj) + old = reservation_object_get_list(resv); + if (!old) return 0; - preempt_disable(); - write_seqcount_begin(&resv->seq); + new = kmalloc(offsetof(typeof(*new), shared[old->shared_max]), + GFP_KERNEL); + if (!new) + return -ENOMEM;- /* Go through all the shared fences in the resevation object. If
- * ef is specified and it exists in the list, remove it and reduce the
- * count. If ef is not specified, then get the count of eviction fences
- * present.
+ /* Go through all the shared fences in the resevation object and sort
+ * the interesting ones to the end of the list.
*/
-shared_count=fobj->shared_count;
- for (i = 0; i <shared_count; ++i) {
+ for (i = 0, j =old->shared_count, k = 0; i <old->shared_count; ++i) {
structdma_fence*f;
- f = rcu_dereference_protected(fobj->shared[i], + f =rcu_dereference_protected(old->shared[i], reservation_object_held(resv)); - if (ef) {
- if (f->context==ef->base.context) {
-dma_fence_put(f);
- fobj->shared_count--;
- } else {
-RCU_INIT_POINTER(fobj->shared[j++], f); - }
- } else if (to_amdgpu_amdkfd_fence(f))
- count++;
+ if ((ef &&f->context==ef->base.context) ||
+ (!ef &&to_amdgpu_amdkfd_fence(f)))
+RCU_INIT_POINTER(new->shared[--j], f);
+ else
+RCU_INIT_POINTER(new->shared[k++], f);
}
-write_seqcount_end(&resv->seq);
-preempt_enable();
+new->shared_max=old->shared_max;
+new->shared_count= k;
- if (ef || !count)
- return 0;
+ if (!ef) {
+ unsigned int count =old->shared_count- j;
- /* Alloc memory for count number of eviction fence pointers. Fill the
- *ef_listarray andef_count
- */
-fence_list= kcalloc(count,sizeof(struct amdgpu_amdkfd_fence *),
-GFP_KERNEL);
- if (!fence_list)
- return-ENOMEM;
+ /* Alloc memory for count number of eviction fence pointers. Fill the
+ *ef_listarray andef_count
+ */
+*ef_list=kcalloc(count, sizeof(**ef_list), GFP_KERNEL); +*ef_count= count;
+ if (!*ef_list) {
+ kfree(new);
+ return-ENOMEM;
+ }Quoted 8 lines
+ }
+
+ /* Install the new fence list, seqcount provides the barriers */ +preempt_disable();
+write_seqcount_begin(&resv->seq);
+RCU_INIT_POINTER(resv->fence, new);preempt_disable();write_seqcount_begin(&resv->seq);You're disabling preemption and calling
write_seqcount_begintwice. I think this is meant to bewrite_seqcount_end(&resv->seq);
preempt_enable();Quoted 25 lines
- j = 0;
- for (i = 0; i <shared_count; ++i) {
+ /* Drop the references to the removed fences or move them toef_list*/
+ for (i = j, k = 0; i <old->shared_count; ++i) { structdma_fence*f;
- structamdgpu_amdkfd_fence*efence;
- f =rcu_dereference_protected(fobj->shared[i], - reservation_object_held(resv));
-
- efence =to_amdgpu_amdkfd_fence(f);
- if (efence) {
- fence_list[k++] = efence;
- fobj->shared_count--;
- } else {
-RCU_INIT_POINTER(fobj->shared[j++], f);
- }
+ f =rcu_dereference_protected(new->shared[i], + reservation_object_held(resv)); + if (!ef)
+ (*ef_list)[k++] =to_amdgpu_amdkfd_fence(f); + else
+dma_fence_put(f);
}
-Quoted 8 lines
Quoted 8 lines
-
write_seqcount_end(&resv->seq);
-preempt_enable();
-
-*ef_list=fence_list;
-*ef_count= k;
+kfree_rcu(old, rcu);
return 0;
}_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Quoted 4 lines
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
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): 7fe01af1-d1ab-7fec-dbc4-fd603aeb1abc-5C7GfCeVMHo@public.gmane.org
Am 15.08.2018 um 20:49 schrieb Felix Kuehling:
Quoted 29 lines · Christian König
Quoted 28 lines
Am 15.08.2018 um 20:17 schrieb Felix Kuehling:
Quoted 27 lines · Christian König
Quoted 11 lines
Hi Felix,
yeah, you pretty much nailed it.
The problem is that the array itself is RCU protected. This means that you can only copy the whole structure when you want to update it.
The exception is
reservation_object_add_shared()which only works because we replace an either signaled fence or a fence within the same context but a later sequence number.Quoted 7 lines
This also explains why this is only a band aid and the whole approach of removing fences doesn't work in general. At any time somebody could have taken an RCU reference to the old array, created a copy of it and is now still using this one.
The only 100% correct solution would be to mark the existing fence as signaled and replace it everywhere else.
Depends what you're trying to achieve. I think the problem you see is, that some reader may still be using the old
reservation_object_listcopy with the fence still in it. But, the remaining lifetime of thereservation_object_listcopy is limited. If we wanted to be sure no more copies with the old fence exist, all we'd need to do is callsynchronize_rcu. Maybe we need to do that before releasing the fence references, or release the fence reference in an RCU callback to be safe.
Quoted 6 lines
Quoted 2 lines
The assumption that the fence would die with the array is what is incorrect here.
I'm making no such assumption. The point is not to destroy the fence. Only to remove the fence reference from the reservation object. That is, we want any BO with this reservation object to stop checking or waiting for our eviction fence.
Maybe I'm overthinking this, but let me explain the point once more.
See reservation_object_wait_timeout_rcu() for an example of the problem I see here:
Quoted 2 lines
seq =
read_seqcount_begin(&obj->seq);
rcu_read_lock();
....
Quoted 2 lines
if (!dma_fence_get_rcu(lfence)) goto
unlock_retry;
...
Quoted 1 line
rcu_read_unlock();
...
Quoted 4 lines
if (
read_seqcount_retry(&obj->seq, seq)) {dma_fence_put(fence); goto retry;
}
...
Quoted 1 line
ret =
dma_fence_wait_timeout(fence, intr, ret);
In other words the RCU mechanism guarantees that we also wait for newly added fences, but it does not guarantee that we don't wait for a fence which is already removed.
Quoted 9 lines
Quoted 6 lines
The lifetime of RCUed array object is limit, but there is absolutely no guarantee that somebody didn't made a copy of the fences.
E.g. somebody could have called
reservation_object_get_fences_rcu(),reservation_object_copy_fences()or a concurrentreservation_object_wait_timeout_rcu()is underway.That's fine. Then there will be additional fence references. When we remove a fence from a reservation object, we don't know and don't care who else is holding a reference to the fence anyway. This is no different.
So the KFD implementation doesn't care that the fence is removed from a BO but somebody could still start to wait on it because of the BO?
I mean it could be that in the worst case we race and stop a KFD process for no good reason.
Regards,
Christian.
Quoted 228 lines
Regards,
FelixQuoted 224 lines
That's also the reason why fences live for much longer than their signaling, e.g. somebody can have a reference to the fence object even hours after it is signaled.
Regards,
Christian.Quoted 217 lines
Regards,
FelixQuoted 214 lines
Going to fix the copy&paste error I made with the sequence number and send it out again.
Regards,
Christian.Am 14.08.2018 um 22:17 schrieb Felix Kuehling:
Quoted 195 lines
[+Harish]
I think this looks good for the most part. See one comment inline below.
But bear with me while I'm trying to understand what was wrong with the
old code. Please correct me if I get this wrong or point out anything I'm missing.The
reservation_object_listlooks to be protected by a combination of three mechanism:* Holding the reservation object
* RCU
* seqcountUpdating the fence list requires holding the reservation object. But there are some readers that can be called without holding that lock (
reservation_object_copy_fences,reservation_object_get_fences_rcu,reservation_object_wait_timeout_rcu,reservation_object_test_signaled_rcu). They rely on RCU to work on a copy and seqcount to make sure they had the most up-to-date information.
So any function updating the fence lists needs to do RCU and seqcount correctly to prevent breaking those readers.As I understand it, RCU with seqcount retry just means that readers will
spin retrying while there are writers, and writers are never blocked by
readers. Writers are blocked by each other, because they need to hold the reservation.The code you added looks a lot like
reservation_object_add_shared_replace, which removes fences that have signalled, and atomically replacesobj->fenceswith a newreservation_fence_list. That atomicity is important because each pointer
in theobj->fences->sharedarray is separately protected by RCU, but not
the array as a whole or theshared_count.See one comment inline.
Regards,
FelixQuoted 106 lines · Christian König
Fix quite a number of bugs here. Unfortunately only compile tested.
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 103 ++++++++++------------- 1 file changed, 46 insertions(+), 57 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +9
-9@@ -206,11 +206,9 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, struct amdgpu_amdkfd_fence ***ef_list, unsigned int *ef_count) { - struct reservation_object_list *fobj; - struct reservation_object *resv; - unsigned int i = 0, j = 0, k = 0, shared_count; - unsigned int count = 0; - struct amdgpu_amdkfd_fence **fence_list; + struct reservation_object *resv = bo->tbo.resv; + struct reservation_object_list *old, *new; + unsigned int i, j, k; if (!ef && !ef_list) return -EINVAL;@@ -220,76 +218,67 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, *ef_count = 0; } - resv = bo->tbo.resv; - fobj = reservation_object_get_list(resv); - - if (!fobj) + old = reservation_object_get_list(resv); + if (!old) return 0; - preempt_disable(); - write_seqcount_begin(&resv->seq); + new = kmalloc(offsetof(typeof(*new), shared[old->shared_max]), + GFP_KERNEL); + if (!new) + return -ENOMEM; - /* Go through all the shared fences in the resevationobject. If
- * ef is specified and it exists in the list, remove it and reduce the
- * count. If ef is not specified, then get the count of eviction fences
- * present.
+ /* Go through all the shared fences in the resevation object and sort
+ * the interesting ones to the end of the list.
*/
-shared_count=fobj->shared_count;
- for (i = 0; i <shared_count; ++i) {
+ for (i = 0, j =old->shared_count, k = 0; i <old->shared_count; ++i) {
structdma_fence*f;
- f = rcu_dereference_protected(fobj->shared[i],
+ f = rcu_dereference_protected(old->shared[i],
reservation_object_held(resv)); - if (ef) {
- if (f->context==ef->base.context) {
-dma_fence_put(f);
- fobj->shared_count--;
- } else {
-RCU_INIT_POINTER(fobj->shared[j++], f);
- }
- } else if (to_amdgpu_amdkfd_fence(f))
- count++;
+ if ((ef &&f->context==ef->base.context) ||
+ (!ef &&to_amdgpu_amdkfd_fence(f)))
+RCU_INIT_POINTER(new->shared[--j], f);
+ else
+RCU_INIT_POINTER(new->shared[k++], f);
}
-write_seqcount_end(&resv->seq);
-preempt_enable();
+new->shared_max=old->shared_max;
+new->shared_count= k;
- if (ef || !count)
- return 0;
+ if (!ef) {
+ unsigned int count =old->shared_count- j;
- /* Alloc memory for count number of eviction fence pointers.
Fill the
- *ef_listarray andef_count
- */
-fence_list= kcalloc(count,sizeof(struct amdgpu_amdkfd_fence *),
-GFP_KERNEL);
- if (!fence_list)
- return-ENOMEM;
+ /* Alloc memory for count number of eviction fence pointers. Fill the
+ *ef_listarray andef_count
+ */
+*ef_list=kcalloc(count, sizeof(**ef_list), GFP_KERNEL); +*ef_count= count;
+ if (!*ef_list) {
+ kfree(new);
+ return-ENOMEM;
+ }Quoted 8 lines
+ }
+
+ /* Install the new fence list, seqcount provides the barriers */ +preempt_disable();
+write_seqcount_begin(&resv->seq);
+RCU_INIT_POINTER(resv->fence, new);
preempt_disable();write_seqcount_begin(&resv->seq);You're disabling preemption and calling
write_seqcount_begintwice. I think this is meant to bewrite_seqcount_end(&resv->seq);
preempt_enable();Quoted 25 lines
- j = 0;
- for (i = 0; i <shared_count; ++i) {
+ /* Drop the references to the removed fences or move them toef_list*/
+ for (i = j, k = 0; i <old->shared_count; ++i) {
structdma_fence*f;
- structamdgpu_amdkfd_fence*efence;
- f = rcu_dereference_protected(fobj->shared[i],
-reservation_object_held(resv));drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +6
-9- - efence = to_amdgpu_amdkfd_fence(f); - if (efence) { - fence_list[k++] = efence; - fobj->shared_count--; - } else { - RCU_INIT_POINTER(fobj->shared[j++], f); - } + f = rcu_dereference_protected(new->shared[i], + reservation_object_held(resv)); + if (!ef) + (*ef_list)[k++] = to_amdgpu_amdkfd_fence(f); + else + dma_fence_put(f); } -Quoted 8 lines
Quoted 8 lines
-
write_seqcount_end(&resv->seq);
-preempt_enable();
-
-*ef_list=fence_list;
-*ef_count= k;
+kfree_rcu(old, rcu);
return 0;
}_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Quoted 8 lines
Quoted 4 lines
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
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): 1c0200c9-6d5f-c6f8-cea8-bfdf51f8a3bd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Quoted 59 lines · Christian König
Am 15.08.2018 um 20:49 schrieb Felix Kuehling:
Quoted 35 lines · Christian König
Quoted 34 lines
Am 15.08.2018 um 20:17 schrieb Felix Kuehling:
Quoted 33 lines · Christian König
Quoted 20 lines
Hi Felix,
yeah, you pretty much nailed it.
The problem is that the array itself is RCU protected. This means that
you can only copy the whole structure when you want to update it.The exception is
reservation_object_add_shared()which only works because we replace an either signaled fence or a fence within the same
context but a later sequence number.This also explains why this is only a band aid and the whole approach of removing fences doesn't work in general. At any time somebody could
have taken an RCU reference to the old array, created a copy of it and
is now still using this one.Quoted 2 lines
The only 100% correct solution would be to mark the existing fence as signaled and replace it everywhere else.
Depends what you're trying to achieve. I think the problem you see is, that some reader may still be using the old
reservation_object_listcopy
with the fence still in it. But, the remaining lifetime of thereservation_object_listcopy is limited. If we wanted to be sure no more
copies with the old fence exist, all we'd need to do is callsynchronize_rcu. Maybe we need to do that before releasing the fence references, or release the fence reference in an RCU callback to be safe.Quoted 6 lines
Quoted 2 lines
The assumption that the fence would die with the array is what is incorrect here.
I'm making no such assumption. The point is not to destroy the fence. Only to remove the fence reference from the reservation object. That is, we want any BO with this reservation object to stop checking or waiting for our eviction fence.
Maybe I'm overthinking this, but let me explain the point once more.
See
reservation_object_wait_timeout_rcu()for an example of the problem I see here:Quoted 2 lines
seq =
read_seqcount_begin(&obj->seq);
rcu_read_lock();....
Quoted 2 lines
if (!dma_fence_get_rcu(lfence)) goto
unlock_retry;...
Quoted 1 line
rcu_read_unlock();...
Quoted 4 lines
if (
read_seqcount_retry(&obj->seq, seq)) {dma_fence_put(fence); goto retry;
}
Quoted 6 lines
...
Quoted 1 line
ret =
dma_fence_wait_timeout(fence, intr, ret);In other words the RCU mechanism guarantees that we also wait for newly added fences, but it does not guarantee that we don't wait for a fence which is already removed.
I understand that.
Quoted 12 lines
Quoted 11 lines
Quoted 7 lines
The lifetime of RCUed array object is limit, but there is absolutely no guarantee that somebody didn't made a copy of the fences.
E.g. somebody could have called
reservation_object_get_fences_rcu(),reservation_object_copy_fences()or a concurrentreservation_object_wait_timeout_rcu()is underway.That's fine. Then there will be additional fence references. When we remove a fence from a reservation object, we don't know and don't care who else is holding a reference to the fence anyway. This is no different.
Quoted 5 lines
So the KFD implementation doesn't care that the fence is removed from a BO but somebody could still start to wait on it because of the BO?
I mean it could be that in the worst case we race and stop a KFD process for no good reason.
Right. For a more practical example, a KFD BO can get evicted just before the application decides to unmap it. The preemption happens asynchronously, handled by an SDMA job in the GPU scheduler. That job will have an amdgpu_sync object with the eviction fence in it.
While that SDMA job is pending or in progress, the application decides to unmap the BO. That removes the eviction fence from that BO's reservation. But it can't remove the fence from all the sync objects that were previously created and are still in flight. So the preemption will be triggered, and the fence will eventually signal when the KFD preemption is complete.
I don't think that's something we can prevent. The worst case is that a preemption happens unnecessarily if an eviction gets triggered just before removing the fence. But removing the fence will prevent future evictions of the BO from triggering a KFD process preemption. That's the best we can do.
Regards,
Felix
Quoted 249 lines
Regards,
Christian.Quoted 244 lines
Regards,
FelixQuoted 232 lines
That's also the reason why fences live for much longer than their signaling, e.g. somebody can have a reference to the fence object even hours after it is signaled.
Regards,
Christian.Quoted 225 lines
Regards,
FelixQuoted 222 lines
Going to fix the copy&paste error I made with the sequence number and send it out again.
Regards,
Christian.Am 14.08.2018 um 22:17 schrieb Felix Kuehling:
Quoted 203 lines
[+Harish]
I think this looks good for the most part. See one comment inline below.
But bear with me while I'm trying to understand what was wrong with the
old code. Please correct me if I get this wrong or point out anything
I'm missing.The
reservation_object_listlooks to be protected by a combination of
three mechanism:* Holding the reservation object
* RCU
* seqcountUpdating the fence list requires holding the reservation object. But there are some readers that can be called without holding that lock (
reservation_object_copy_fences,reservation_object_get_fences_rcu,reservation_object_wait_timeout_rcu,reservation_object_test_signaled_rcu). They rely on RCU to work on a copy and seqcount to make sure they had the most up-to-date information.
So any function updating the fence lists needs to do RCU and seqcount
correctly to prevent breaking those readers.As I understand it, RCU with seqcount retry just means that readers will
spin retrying while there are writers, and writers are never blocked by
readers. Writers are blocked by each other, because they need to hold
the reservation.The code you added looks a lot like
reservation_object_add_shared_replace, which removes fences that have
signalled, and atomically replacesobj->fenceswith a newreservation_fence_list. That atomicity is important because each pointer
in theobj->fences->sharedarray is separately protected by RCU, but not
the array as a whole or theshared_count.See one comment inline.
Regards,
FelixQuoted 108 lines · Christian König
Fix quite a number of bugs here. Unfortunately only compile tested.
Signed-off-by Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c| 103
++++++++++-------------1 file changed, 46 insertions(+), 57 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +9
-9@@ -206,11 +206,9 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, struct amdgpu_amdkfd_fence ***ef_list, unsigned int *ef_count) { - struct reservation_object_list *fobj; - struct reservation_object *resv; - unsigned int i = 0, j = 0, k = 0, shared_count; - unsigned int count = 0; - struct amdgpu_amdkfd_fence **fence_list; + struct reservation_object *resv = bo->tbo.resv; + struct reservation_object_list *old, *new; + unsigned int i, j, k; if (!ef && !ef_list) return -EINVAL;@@ -220,76 +218,67 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo, *ef_count = 0; } - resv = bo->tbo.resv; - fobj = reservation_object_get_list(resv); - - if (!fobj) + old = reservation_object_get_list(resv); + if (!old) return 0; - preempt_disable(); - write_seqcount_begin(&resv->seq); + new = kmalloc(offsetof(typeof(*new), shared[old->shared_max]), + GFP_KERNEL); + if (!new) + return -ENOMEM;- /* Go through all the shared fences in the resevation object. If
- * ef is specified and it exists in the list, remove it and reduce the
- * count. If ef is not specified, then get the count of eviction fences
- * present.
+ /* Go through all the shared fences in the resevation object and sort
+ * the interesting ones to the end of the list.
*/
-shared_count=fobj->shared_count;
- for (i = 0; i <shared_count; ++i) {
+ for (i = 0, j =old->shared_count, k = 0; i <old->shared_count; ++i) {
structdma_fence*f;
- f = rcu_dereference_protected(fobj->shared[i], + f =rcu_dereference_protected(old->shared[i], reservation_object_held(resv)); - if (ef) {
- if (f->context==ef->base.context) {
-dma_fence_put(f);
- fobj->shared_count--;
- } else {
-RCU_INIT_POINTER(fobj->shared[j++], f); - }
- } else if (to_amdgpu_amdkfd_fence(f))
- count++;
+ if ((ef &&f->context==ef->base.context) || + (!ef &&to_amdgpu_amdkfd_fence(f)))
+RCU_INIT_POINTER(new->shared[--j], f);
+ else
+RCU_INIT_POINTER(new->shared[k++], f);
}
-write_seqcount_end(&resv->seq);
-preempt_enable();
+new->shared_max=old->shared_max;
+new->shared_count= k;
- if (ef || !count)
- return 0;
+ if (!ef) {
+ unsigned int count =old->shared_count- j;
- /* Alloc memory for count number of eviction fence pointers.
Fill the
- *ef_listarray andef_count
- */
-fence_list= kcalloc(count,sizeof(struct amdgpu_amdkfd_fence *),
-GFP_KERNEL);
- if (!fence_list)
- return-ENOMEM;
+ /* Alloc memory for count number of eviction fence pointers. Fill the
+ *ef_listarray andef_count
+ */
+*ef_list=kcalloc(count, sizeof(**ef_list), GFP_KERNEL); +*ef_count= count;
+ if (!*ef_list) {
+ kfree(new);
+ return-ENOMEM;
+ }
+ }Quoted 8 lines
+
+ /* Install the new fence list, seqcount provides the barriers */
+preempt_disable();
+write_seqcount_begin(&resv->seq);
+RCU_INIT_POINTER(resv->fence, new);preempt_disable();write_seqcount_begin(&resv->seq);You're disabling preemption and calling
write_seqcount_begintwice. I
think this is meant to bewrite_seqcount_end(&resv->seq);
preempt_enable();Quoted 25 lines
- j = 0;
- for (i = 0; i <shared_count; ++i) {
+ /* Drop the references to the removed fences or move them toef_list*/
+ for (i = j, k = 0; i <old->shared_count; ++i) { structdma_fence*f;
- structamdgpu_amdkfd_fence*efence;
- f =rcu_dereference_protected(fobj->shared[i], - reservation_object_held(resv));
-
- efence =to_amdgpu_amdkfd_fence(f);
- if (efence) {
- fence_list[k++] = efence;
- fobj->shared_count--;
- } else {
-RCU_INIT_POINTER(fobj->shared[j++], f);
- }
+ f =rcu_dereference_protected(new->shared[i], + reservation_object_held(resv)); + if (!ef)
+ (*ef_list)[k++] =to_amdgpu_amdkfd_fence(f); + else
+dma_fence_put(f);
}
-Quoted 8 lines
Quoted 8 lines
-
write_seqcount_end(&resv->seq);
-preempt_enable();
-
-*ef_list=fence_list;
-*ef_count= k;
+kfree_rcu(old, rcu);
return 0;
}_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfxQuoted 4 lines
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
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): 6d992bc6-479f-12d4-928b-06d751ab477e-5C7GfCeVMHo@public.gmane.org
Am 16.08.2018 um 18:50 schrieb Felix Kuehling:
Quoted 16 lines · Christian König
[SNIP]
Quoted 2 lines
I mean it could be that in the worst case we race and stop a KFD process for no good reason.
Right. For a more practical example, a KFD BO can get evicted just before the application decides to unmap it. The preemption happens asynchronously, handled by an SDMA job in the GPU scheduler. That job will have an
amdgpu_syncobject with the eviction fence in it.While that SDMA job is pending or in progress, the application decides to unmap the BO. That removes the eviction fence from that BO's reservation. But it can't remove the fence from all the sync objects that were previously created and are still in flight. So the preemption will be triggered, and the fence will eventually signal when the KFD preemption is complete.
Quoted 5 lines
I don't think that's something we can prevent. The worst case is that a preemption happens unnecessarily if an eviction gets triggered just before removing the fence. But removing the fence will prevent future evictions of the BO from triggering a KFD process preemption. That's the best we can do.
It's true that you can't drop the SDMA job which wants to evict the BO, but at this time the fence signaling is already underway and not stoppable anymore.
Replacing the fence with a new one would just be much more cleaner and fix quite a bunch of corner cases where the KFD process would be preempted without good reason.
It's probably quite a bit of more CPU overhead of doing so, but I think that this would still be the more fail prove option.
Regards,
Christian.
Quoted 4 lines
Regards,
Felix
_______________________________________________
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): 09593d90-91a4-31d7-edcf-d363c83faf82-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Quoted 23 lines · Christian König
Am 16.08.2018 um 18:50 schrieb Felix Kuehling:
Quoted 16 lines · Christian König
[SNIP]
Quoted 2 lines
I mean it could be that in the worst case we race and stop a KFD process for no good reason.
Right. For a more practical example, a KFD BO can get evicted just before the application decides to unmap it. The preemption happens asynchronously, handled by an SDMA job in the GPU scheduler. That job will have an
amdgpu_syncobject with the eviction fence in it.While that SDMA job is pending or in progress, the application decides to unmap the BO. That removes the eviction fence from that BO's reservation. But it can't remove the fence from all the sync objects that were previously created and are still in flight. So the preemption will be triggered, and the fence will eventually signal when the KFD preemption is complete.
Quoted 5 lines
I don't think that's something we can prevent. The worst case is that a preemption happens unnecessarily if an eviction gets triggered just before removing the fence. But removing the fence will prevent future evictions of the BO from triggering a KFD process preemption. That's the best we can do.
Quoted 7 lines
It's true that you can't drop the SDMA job which wants to evict the BO, but at this time the fence signaling is already underway and not stoppable anymore.
Replacing the fence with a new one would just be much more cleaner and fix quite a bunch of corner cases where the KFD process would be preempted without good reason.
Replacing the fence cleanly probably also involves a preemption, so you don't gain anything.
Regards,
Felix
Quoted 13 lines
It's probably quite a bit of more CPU overhead of doing so, but I think that this would still be the more fail prove option.
Regards,
Christian.Quoted 4 lines
Regards,
Felix
_______________________________________________
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): 70f7faac-61f4-b570-cb99-80a2d4694ef6-5C7GfCeVMHo@public.gmane.org
Am 16.08.2018 um 20:23 schrieb Felix Kuehling:
Quoted 27 lines · Christian König
Quoted 26 lines
Am 16.08.2018 um 18:50 schrieb Felix Kuehling:
Quoted 16 lines · Christian König
[SNIP]
Quoted 2 lines
I mean it could be that in the worst case we race and stop a KFD process for no good reason.
Right. For a more practical example, a KFD BO can get evicted just before the application decides to unmap it. The preemption happens asynchronously, handled by an SDMA job in the GPU scheduler. That job will have an
amdgpu_syncobject with the eviction fence in it.While that SDMA job is pending or in progress, the application decides to unmap the BO. That removes the eviction fence from that BO's reservation. But it can't remove the fence from all the sync objects that were previously created and are still in flight. So the preemption will be triggered, and the fence will eventually signal when the KFD preemption is complete.
Quoted 5 lines
I don't think that's something we can prevent. The worst case is that a preemption happens unnecessarily if an eviction gets triggered just before removing the fence. But removing the fence will prevent future evictions of the BO from triggering a KFD process preemption. That's the best we can do.
It's true that you can't drop the SDMA job which wants to evict the BO, but at this time the fence signaling is already underway and not stoppable anymore.
Quoted 5 lines
Quoted 3 lines
Replacing the fence with a new one would just be much more cleaner and fix quite a bunch of corner cases where the KFD process would be preempted without good reason.
Replacing the fence cleanly probably also involves a preemption, so you don't gain anything.
Mhm, why that?
My idea would be to create a new fence, replace the old one with the new one and then manually signal the old one.
So why should there be a preemption triggered here?
Christian.
Quoted 14 lines
Regards,
FelixQuoted 10 lines
It's probably quite a bit of more CPU overhead of doing so, but I think that this would still be the more fail prove option.
Regards,
Christian.Quoted 3 lines
Regards,
Felix
_______________________________________________
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): d7440290-7343-661c-23e6-65ff19d25ab3-5C7GfCeVMHo@public.gmane.org
Quoted 37 lines · Christian König
Am 16.08.2018 um 20:23 schrieb Felix Kuehling:
Quoted 30 lines · Christian König
Quoted 29 lines
Am 16.08.2018 um 18:50 schrieb Felix Kuehling:
Quoted 17 lines · Christian König
[SNIP]
Quoted 2 lines
I mean it could be that in the worst case we race and stop a KFD process for no good reason.
Right. For a more practical example, a KFD BO can get evicted just before the application decides to unmap it. The preemption happens asynchronously, handled by an SDMA job in the GPU scheduler. That job will have an
amdgpu_syncobject with the eviction fence in it.While that SDMA job is pending or in progress, the application decides to unmap the BO. That removes the eviction fence from that BO's reservation. But it can't remove the fence from all the sync objects that were previously created and are still in flight. So the preemption
will be triggered, and the fence will eventually signal when the KFD preemption is complete.Quoted 7 lines
I don't think that's something we can prevent. The worst case is that a
preemption happens unnecessarily if an eviction gets triggered just before removing the fence. But removing the fence will prevent future evictions of the BO from triggering a KFD process preemption. That's the
best we can do.It's true that you can't drop the SDMA job which wants to evict the BO, but at this time the fence signaling is already underway and not stoppable anymore.
Quoted 5 lines
Quoted 3 lines
Replacing the fence with a new one would just be much more cleaner and fix quite a bunch of corner cases where the KFD process would be preempted without good reason.
Replacing the fence cleanly probably also involves a preemption, so you don't gain anything.
Quoted 6 lines
Mhm, why that?
My idea would be to create a new fence, replace the old one with the new one and then manually signal the old one.
So why should there be a preemption triggered here?
Right maybe you can do this without triggering a preemption, but it would require a bunch of new code. Currently the only thing that replaces an old eviction fence with a new one is a preemption+restore.
You'll need to replace the fence in all BOs belonging to the process. Since removing a fence is something you don't want to do, that really means adding a new fence, and leaving the old fence in place until it signals. In the mean time you have two eviction fences active for this process, and if either one gets triggered, you'll get a preemption. Only after all BOs have the new eviction fence, you can disarm the old eviction fence and signal it (without triggering a preemption).
The way I see it, this adds a bunch of CPU overhead (depending on the number of BOs in the process), and increases the likelihood of unnecessary preemptions, because it takes that much longer for the operation to complete.
Talking about overhead, imagine a process cleaning up at process termination, which unmaps and frees all BOs. Each BO that gets freed replaces the eviction fence on all remaining BOs. If you have N BOs, you'll end up creating N new eviction fences in the process. The overhead scales with O(N²) of the number of BOs in the process.
Regards,
Felix
Quoted 14 lines
Christian.
Quoted 11 lines
Regards,
FelixQuoted 7 lines
It's probably quite a bit of more CPU overhead of doing so, but I think that this would still be the more fail prove option.
Regards,
Christian.
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Regards,
Felix_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
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): fbd0df5a-4e78-d077-dd59-df8732ac3cb5-5C7GfCeVMHo@public.gmane.org
Am 16.08.2018 um 20:43 schrieb Felix Kuehling:
Quoted 56 lines · Christian König
Quoted 34 lines
Am 16.08.2018 um 20:23 schrieb Felix Kuehling:
Quoted 33 lines · Christian König
Quoted 32 lines
Am 16.08.2018 um 18:50 schrieb Felix Kuehling:
Quoted 17 lines · Christian König
[SNIP]
Quoted 2 lines
I mean it could be that in the worst case we race and stop a KFD process for no good reason.
Right. For a more practical example, a KFD BO can get evicted just before the application decides to unmap it. The preemption happens asynchronously, handled by an SDMA job in the GPU scheduler. That job will have an
amdgpu_syncobject with the eviction fence in it.While that SDMA job is pending or in progress, the application decides to unmap the BO. That removes the eviction fence from that BO's reservation. But it can't remove the fence from all the sync objects that were previously created and are still in flight. So the preemption
will be triggered, and the fence will eventually signal when the KFD preemption is complete.Quoted 7 lines
I don't think that's something we can prevent. The worst case is that a
preemption happens unnecessarily if an eviction gets triggered just before removing the fence. But removing the fence will prevent future evictions of the BO from triggering a KFD process preemption. That's the
best we can do.It's true that you can't drop the SDMA job which wants to evict the BO, but at this time the fence signaling is already underway and not stoppable anymore.
Replacing the fence with a new one would just be much more cleaner and fix quite a bunch of corner cases where the KFD process would be preempted without good reason.
Quoted 8 lines
Quoted 2 lines
Replacing the fence cleanly probably also involves a preemption, so you don't gain anything.
Mhm, why that?
My idea would be to create a new fence, replace the old one with the new one and then manually signal the old one.
So why should there be a preemption triggered here?
Right maybe you can do this without triggering a preemption, but it would require a bunch of new code. Currently the only thing that replaces an old eviction fence with a new one is a preemption+restore.
You'll need to replace the fence in all BOs belonging to the process. Since removing a fence is something you don't want to do, that really means adding a new fence, and leaving the old fence in place until it signals. In the mean time you have two eviction fences active for this process, and if either one gets triggered, you'll get a preemption. Only after all BOs have the new eviction fence, you can disarm the old eviction fence and signal it (without triggering a preemption).
Quoted 4 lines
The way I see it, this adds a bunch of CPU overhead (depending on the number of BOs in the process), and increases the likelihood of unnecessary preemptions, because it takes that much longer for the operation to complete.
Yeah, the CPU overhead is indeed a really good point.
Quoted 5 lines
Talking about overhead, imagine a process cleaning up at process termination, which unmaps and frees all BOs. Each BO that gets freed replaces the eviction fence on all remaining BOs. If you have N BOs, you'll end up creating N new eviction fences in the process. The overhead scales with O(N²) of the number of BOs in the process.
Well as I said that is the only case where I think replacing fences is unproblematic.
E.g. when a BO is freed we already have a mechanism to copy all fences to a local reservation object (to prevent that any new fences can be added to the BO if it shares it's reservation object).
At this time it should be really easy to filter out BOs you don't want to wait for when the BO is destructed.
But anyway, when you can live with the possibility that a stale fence triggers a process preemption than there isn't much point in cleaning this up.
Regards,
Christian.
Quoted 23 lines
Regards,
FelixQuoted 19 lines
Christian.
Quoted 13 lines
Regards,
FelixQuoted 10 lines
It's probably quite a bit of more CPU overhead of doing so, but I think that this would still be the more fail prove option.
Regards,
Christian.Quoted 3 lines
Regards,
Felix_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx