From: Andrey Grodzovsky Subject: Re: [PATCH 1/2] drm/scheduler: only kill entity if last user is killed v2 Date: Mon, 30 Jul 2018 16:42:18 -0400 Message-ID: <36e09782-2acf-16cf-7be6-5ca3c9c3c9fb@amd.com> References: <20180730110352.2657-1-christian.koenig@amd.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1530797043==" Return-path: Received: from NAM03-DM3-obe.outbound.protection.outlook.com (mail-dm3nam03on0061.outbound.protection.outlook.com [104.47.41.61]) by gabe.freedesktop.org (Postfix) with ESMTPS id 60D1389B49 for ; Mon, 30 Jul 2018 20:42:27 +0000 (UTC) In-Reply-To: Content-Language: en-US List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" To: Nayan Deshmukh , ckoenig.leichtzumerken@gmail.com Cc: Maling list - DRI developers List-Id: dri-devel@lists.freedesktop.org This is a multi-part message in MIME format. --===============1530797043== Content-Type: multipart/alternative; boundary="------------B1D59BD7DF63996E2C24C134" Content-Language: en-US This is a multi-part message in MIME format. --------------B1D59BD7DF63996E2C24C134 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit I believe that in this case if (!entity->rq) {     DRM_ERROR...     return; } clause will take place. P.S I remember we planned to actually propagate the error back to the caller so i guess we should take care of this sooner or later. The change is Reviewed-by: Andrey Grodzovsky Andrey On 07/30/2018 09:34 AM, Nayan Deshmukh wrote: > Hi Christian, > > The code looks good to me. But I was just wondering what will happen > when the last user is killed and some other user tries to push to the > entity. > > Regards, > Nayan Deshmukh > > On Mon, Jul 30, 2018 at 4:33 PM Christian König > > wrote: > > Note which task is using the entity and only kill it if the last > user of > the entity is killed. This should prevent problems when entities > are leaked to > child processes. > > v2: add missing kernel doc > > Signed-off-by: Christian König > > --- >  drivers/gpu/drm/scheduler/gpu_scheduler.c | 6 +++++- >  include/drm/gpu_scheduler.h               | 2 ++ >  2 files changed, 7 insertions(+), 1 deletion(-) > > diff --git a/drivers/gpu/drm/scheduler/gpu_scheduler.c > b/drivers/gpu/drm/scheduler/gpu_scheduler.c > index 3f2fc5e8242a..f563e4fbb4b6 100644 > --- a/drivers/gpu/drm/scheduler/gpu_scheduler.c > +++ b/drivers/gpu/drm/scheduler/gpu_scheduler.c > @@ -275,6 +275,7 @@ static void > drm_sched_entity_kill_jobs_cb(struct dma_fence *f, >  long drm_sched_entity_flush(struct drm_sched_entity *entity, long > timeout) >  { >         struct drm_gpu_scheduler *sched; > +       struct task_struct *last_user; >         long ret = timeout; > >         sched = entity->rq->sched; > @@ -295,7 +296,9 @@ long drm_sched_entity_flush(struct > drm_sched_entity *entity, long timeout) > > >         /* For killed process disable any more IBs enqueue right > now */ > -       if ((current->flags & PF_EXITING) && (current->exit_code > == SIGKILL)) > +       last_user = cmpxchg(&entity->last_user, > current->group_leader, NULL); > +       if ((!last_user || last_user == current->group_leader) && > +           (current->flags & PF_EXITING) && (current->exit_code > == SIGKILL)) >                 drm_sched_entity_set_rq(entity, NULL); > >         return ret; > @@ -541,6 +544,7 @@ void drm_sched_entity_push_job(struct > drm_sched_job *sched_job, > >         trace_drm_sched_job(sched_job, entity); > > +       WRITE_ONCE(entity->last_user, current->group_leader); >         first = spsc_queue_push(&entity->job_queue, > &sched_job->queue_node); > >         /* first job wakes up scheduler */ > diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h > index 091b9afcd184..21c648b0b2a1 100644 > --- a/include/drm/gpu_scheduler.h > +++ b/include/drm/gpu_scheduler.h > @@ -66,6 +66,7 @@ enum drm_sched_priority { >   * @guilty: points to ctx's guilty. >   * @fini_status: contains the exit status in case the process was > signalled. >   * @last_scheduled: points to the finished fence of the last > scheduled job. > + * @last_user: last group leader pushing a job into the entity. >   * >   * Entities will emit jobs in order to their corresponding hardware >   * ring, and the scheduler will alternate between entities based on > @@ -85,6 +86,7 @@ struct drm_sched_entity { >         struct dma_fence_cb             cb; >         atomic_t                        *guilty; >         struct dma_fence                *last_scheduled; > +       struct task_struct              *last_user; >  }; > >  /** > -- > 2.14.1 > > > > _______________________________________________ > dri-devel mailing list > dri-devel@lists.freedesktop.org > https://lists.freedesktop.org/mailman/listinfo/dri-devel --------------B1D59BD7DF63996E2C24C134 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

I believe that in this case

if (!entity->rq) {

    DRM_ERROR...

    return;

}

clause will take place.

P.S I remember we planned to actually propagate the error back to the caller so i guess we should take care of this sooner or later.

The change is Reviewed-by: Andrey Grodzovsky <andrey.grodzovsky@amd.com>

Andrey


On 07/30/2018 09:34 AM, Nayan Deshmukh wrote:
Hi Christian,

The code looks good to me. But I was just wondering what will happen when the last user is killed and some other user tries to push to the entity.

Regards,
Nayan Deshmukh

On Mon, Jul 30, 2018 at 4:33 PM Christian König <ckoenig.leichtzumerken@gmail.com> wrote:
Note which task is using the entity and only kill it if the last user of
the entity is killed. This should prevent problems when entities are leaked to
child processes.

v2: add missing kernel doc

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/gpu/drm/scheduler/gpu_scheduler.c | 6 +++++-
 include/drm/gpu_scheduler.h               | 2 ++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/scheduler/gpu_scheduler.c b/drivers/gpu/drm/scheduler/gpu_scheduler.c
index 3f2fc5e8242a..f563e4fbb4b6 100644
--- a/drivers/gpu/drm/scheduler/gpu_scheduler.c
+++ b/drivers/gpu/drm/scheduler/gpu_scheduler.c
@@ -275,6 +275,7 @@ static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f,
 long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout)
 {
        struct drm_gpu_scheduler *sched;
+       struct task_struct *last_user;
        long ret = timeout;

        sched = entity->rq->sched;
@@ -295,7 +296,9 @@ long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout)


        /* For killed process disable any more IBs enqueue right now */
-       if ((current->flags & PF_EXITING) && (current->exit_code == SIGKILL))
+       last_user = cmpxchg(&entity->last_user, current->group_leader, NULL);
+       if ((!last_user || last_user == current->group_leader) &&
+           (current->flags & PF_EXITING) && (current->exit_code == SIGKILL))
                drm_sched_entity_set_rq(entity, NULL);

        return ret;
@@ -541,6 +544,7 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job,

        trace_drm_sched_job(sched_job, entity);

+       WRITE_ONCE(entity->last_user, current->group_leader);
        first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);

        /* first job wakes up scheduler */
diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h
index 091b9afcd184..21c648b0b2a1 100644
--- a/include/drm/gpu_scheduler.h
+++ b/include/drm/gpu_scheduler.h
@@ -66,6 +66,7 @@ enum drm_sched_priority {
  * @guilty: points to ctx's guilty.
  * @fini_status: contains the exit status in case the process was signalled.
  * @last_scheduled: points to the finished fence of the last scheduled job.
+ * @last_user: last group leader pushing a job into the entity.
  *
  * Entities will emit jobs in order to their corresponding hardware
  * ring, and the scheduler will alternate between entities based on
@@ -85,6 +86,7 @@ struct drm_sched_entity {
        struct dma_fence_cb             cb;
        atomic_t                        *guilty;
        struct dma_fence                *last_scheduled;
+       struct task_struct              *last_user;
 };

 /**
--
2.14.1



_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

--------------B1D59BD7DF63996E2C24C134-- --===============1530797043== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KZHJpLWRldmVs IG1haWxpbmcgbGlzdApkcmktZGV2ZWxAbGlzdHMuZnJlZWRlc2t0b3Aub3JnCmh0dHBzOi8vbGlz dHMuZnJlZWRlc2t0b3Aub3JnL21haWxtYW4vbGlzdGluZm8vZHJpLWRldmVsCg== --===============1530797043==--