<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I recently updated amd-staging-drm-next.  Apply whatever makes sense for now and it'll naturally fall out in the next rebase.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Alex<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Grodzovsky, Andrey <Andrey.Grodzovsky@amd.com><br>
<b>Sent:</b> Monday, November 25, 2019 7:09 PM<br>
<b>To:</b> Deng, Emily <Emily.Deng@amd.com><br>
<b>Cc:</b> dri-devel@lists.freedesktop.org <dri-devel@lists.freedesktop.org>; amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org>; Koenig, Christian <Christian.Koenig@amd.com>; steven.price@arm.com <steven.price@arm.com>; Deucher, Alexander <Alexander.Deucher@amd.com><br>
<b>Subject:</b> Re: [PATCH v4] drm/scheduler: Avoid accessing freed bad job.</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Christian asked to submit it to drm-misc instead of our drm-next to avoid later conflicts with Steven's patch which he mentioned in this thread which is not in drm-next yet.<br>
Christian, Alex, once this merged to drm-misc I guess we need to pull all latest changes from there to drm-next so the issue Emily reported can be avoided.<br>
<br>
Andrey<br>
<br>
________________________________________<br>
From: Deng, Emily <Emily.Deng@amd.com><br>
Sent: 25 November 2019 16:44:36<br>
To: Grodzovsky, Andrey<br>
Cc: dri-devel@lists.freedesktop.org; amd-gfx@lists.freedesktop.org; Koenig, Christian; steven.price@arm.com; Grodzovsky, Andrey<br>
Subject: RE: [PATCH v4] drm/scheduler: Avoid accessing freed bad job.<br>
<br>
[AMD Official Use Only - Internal Distribution Only]<br>
<br>
Hi Andrey,<br>
    Seems you didn't submit this patch?<br>
<br>
Best wishes<br>
Emily Deng<br>
<br>
<br>
<br>
>-----Original Message-----<br>
>From: Andrey Grodzovsky <andrey.grodzovsky@amd.com><br>
>Sent: Monday, November 25, 2019 12:51 PM<br>
>Cc: dri-devel@lists.freedesktop.org; amd-gfx@lists.freedesktop.org; Koenig,<br>
>Christian <Christian.Koenig@amd.com>; Deng, Emily<br>
><Emily.Deng@amd.com>; steven.price@arm.com; Grodzovsky, Andrey<br>
><Andrey.Grodzovsky@amd.com><br>
>Subject: [PATCH v4] drm/scheduler: Avoid accessing freed bad job.<br>
><br>
>Problem:<br>
>Due to a race between drm_sched_cleanup_jobs in sched thread and<br>
>drm_sched_job_timedout in timeout work there is a possiblity that bad job<br>
>was already freed while still being accessed from the timeout thread.<br>
><br>
>Fix:<br>
>Instead of just peeking at the bad job in the mirror list remove it from the list<br>
>under lock and then put it back later when we are garanteed no race with<br>
>main sched thread is possible which is after the thread is parked.<br>
><br>
>v2: Lock around processing ring_mirror_list in drm_sched_cleanup_jobs.<br>
><br>
>v3: Rebase on top of drm-misc-next. v2 is not needed anymore as<br>
>drm_sched_get_cleanup_job already has a lock there.<br>
><br>
>v4: Fix comments to relfect latest code in drm-misc.<br>
><br>
>Signed-off-by: Andrey Grodzovsky <andrey.grodzovsky@amd.com><br>
>Reviewed-by: Christian König <christian.koenig@amd.com><br>
>Tested-by: Emily Deng <Emily.Deng@amd.com><br>
>---<br>
> drivers/gpu/drm/scheduler/sched_main.c | 27<br>
>+++++++++++++++++++++++++++<br>
> 1 file changed, 27 insertions(+)<br>
><br>
>diff --git a/drivers/gpu/drm/scheduler/sched_main.c<br>
>b/drivers/gpu/drm/scheduler/sched_main.c<br>
>index 6774955..1bf9c40 100644<br>
>--- a/drivers/gpu/drm/scheduler/sched_main.c<br>
>+++ b/drivers/gpu/drm/scheduler/sched_main.c<br>
>@@ -284,10 +284,21 @@ static void drm_sched_job_timedout(struct<br>
>work_struct *work)<br>
>       unsigned long flags;<br>
><br>
>       sched = container_of(work, struct drm_gpu_scheduler,<br>
>work_tdr.work);<br>
>+<br>
>+      /* Protects against concurrent deletion in<br>
>drm_sched_get_cleanup_job */<br>
>+      spin_lock_irqsave(&sched->job_list_lock, flags);<br>
>       job = list_first_entry_or_null(&sched->ring_mirror_list,<br>
>                                      struct drm_sched_job, node);<br>
><br>
>       if (job) {<br>
>+              /*<br>
>+               * Remove the bad job so it cannot be freed by concurrent<br>
>+               * drm_sched_cleanup_jobs. It will be reinserted back after<br>
>sched->thread<br>
>+               * is parked at which point it's safe.<br>
>+               */<br>
>+              list_del_init(&job->node);<br>
>+              spin_unlock_irqrestore(&sched->job_list_lock, flags);<br>
>+<br>
>               job->sched->ops->timedout_job(job);<br>
><br>
>               /*<br>
>@@ -298,6 +309,8 @@ static void drm_sched_job_timedout(struct<br>
>work_struct *work)<br>
>                       job->sched->ops->free_job(job);<br>
>                       sched->free_guilty = false;<br>
>               }<br>
>+      } else {<br>
>+              spin_unlock_irqrestore(&sched->job_list_lock, flags);<br>
>       }<br>
><br>
>       spin_lock_irqsave(&sched->job_list_lock, flags); @@ -370,6 +383,20<br>
>@@ void drm_sched_stop(struct drm_gpu_scheduler *sched, struct<br>
>drm_sched_job *bad)<br>
>       kthread_park(sched->thread);<br>
><br>
>       /*<br>
>+       * Reinsert back the bad job here - now it's safe as<br>
>+       * drm_sched_get_cleanup_job cannot race against us and release the<br>
>+       * bad job at this point - we parked (waited for) any in progress<br>
>+       * (earlier) cleanups and drm_sched_get_cleanup_job will not be<br>
>called<br>
>+       * now until the scheduler thread is unparked.<br>
>+       */<br>
>+      if (bad && bad->sched == sched)<br>
>+              /*<br>
>+               * Add at the head of the queue to reflect it was the earliest<br>
>+               * job extracted.<br>
>+               */<br>
>+              list_add(&bad->node, &sched->ring_mirror_list);<br>
>+<br>
>+      /*<br>
>        * Iterate the job list from later to  earlier one and either deactive<br>
>        * their HW callbacks or remove them from mirror list if they already<br>
>        * signaled.<br>
>--<br>
>2.7.4<br>
</div>
</span></font></div>
</body>
</html>