RE: [PATCH] drm/gpu-sched: fix force APP kill hang(v2)

2018-04-10 Thread Deng, Emily
Thanks Christian, will refine the code as your suggestion.

Best Wishes,
Emily Deng

> -Original Message-
> From: Christian König [mailto:ckoenig.leichtzumer...@gmail.com]
> Sent: Wednesday, April 04, 2018 6:02 PM
> To: Deng, Emily ; amd-gfx@lists.freedesktop.org
> Cc: Liu, Monk 
> Subject: Re: [PATCH] drm/gpu-sched: fix force APP kill hang(v2)
> 
> Am 04.04.2018 um 11:37 schrieb Emily Deng:
> > issue:
> > there are VMC page fault occured if force APP kill during 3dmark test,
> > the cause is in entity_fini we manually signal all those jobs in
> > entity's queue which confuse the sync/dep
> > mechanism:
> >
> > 1)page fault occured in sdma's clear job which operate on shadow
> > buffer, and shadow buffer's Gart table is cleaned by ttm_bo_release
> > since the fence in its reservation was fake signaled by entity_fini()
> > under the case of SIGKILL received.
> >
> > 2)page fault occured in gfx' job because during the lifetime of gfx
> > job we manually fake signal all jobs from its entity in entity_fini(),
> > thus the unmapping/clear PTE job depend on those result fence is
> > satisfied and sdma start clearing the PTE and lead to GFX page fault.
> >
> > fix:
> > 1)should at least wait all jobs already scheduled complete in
> > entity_fini() if SIGKILL is the case.
> >
> > 2)if a fence signaled and try to clear some entity's dependency,
> > should set this entity guilty to prevent its job really run since the
> > dependency is fake signaled.
> >
> > v2:
> > splitting drm_sched_entity_fini() into two functions:
> > 1)The first one is does the waiting, removes the entity from the
> > runqueue and returns an error when the process was killed.
> > 2)The second one then goes over the entity, install it as completion
> > signal for the remaining jobs and signals all jobs with an error code.
> 
> First of all this patch won't work like this. You need to call the first part
> before the VM teardown in amdgpu_driver_postclose_kms() and the second
> part after the VM teardown.
> 
> Then please come up with a better name than fini1 and fini2, something like
> drm_sched_entity_cleanup() or something like this.
> 
> Then I think it is harmless to call the _cleanup() function from the
> _fini() function and only change the places where it makes sense to
> explicitely call _cleanup().
> 
> E.g. add a new function amdgpu_ctx_mgr_cleanup() which calls the
> cleanup() for each still open ctx and then modify the order in
> amdgpu_driver_postclose_kms().
> 
> Regards,
> Christian.
> 
> >
> > Signed-off-by: Monk Liu 
> > Signed-off-by: Emily Deng 
> > ---
> >   drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c   | 13 +-
> >   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c   |  4 +-
> >   drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c   |  3 +-
> >   drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c   |  3 +-
> >   drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c   |  8 +++-
> >   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c|  6 ++-
> >   drivers/gpu/drm/amd/amdgpu/uvd_v6_0.c |  3 +-
> >   drivers/gpu/drm/amd/amdgpu/uvd_v7_0.c |  3 +-
> >   drivers/gpu/drm/scheduler/gpu_scheduler.c | 67
> +--
> >   include/drm/gpu_scheduler.h   |  7 +++-
> >   10 files changed, 93 insertions(+), 24 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
> > b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
> > index 09d35051..69dadf9 100644
> > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
> > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
> > @@ -104,8 +104,13 @@ static int amdgpu_ctx_init(struct amdgpu_device
> > *adev,
> >
> >   failed:
> > for (j = 0; j < i; j++)
> > -   drm_sched_entity_fini(&adev->rings[j]->sched,
> > +   drm_sched_entity_fini1(&adev->rings[j]->sched,
> >   &ctx->rings[j].entity);
> > +
> > +   for (j = 0; j < i; j++)
> > +   drm_sched_entity_fini2(&adev->rings[j]->sched,
> > + &ctx->rings[j].entity);
> > +
> > kfree(ctx->fences);
> > ctx->fences = NULL;
> > return r;
> > @@ -126,7 +131,11 @@ static void amdgpu_ctx_fini(struct amdgpu_ctx
> *ctx)
> > ctx->fences = NULL;
> >
> > for (i = 0; i < adev->num_rings; i++)
> > -   drm_sched_entity_fini(&adev->rings[i]->sched,
> > +   drm_sched_entity_fini1(&adev->rings[i]->sched,
> > +

Re: [PATCH] drm/gpu-sched: fix force APP kill hang(v2)

2018-04-04 Thread Christian König

Am 04.04.2018 um 11:37 schrieb Emily Deng:

issue:
there are VMC page fault occured if force APP kill during
3dmark test, the cause is in entity_fini we manually signal
all those jobs in entity's queue which confuse the sync/dep
mechanism:

1)page fault occured in sdma's clear job which operate on
shadow buffer, and shadow buffer's Gart table is cleaned by
ttm_bo_release since the fence in its reservation was fake signaled
by entity_fini() under the case of SIGKILL received.

2)page fault occured in gfx' job because during the lifetime
of gfx job we manually fake signal all jobs from its entity
in entity_fini(), thus the unmapping/clear PTE job depend on those
result fence is satisfied and sdma start clearing the PTE and lead
to GFX page fault.

fix:
1)should at least wait all jobs already scheduled complete in entity_fini()
if SIGKILL is the case.

2)if a fence signaled and try to clear some entity's dependency, should
set this entity guilty to prevent its job really run since the dependency
is fake signaled.

v2:
splitting drm_sched_entity_fini() into two functions:
1)The first one is does the waiting, removes the entity from the
runqueue and returns an error when the process was killed.
2)The second one then goes over the entity, install it as
completion signal for the remaining jobs and signals all jobs
with an error code.


First of all this patch won't work like this. You need to call the first 
part before the VM teardown in amdgpu_driver_postclose_kms() and the 
second part after the VM teardown.


Then please come up with a better name than fini1 and fini2, something 
like drm_sched_entity_cleanup() or something like this.


Then I think it is harmless to call the _cleanup() function from the 
_fini() function and only change the places where it makes sense to 
explicitely call _cleanup().


E.g. add a new function amdgpu_ctx_mgr_cleanup() which calls the 
cleanup() for each still open ctx and then modify the order in 
amdgpu_driver_postclose_kms().


Regards,
Christian.



Signed-off-by: Monk Liu 
Signed-off-by: Emily Deng 
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c   | 13 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c   |  4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c   |  3 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c   |  3 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c   |  8 +++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c|  6 ++-
  drivers/gpu/drm/amd/amdgpu/uvd_v6_0.c |  3 +-
  drivers/gpu/drm/amd/amdgpu/uvd_v7_0.c |  3 +-
  drivers/gpu/drm/scheduler/gpu_scheduler.c | 67 +--
  include/drm/gpu_scheduler.h   |  7 +++-
  10 files changed, 93 insertions(+), 24 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
index 09d35051..69dadf9 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -104,8 +104,13 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
  
  failed:

for (j = 0; j < i; j++)
-   drm_sched_entity_fini(&adev->rings[j]->sched,
+   drm_sched_entity_fini1(&adev->rings[j]->sched,
  &ctx->rings[j].entity);
+
+   for (j = 0; j < i; j++)
+   drm_sched_entity_fini2(&adev->rings[j]->sched,
+ &ctx->rings[j].entity);
+
kfree(ctx->fences);
ctx->fences = NULL;
return r;
@@ -126,7 +131,11 @@ static void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
ctx->fences = NULL;
  
  	for (i = 0; i < adev->num_rings; i++)

-   drm_sched_entity_fini(&adev->rings[i]->sched,
+   drm_sched_entity_fini1(&adev->rings[i]->sched,
+ &ctx->rings[i].entity);
+
+   for (i = 0; i < adev->num_rings; i++)
+   drm_sched_entity_fini2(&adev->rings[i]->sched,
  &ctx->rings[i].entity);
  
  	amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index d2ab404..83d46bb 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -131,8 +131,10 @@ static int amdgpu_ttm_global_init(struct amdgpu_device 
*adev)
  static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
  {
if (adev->mman.mem_global_referenced) {
-   drm_sched_entity_fini(adev->mman.entity.sched,
+   drm_sched_entity_fini1(adev->mman.entity.sched,
  &adev->mman.entity);
+   drm_sched_entity_fini2(adev->mman.entity.sched,
+  &adev->mman.entity);
mutex_destroy(&adev->mman.gtt_window_lock);
drm_global_item_unref(&adev->mman.bo_global_ref.ref);
drm_global_item_unref(&adev->mman.mem_global_ref);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c 
b/drivers/gpu/drm/amd/amd

[PATCH] drm/gpu-sched: fix force APP kill hang(v2)

2018-04-04 Thread Emily Deng
issue:
there are VMC page fault occured if force APP kill during
3dmark test, the cause is in entity_fini we manually signal
all those jobs in entity's queue which confuse the sync/dep
mechanism:

1)page fault occured in sdma's clear job which operate on
shadow buffer, and shadow buffer's Gart table is cleaned by
ttm_bo_release since the fence in its reservation was fake signaled
by entity_fini() under the case of SIGKILL received.

2)page fault occured in gfx' job because during the lifetime
of gfx job we manually fake signal all jobs from its entity
in entity_fini(), thus the unmapping/clear PTE job depend on those
result fence is satisfied and sdma start clearing the PTE and lead
to GFX page fault.

fix:
1)should at least wait all jobs already scheduled complete in entity_fini()
if SIGKILL is the case.

2)if a fence signaled and try to clear some entity's dependency, should
set this entity guilty to prevent its job really run since the dependency
is fake signaled.

v2:
splitting drm_sched_entity_fini() into two functions:
1)The first one is does the waiting, removes the entity from the
runqueue and returns an error when the process was killed.
2)The second one then goes over the entity, install it as
completion signal for the remaining jobs and signals all jobs
with an error code.

Signed-off-by: Monk Liu 
Signed-off-by: Emily Deng 
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c   | 13 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c   |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c   |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c   |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c   |  8 +++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c|  6 ++-
 drivers/gpu/drm/amd/amdgpu/uvd_v6_0.c |  3 +-
 drivers/gpu/drm/amd/amdgpu/uvd_v7_0.c |  3 +-
 drivers/gpu/drm/scheduler/gpu_scheduler.c | 67 +--
 include/drm/gpu_scheduler.h   |  7 +++-
 10 files changed, 93 insertions(+), 24 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
index 09d35051..69dadf9 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -104,8 +104,13 @@ static int amdgpu_ctx_init(struct amdgpu_device *adev,
 
 failed:
for (j = 0; j < i; j++)
-   drm_sched_entity_fini(&adev->rings[j]->sched,
+   drm_sched_entity_fini1(&adev->rings[j]->sched,
  &ctx->rings[j].entity);
+
+   for (j = 0; j < i; j++)
+   drm_sched_entity_fini2(&adev->rings[j]->sched,
+ &ctx->rings[j].entity);
+
kfree(ctx->fences);
ctx->fences = NULL;
return r;
@@ -126,7 +131,11 @@ static void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
ctx->fences = NULL;
 
for (i = 0; i < adev->num_rings; i++)
-   drm_sched_entity_fini(&adev->rings[i]->sched,
+   drm_sched_entity_fini1(&adev->rings[i]->sched,
+ &ctx->rings[i].entity);
+
+   for (i = 0; i < adev->num_rings; i++)
+   drm_sched_entity_fini2(&adev->rings[i]->sched,
  &ctx->rings[i].entity);
 
amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index d2ab404..83d46bb 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -131,8 +131,10 @@ static int amdgpu_ttm_global_init(struct amdgpu_device 
*adev)
 static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
 {
if (adev->mman.mem_global_referenced) {
-   drm_sched_entity_fini(adev->mman.entity.sched,
+   drm_sched_entity_fini1(adev->mman.entity.sched,
  &adev->mman.entity);
+   drm_sched_entity_fini2(adev->mman.entity.sched,
+  &adev->mman.entity);
mutex_destroy(&adev->mman.gtt_window_lock);
drm_global_item_unref(&adev->mman.bo_global_ref.ref);
drm_global_item_unref(&adev->mman.mem_global_ref);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c
index 627542b..74a6953 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c
@@ -277,7 +277,8 @@ int amdgpu_uvd_sw_fini(struct amdgpu_device *adev)
int i;
kfree(adev->uvd.saved_bo);
 
-   drm_sched_entity_fini(&adev->uvd.ring.sched, &adev->uvd.entity);
+   drm_sched_entity_fini1(&adev->uvd.ring.sched, &adev->uvd.entity);
+   drm_sched_entity_fini2(&adev->uvd.ring.sched, &adev->uvd.entity);
 
amdgpu_bo_free_kernel(&adev->uvd.vcpu_bo,
  &adev->uvd.gpu_addr,
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
index a33804b..4166d26 100644
--- a/drive