Skip to contentSkip to conversation

accel/amdxdna: Check the fence error after waiting for a driver command

PATCH New Lizhi Hou 2 participants 3 messages Nov 17

Subsystems (2)
lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

PATCH 1/2 accel/amdxdna: Check the fence error after waiting for a driver command

aie2_cmd_wait() discarded the result of the wait, and both callers, aie2_hwctx_cfg_debug_bo() and aie2_hwctx_sync_debug_bo(), decided success solely from cmd->result.

cmd->result is only written by aie2_sched_drvcmd_resp_handler(), which runs when firmware responds. If the job fails before it reaches firmware, for example aie2_sync_bo() or aie2_config_debug_bo() fail to send the mailbox message, the error is recorded on the fence and no response handler runs. cmd is zero allocated, so cmd->result stays 0 and the caller treats the failed command as successful. aie2_hwctx_cfg_debug_bo() then goes on to update abo->assigned_hwctx.

Return the fence error from aie2_cmd_wait() and check it in both callers. This also covers a fence errored after the command was posted, and the case where aie2_cmd_get_out_fence() finds no fence at all, which previously fell through to the same false success.

Fixes 7ea046838021 ("accel/amdxdna: Support firmware debug buffer")
---
 drivers/accel/amdxdna/aie2_ctx.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +24 -4
index 11f5421e497a..0a1914f0f1c0 100644
@@ -984,20 +984,30 @@ static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size
 	return ret;
 }
 
-static void aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq)
+static int aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq)
 {
 	struct dma_fence *out_fence = aie2_cmd_get_out_fence(hwctx, seq);
 	struct amdxdna_dev *xdna = hwctx->client->xdna;
+	int ret;
 
 	if (!out_fence) {
 		XDNA_ERR(xdna, "Failed to get fence");
-		return;
+		return -EINVAL;
 	}
 
 	mutex_unlock(&xdna->dev_lock);
 	dma_fence_wait_timeout(out_fence, false, MAX_SCHEDULE_TIMEOUT);
 	mutex_lock(&xdna->dev_lock);
+	/*
+	 * The job may never reach firmware, for example the submission to
+	 * the mailbox failed. In that case there is no response and
+	 * cmd->result stays untouched, so the fence error is the only way
+	 * to tell the command did not complete.
+	 */
+	ret = out_fence->error;
 	dma_fence_put(out_fence);
+
+	return ret;
 }
 
 static int aie2_hwctx_cfg_debug_bo(struct amdxdna_hwctx *hwctx, u32 bo_hdl,
@@ -1044,7 +1054,12 @@ static int aie2_hwctx_cfg_debug_bo(struct amdxdna_hwctx *hwctx, u32 bo_hdl,
 		goto put_cmd;
 	}
 
-	aie2_cmd_wait(hwctx, seq);
+	ret = aie2_cmd_wait(hwctx, seq);
+	if (ret) {
+		XDNA_ERR(xdna, "Command failed, ret %d", ret);
+		goto put_cmd;
+	}
+
 	if (cmd->result) {
 		XDNA_ERR(xdna, "Response failure 0x%x", cmd->result);
 		ret = -EINVAL;
@@ -1104,7 +1119,12 @@ int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl)
 		goto put_cmd;
 	}
 
-	aie2_cmd_wait(hwctx, seq);
+	ret = aie2_cmd_wait(hwctx, seq);
+	if (ret) {
+		XDNA_ERR(xdna, "Command failed, ret %d", ret);
+		goto put_cmd;
+	}
+
 	if (cmd->result) {
 		XDNA_ERR(xdna, "Response failure 0x%x", cmd->result);
 		ret = -EINVAL;
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 2/2 accel/amdxdna: Abort jobs when the firmware context is gone

aie2_sched_job_run() returned NULL when the firmware context is gone (mbox_chann pointer is NULL). The DRM scheduler treats a NULL return as a job that completed without an error.

Return ERR_PTR(-ECANCELED) so the error is recorded on the fence, and mark the command BO aborted.

Fixes 6b13cb8f48a4 ("accel/amdxdna: Fix runtime suspend deadlock when there is pending job")
---
 drivers/accel/amdxdna/aie2_ctx.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +12 -2
index 0a1914f0f1c0..3b898c05513e 100644
@@ -404,8 +404,18 @@ aie2_sched_job_run(struct drm_sched_job *sched_job)
 	trace_xdna_job(sched_job, hwctx->name, "job run",
 		       job->seq, job->drv_cmd ? job->drv_cmd->opcode : DEFAULT_IO);
 
-	if (!hwctx->priv->mbox_chann)
-		return NULL;
+	/*
+	 * The firmware context is gone, but the scheduler keeps draining the
+	 * entity, for example from drm_sched_entity_destroy(). Returning NULL
+	 * would make the DRM scheduler signal the job fence without an error
+	 * and user space would see the command as successfully completed.
+	 * Abort the job instead. Driver internal commands have no command BO.
+	 */
+	if (!hwctx->priv->mbox_chann) {
+		if (cmd_abo)
+			amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_ABORT);
+		return ERR_PTR(-ECANCELED);
+	}
 
 	if (!mmget_not_zero(job->mm))
 		return ERR_PTR(-ESRCH);
Signature · 1 line
2.34.1
#2 lore raw
In reply to Lizhi Hou
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] accel/amdxdna: false -EINVAL in aie2_cmd_wait() when fence is garbage-collected --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] accel/amdxdna: false -EINVAL in aie2_cmd_wait() when fence is garbage-collected
--

commit c14f81e589ab4e7ef12c7146189062d34baca05a
Author: Lizhi Hou <[email protected]>

accel/amdxdna: Check the fence error after waiting for a driver command

This commit returns the out_fence error from aie2_cmd_wait() and checks that return value in aie2_hwctx_cfg_debug_bo() and aie2_hwctx_sync_debug_bo() so driver commands that fail before reaching firmware are not treated as successful.

Quoted 10 lines
drivers/accel/amdxdna/aie2_ctx.c +1 -1
index 74d7019268cb6..87bd6117e943f 100644
@@ -980,20 +980,30 @@ static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size
 	return ret;
 }
 
-static void aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq)
+static int aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq)

drivers/accel/amdxdna/aie2_ctx.c static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size · line 992

Quoted 10 lines
drivers/accel/amdxdna/aie2_ctx.c +2 -1
 {
 	struct dma_fence *out_fence = aie2_cmd_get_out_fence(hwctx, seq);
 	struct amdxdna_dev *xdna = hwctx->client->xdna;
+	int ret;
 
 	if (!out_fence) {
 		XDNA_ERR(xdna, "Failed to get fence");
-		return;
+		return -EINVAL;
 	}

[Severity: Medium]
Can aie2_cmd_wait() return -EINVAL for a command that already succeeded if a concurrent submission on the same hwctx advances hwctx->priv->syncobj before aie2_cmd_get_out_fence() runs?

drivers/accel/amdxdna/aie2_ctx.c:aie2_cmd_get_out_fence() {
    ...
	ret = dma_fence_chain_find_seqno(&fence,  seq);
	if (ret)
		goto out;

	out_fence = dma_fence_get(dma_fence_chain_contained(fence));
    ...
}

aie2_cmd_submit() drops hwctx->priv->io_lock after adding the timeline point to hwctx->priv->syncobj, and regular command submissions via amdxdna_drm_submit_cmd_ioctl() do not hold xdna->dev_lock:

CPU0 CPU1
aie2_hwctx_cfg_debug_bo()
amdxdna_cmd_submit(..., &seq)

    ...
    drm_syncobj_add_point(..., seq)
    mutex_unlock(&hwctx->priv->io_lock)
  <job at seq finishes and signals>
                                      amdxdna_drm_submit_cmd_ioctl()
                                        ...
                                        drm_syncobj_add_point(..., seq + 1)
  aie2_cmd_wait(hwctx, seq)
    aie2_cmd_get_out_fence(hwctx, seq)
      dma_fence_chain_find_seqno(&fence, seq)
        dma_fence_chain_walk()
          /* seq is signaled: sets fence = NULL, returns 0 */
    if (!out_fence)
      return -EINVAL;  <-- false failure

When aie2_hwctx_cfg_debug_bo() hits this race, it jumps to put_cmd without updating abo->assigned_hwctx even though firmware already executed ATTACH_DEBUG_BO or DETACH_DEBUG_BO, leaving abo->assigned_hwctx out of sync with firmware.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1