From e8886a4d425b5cb915acfb7da165e42b917b7626 Mon Sep 17 00:00:00 2001
From: Andrey Grodzovsky <andrey.grodzovsky@amd.com>
Date: Fri, 18 Jan 2019 14:06:25 -0500
Subject: drm/sched: DRAFT: drm/sched: Skip HW reset if guilty job is signaled.

Also reject other tdr jobs while current in progress as it is going
to take care of all the schedulres for a device and all devices for
XGMI hive.

Signed-off-by: Andrey Grodzovsky <andrey.grodzovsky@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 65 +++++++++++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.h    |  1 +
 drivers/gpu/drm/etnaviv/etnaviv_sched.c    |  2 +-
 drivers/gpu/drm/scheduler/sched_main.c     | 47 ++++++++++++---------
 drivers/gpu/drm/v3d/v3d_sched.c            |  3 +-
 include/drm/gpu_scheduler.h                |  9 ++++-
 6 files changed, 79 insertions(+), 48 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
index e2f578a..db2b984 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
@@ -3313,7 +3313,7 @@ static int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
 		if (!ring || !ring->sched.thread)
 			continue;
 
-		drm_sched_stop(&ring->sched, job ? &job->base : NULL);
+		drm_sched_stop(&ring->sched);
 
 		/* after all hw jobs are reset, hw fence is meaningless, so force_completion */
 		amdgpu_fence_driver_force_completion(ring);
@@ -3457,7 +3457,7 @@ static int amdgpu_do_asic_reset(struct amdgpu_hive_info *hive,
 }
 
 static void amdgpu_device_post_asic_reset(struct amdgpu_device *adev,
-					  struct amdgpu_job *job)
+					  bool resubmit)
 {
 	int i;
 
@@ -3467,7 +3467,7 @@ static void amdgpu_device_post_asic_reset(struct amdgpu_device *adev,
 		if (!ring || !ring->sched.thread)
 			continue;
 
-		if (!adev->asic_reset_res)
+		if (!adev->asic_reset_res && resubmit)
 			drm_sched_resubmit_jobs(&ring->sched);
 
 		drm_sched_start(&ring->sched, !adev->asic_reset_res);
@@ -3480,14 +3480,22 @@ static void amdgpu_device_post_asic_reset(struct amdgpu_device *adev,
 	adev->asic_reset_res = 0;
 }
 
-static void amdgpu_device_lock_adev(struct amdgpu_device *adev)
+static bool amdgpu_device_lock_adev(struct amdgpu_device *adev, bool trylock_reset)
 {
-	mutex_lock(&adev->lock_reset);
+	if (trylock_reset) {
+		if (!mutex_trylock(&adev->lock_reset))
+			return false;
+	}
+	else
+		mutex_lock(&adev->lock_reset);
+
 	atomic_inc(&adev->gpu_reset_counter);
 	adev->in_gpu_reset = 1;
 	/* Block kfd: SRIOV would do it separately */
 	if (!amdgpu_sriov_vf(adev))
                 amdgpu_amdkfd_pre_reset(adev);
+
+	return true;
 }
 
 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
@@ -3520,23 +3528,29 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 	bool need_full_reset = false;
 	struct amdgpu_device *tmp_adev = NULL;
 	struct list_head device_list, *device_list_handle =  NULL;
+	bool job_finished, xgmi_topology_present;
 
 	INIT_LIST_HEAD(&device_list);
 
 	dev_info(adev->dev, "GPU reset begin!\n");
 
+	hive = amdgpu_get_xgmi_hive(adev, 0);
+	xgmi_topology_present = hive && adev->gmc.xgmi.num_physical_nodes > 1;
+
 	/*
-	 * In case of XGMI hive disallow concurrent resets to be triggered
-	 * by different nodes. No point also since the one node already executing
-	 * reset will also reset all the other nodes in the hive.
+	 * Here and bellow we trylock to avoid chain of resets executing from
+	 * either trigger by jobs on different adevs in XGMI hive or jobs on
+	 * different schedulers for same device while this tdr is running.
+	 * We always reset all schedulers for device and all devices for XGMI
+	 * hive so that should take care of them too.
 	 */
-	hive = amdgpu_get_xgmi_hive(adev, 0);
-	if (hive && adev->gmc.xgmi.num_physical_nodes > 1 &&
-	    !mutex_trylock(&hive->reset_lock))
+	if (xgmi_topology_present && !mutex_trylock(&hive->reset_lock))
 		return 0;
 
 	/* Start with adev pre asic reset first for soft reset check.*/
-	amdgpu_device_lock_adev(adev);
+	if (!amdgpu_device_lock_adev(adev, !xgmi_topology_present))
+			return 0;
+
 	r = amdgpu_device_pre_asic_reset(adev,
 					 job,
 					 &need_full_reset);
@@ -3571,7 +3585,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 		if (tmp_adev == adev)
 			continue;
 
-		amdgpu_device_lock_adev(tmp_adev);
+		amdgpu_device_lock_adev(tmp_adev, false);
 		r = amdgpu_device_pre_asic_reset(tmp_adev,
 						 NULL,
 						 &need_full_reset);
@@ -3583,21 +3597,26 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 		}
 	}
 
+
+
 	/* Actual ASIC resets if needed.*/
 	/* TODO Implement XGMI hive reset logic for SRIOV */
-	if (amdgpu_sriov_vf(adev)) {
-		r = amdgpu_device_reset_sriov(adev, job ? false : true);
-		if (r)
-			adev->asic_reset_res = r;
-	} else {
-		r  = amdgpu_do_asic_reset(hive, device_list_handle, &need_full_reset);
-		if (r && r == -EAGAIN)
-			goto retry;
+
+	if (!(job_finished = drm_sched_job_finished(&job->base))) {
+		if (amdgpu_sriov_vf(adev)) {
+			r = amdgpu_device_reset_sriov(adev, job ? false : true);
+			if (r)
+				adev->asic_reset_res = r;
+		} else {
+			r  = amdgpu_do_asic_reset(hive, device_list_handle, &need_full_reset);
+			if (r && r == -EAGAIN)
+				goto retry;
+		}
 	}
 
 	/* Post ASIC reset for all devs .*/
 	list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
-		amdgpu_device_post_asic_reset(tmp_adev, tmp_adev == adev ? job : NULL);
+		amdgpu_device_post_asic_reset(tmp_adev, job_finished);
 
 		if (r) {
 			/* bad news, how to tell it to userspace ? */
@@ -3610,7 +3629,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 		amdgpu_device_unlock_adev(tmp_adev);
 	}
 
-	if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
+	if (xgmi_topology_present)
 		mutex_unlock(&hive->reset_lock);
 
 	if (r)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_job.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_job.h
index e1b46a6..f4195b9 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_job.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_job.h
@@ -73,4 +73,5 @@ int amdgpu_job_submit(struct amdgpu_job *job, struct drm_sched_entity *entity,
 		      void *owner, struct dma_fence **f);
 int amdgpu_job_submit_direct(struct amdgpu_job *job, struct amdgpu_ring *ring,
 			     struct dma_fence **fence);
+
 #endif
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_sched.c b/drivers/gpu/drm/etnaviv/etnaviv_sched.c
index 6f1268f..67ae266 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_sched.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_sched.c
@@ -109,7 +109,7 @@ static void etnaviv_sched_timedout_job(struct drm_sched_job *sched_job)
 	}
 
 	/* block scheduler */
-	drm_sched_stop(&gpu->sched, sched_job);
+	drm_sched_stop(&gpu->sched);
 
 	if(sched_job)
 		drm_sched_increase_karma(sched_job);
diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c
index 16c6363..4617fd9 100644
--- a/drivers/gpu/drm/scheduler/sched_main.c
+++ b/drivers/gpu/drm/scheduler/sched_main.c
@@ -189,11 +189,13 @@ EXPORT_SYMBOL(drm_sched_dependency_optimized);
  *
  * Start the timeout for the given scheduler.
  */
-static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched)
+static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched, long delay)
 {
+	long timeout = delay >= 0 ? delay : sched->timeout;
+
 	if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
 	    !list_empty(&sched->ring_mirror_list))
-		schedule_delayed_work(&sched->work_tdr, sched->timeout);
+		mod_delayed_work(system_wq, &sched->work_tdr, timeout);
 }
 
 /**
@@ -275,18 +277,16 @@ static void drm_sched_job_finish(struct work_struct *work)
 	unsigned long flags;
 
 	/*
-	 * Canceling the timeout without removing our job from the ring mirror
-	 * list is safe, as we will only end up in this worker if our jobs
-	 * finished fence has been signaled. So even if some another worker
-	 * manages to find this job as the next job in the list, the fence
-	 * signaled check below will prevent the timeout to be restarted.
+	 * Avoid restarting timer if you are racing against tdr in progress
+	 * as tdr tasks will stop and restart the timer itself otherwise you
+	 * can end up rearming timer which the tdr just stopped.
 	 */
-	cancel_delayed_work_sync(&sched->work_tdr);
-
-	spin_lock_irqsave(&sched->job_list_lock, flags);
-	/* queue TDR for next job */
-	drm_sched_start_timeout(sched);
-	spin_unlock_irqrestore(&sched->job_list_lock, flags);
+	if (!flush_delayed_work(&sched->work_tdr)) {
+		spin_lock_irqsave(&sched->job_list_lock, flags);
+		/* queue TDR for next job */
+		drm_sched_start_timeout(sched, -1);
+		spin_unlock_irqrestore(&sched->job_list_lock, flags);
+	}
 
 	sched->ops->free_job(s_job);
 }
@@ -298,7 +298,7 @@ static void drm_sched_job_begin(struct drm_sched_job *s_job)
 
 	spin_lock_irqsave(&sched->job_list_lock, flags);
 	list_add_tail(&s_job->node, &sched->ring_mirror_list);
-	drm_sched_start_timeout(sched);
+	drm_sched_start_timeout(sched, -1);
 	spin_unlock_irqrestore(&sched->job_list_lock, flags);
 }
 
@@ -307,6 +307,7 @@ static void drm_sched_job_timedout(struct work_struct *work)
 	struct drm_gpu_scheduler *sched;
 	struct drm_sched_job *job;
 	unsigned long flags;
+	long delay = -1;
 
 	sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work);
 	job = list_first_entry_or_null(&sched->ring_mirror_list,
@@ -316,7 +317,9 @@ static void drm_sched_job_timedout(struct work_struct *work)
 		job->sched->ops->timedout_job(job);
 
 	spin_lock_irqsave(&sched->job_list_lock, flags);
-	drm_sched_start_timeout(sched);
+	if (job && job->s_fence->parent && !drm_sched_job_finished(job))
+		delay = msecs_to_jiffies(2);
+	drm_sched_start_timeout(sched, delay);
 	spin_unlock_irqrestore(&sched->job_list_lock, flags);
 }
 
@@ -372,12 +375,11 @@ EXPORT_SYMBOL(drm_sched_increase_karma);
  * @bad: bad scheduler job
  *
  */
-void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
+void drm_sched_stop(struct drm_gpu_scheduler *sched)
 {
 	struct drm_sched_job *s_job;
 	unsigned long flags;
 	struct dma_fence *last_fence =  NULL;
-	int r;
 
 	kthread_park(sched->thread);
 
@@ -387,14 +389,12 @@ void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
 	 * also all the previous jobs that were in flight also already singaled
 	 * and removed from the list.
 	 */
-retry_wait:
 	spin_lock_irqsave(&sched->job_list_lock, flags);
 	list_for_each_entry_reverse(s_job, &sched->ring_mirror_list, node) {
 		if (s_job->s_fence->parent &&
 		    dma_fence_remove_callback(s_job->s_fence->parent,
 					      &s_job->cb)) {
 			dma_fence_put(s_job->s_fence->parent);
-			s_job->s_fence->parent = NULL;
 			atomic_dec(&sched->hw_rq_count);
 		} else {
 			 last_fence = dma_fence_get(&s_job->s_fence->finished);
@@ -407,6 +407,13 @@ void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
 		dma_fence_wait(last_fence, false);
 		dma_fence_put(last_fence);
 	}
+
+	/*
+	 * We parked the sched thread so no new job can rearm the timer now,
+	 * was also are flushing work_tdr from drm_sched_job_finish so no job
+	 * finishing work in progress can rearm the timer.
+	 */
+	cancel_delayed_work(&sched->work_tdr);
 }
 
 EXPORT_SYMBOL(drm_sched_stop);
@@ -447,7 +454,7 @@ void drm_sched_start(struct drm_gpu_scheduler *sched, bool full_recovery)
 			drm_sched_process_job(NULL, &s_job->cb);
 	}
 
-	drm_sched_start_timeout(sched);
+	drm_sched_start_timeout(sched, -1);
 
 unpark:
 	kthread_unpark(sched->thread);
diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c
index f76d9ed..9424388 100644
--- a/drivers/gpu/drm/v3d/v3d_sched.c
+++ b/drivers/gpu/drm/v3d/v3d_sched.c
@@ -178,8 +178,7 @@ v3d_job_timedout(struct drm_sched_job *sched_job)
 	for (q = 0; q < V3D_MAX_QUEUES; q++) {
 		struct drm_gpu_scheduler *sched = &v3d->queue[q].sched;
 
-		drm_sched_stop(sched, (sched_job->sched == sched ?
-					       sched_job : NULL));
+		drm_sched_stop(sched);
 
 		if(sched_job)
 			drm_sched_increase_karma(sched_job);
diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h
index 62c2352..24077f6 100644
--- a/include/drm/gpu_scheduler.h
+++ b/include/drm/gpu_scheduler.h
@@ -203,6 +203,12 @@ static inline bool drm_sched_invalidate_job(struct drm_sched_job *s_job,
 	return (s_job && atomic_inc_return(&s_job->karma) > threshold);
 }
 
+static inline bool drm_sched_job_finished(struct drm_sched_job *s_job)
+{
+	WARN_ON(!s_job->s_fence->parent);
+	return dma_fence_is_signaled(s_job->s_fence->parent);
+}
+
 /**
  * struct drm_sched_backend_ops
  *
@@ -296,8 +302,7 @@ int drm_sched_job_init(struct drm_sched_job *job,
 		       void *owner);
 void drm_sched_job_cleanup(struct drm_sched_job *job);
 void drm_sched_wakeup(struct drm_gpu_scheduler *sched);
-void drm_sched_stop(struct drm_gpu_scheduler *sched,
-		    struct drm_sched_job *job);
+void drm_sched_stop(struct drm_gpu_scheduler *sched);
 void drm_sched_start(struct drm_gpu_scheduler *sched, bool full_recovery);
 void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched);
 void drm_sched_increase_karma(struct drm_sched_job *bad);
-- 
2.7.4

