Skip to contentSkip to conversation

accel/amdxdna: Stop job scheduling across aie2_release_resource()

PATCH In review Lizhi Hou 2 participants 3 messages Jan 30

Subsystems (2)
lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

PATCH 1/1 accel/amdxdna: Stop job scheduling across aie2_release_resource()

Running jobs on a hardware context while it is in the process of releasing resources can lead to use-after-free and crashes.

Fix this by stopping job scheduling before calling aie2_release_resource() and restarting it after the release completes. Additionally, aie2_sched_job_run() now checks whether the hardware context is still active.

Fixes 4fd6ca90fc7f ("accel/amdxdna: Refactor hardware context destroy routine")
---
 drivers/accel/amdxdna/aie2_ctx.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/accel/amdxdna/aie2_ctx.c +6 -0
index 5511ab2ef242..c4a58c00e442 100644
@@ -315,6 +315,9 @@ aie2_sched_job_run(struct drm_sched_job *sched_job)
 	struct dma_fence *fence;
 	int ret;
 
+	if (hwctx->status != HWCTX_STAT_READY)
+		return NULL;
+
 	if (!mmget_not_zero(job->mm))
 		return ERR_PTR(-ESRCH);
 
@@ -705,7 +708,10 @@ void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx)
 	aie2_hwctx_wait_for_idle(hwctx);
 
 	/* Request fw to destroy hwctx and cancel the rest pending requests */
+	drm_sched_stop(&hwctx->priv->sched, NULL);
 	aie2_release_resource(hwctx);
+	hwctx->status = HWCTX_STAT_STOP;
+	drm_sched_start(&hwctx->priv->sched, 0);
 
 	mutex_unlock(&xdna->dev_lock);
 	drm_sched_entity_destroy(&hwctx->priv->entity);
Signature · 1 line
2.34.1
Reviewed-by Mario Limonciello (AMD) on v1 1/1
#1 lore raw
Quoted 10 lines · Lizhi Hou

Running jobs on a hardware context while it is in the process of releasing resources can lead to use-after-free and crashes.

Fix this by stopping job scheduling before calling aie2_release_resource() and restarting it after the release completes. Additionally, aie2_sched_job_run() now checks whether the hardware context is still active.

Fixes 4fd6ca90fc7f ("accel/amdxdna: Refactor hardware context destroy routine")
Quoted 29 lines
---
  drivers/accel/amdxdna/aie2_ctx.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/accel/amdxdna/aie2_ctx.c +6 -0
index 5511ab2ef242..c4a58c00e442 100644
@@ -315,6 +315,9 @@ aie2_sched_job_run(struct drm_sched_job *sched_job)
  	struct dma_fence *fence;
  	int ret;
 
+	if (hwctx->status != HWCTX_STAT_READY)
+		return NULL;
+
  	if (!mmget_not_zero(job->mm))
  		return ERR_PTR(-ESRCH);
 
@@ -705,7 +708,10 @@ void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx)
  	aie2_hwctx_wait_for_idle(hwctx);
 
  	/* Request fw to destroy hwctx and cancel the rest pending requests */
+	drm_sched_stop(&hwctx->priv->sched, NULL);
  	aie2_release_resource(hwctx);
+	hwctx->status = HWCTX_STAT_STOP;
+	drm_sched_start(&hwctx->priv->sched, 0);
 
  	mutex_unlock(&xdna->dev_lock);
  	drm_sched_entity_destroy(&hwctx->priv->entity);
author maintainer Applied
#2 lore raw

Applied to drm-misc-next-fixes

Quoted 38 lines · Mario Limonciello
Quoted 10 lines · Lizhi Hou

Running jobs on a hardware context while it is in the process of releasing resources can lead to use-after-free and crashes.

Fix this by stopping job scheduling before calling aie2_release_resource() and restarting it after the release completes. Additionally, aie2_sched_job_run() now checks whether the hardware context is still active.

Fixes 4fd6ca90fc7f ("accel/amdxdna: Refactor hardware context destroy routine")
Quoted 25 lines

---
  drivers/accel/amdxdna/aie2_ctx.c | 6 ++++++

  1 file changed, 6 insertions(+)
drivers/accel/amdxdna/aie2_ctx.c +2 -0
index 5511ab2ef242..c4a58c00e442 100644
@@ -315,6 +315,9 @@ aie2_sched_job_run(struct drm_sched_job *sched_job)
       struct dma_fence *fence;
       int ret;
   +    if (hwctx->status != HWCTX_STAT_READY)
+        return NULL;
+
       if (!mmget_not_zero(job->mm))
           return ERR_PTR(-ESRCH);
   @@ -705,7 +708,10 @@ void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx)

      aie2_hwctx_wait_for_idle(hwctx);
        /* Request fw to destroy hwctx and cancel the rest pending requests */ +    drm_sched_stop(&hwctx->priv->sched, NULL); aie2_release_resource(hwctx);
+    hwctx->status = HWCTX_STAT_STOP;
+    drm_sched_start(&hwctx->priv->sched, 0); mutex_unlock(&xdna->dev_lock); drm_sched_entity_destroy(&hwctx->priv->entity);