All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH V1] accel/amdxdna: Destroy the context when hwctx restart fails
@ 2026-11-17 16:57 Lizhi Hou
  2026-11-17 17:10 ` sashiko-bot
  2026-11-17 20:54 ` Max Zhen
  0 siblings, 2 replies; 3+ messages in thread
From: Lizhi Hou @ 2026-11-17 16:57 UTC (permalink / raw)
  To: ogabbay, quic_jhugo, dri-devel, mario.limonciello,
	karol.wachowski, max.zhen
  Cc: Lizhi Hou, linux-kernel, sonal.santan

aie2_hwctx_restart() creates the firmware context first, then maps the
host buffers and configures the CUs. When any of the later steps fails,
the error is propagated to the caller but the context created at the
beginning is left behind: hwctx->fw_ctx_id stays valid, the mailbox
channel stays allocated and started, and ndev->hwctx_num is not
decremented. The hardware context is reported as failed yet remains
alive, and because aie2_sched_job_run() only tests hwctx->priv->mbox_chann
the scheduler keeps submitting jobs to a context which has no host buffer
mapped and no CU configured. Neither aie2_hwctx_resume() nor
aie2_sched_job_timedout() looks at the return value, so the context is
never cleaned up and the firmware context slot is leaked until the
hardware context is destroyed.

Tear the context down on the error path.

Fixes: aac243092b70 ("accel/amdxdna: Add command execution")
Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>
---
 drivers/accel/amdxdna/aie2_ctx.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/accel/amdxdna/aie2_ctx.c b/drivers/accel/amdxdna/aie2_ctx.c
index 74d7019268cb..11f5421e497a 100644
--- a/drivers/accel/amdxdna/aie2_ctx.c
+++ b/drivers/accel/amdxdna/aie2_ctx.c
@@ -122,7 +122,7 @@ static int aie2_hwctx_restart(struct amdxdna_dev *xdna, struct amdxdna_hwctx *hw
 				heap->mem.size);
 	if (ret) {
 		XDNA_ERR(xdna, "Map host buf failed, ret %d", ret);
-		goto out;
+		goto destroy;
 	}
 
 	xa_for_each_range(&hwctx->client->dev_heap_xa, heap_id, heap, 1,
@@ -132,16 +132,20 @@ static int aie2_hwctx_restart(struct amdxdna_dev *xdna, struct amdxdna_hwctx *hw
 					heap->mem.size);
 		if (ret) {
 			XDNA_ERR(xdna, "Add heap %ld failed ret %d", heap_id, ret);
-			goto out;
+			goto destroy;
 		}
 	}
 
 	ret = aie2_config_cu(hwctx, NULL);
 	if (ret) {
 		XDNA_ERR(xdna, "Config cu failed, ret %d", ret);
-		goto out;
+		goto destroy;
 	}
 
+	return 0;
+
+destroy:
+	aie2_hwctx_stop(xdna, hwctx, NULL);
 out:
 	XDNA_DBG(xdna, "%s restarted, ret %d", hwctx->name, ret);
 	return ret;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH V1] accel/amdxdna: Destroy the context when hwctx restart fails
  2026-11-17 16:57 [PATCH V1] accel/amdxdna: Destroy the context when hwctx restart fails Lizhi Hou
@ 2026-11-17 17:10 ` sashiko-bot
  2026-11-17 20:54 ` Max Zhen
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-17 17:10 UTC (permalink / raw)
  To: Lizhi Hou, dri-devel

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] accel/amdxdna: hardware use-after-free in aie2_hwctx_fini()
  [1]
--

commit 3c1fe55acb7bc743b1d9dd435d5bd52e64f6bdb4
Author: Lizhi Hou <lizhi.hou@amd.com>

accel/amdxdna: Destroy the context when hwctx restart fails

This commit updates aie2_hwctx_restart() to stop and destroy the hardware
context when mapping host buffers, adding heaps, or configuring CUs fails.

> diff --git a/drivers/accel/amdxdna/aie2_ctx.c b/drivers/accel/amdxdna/aie2_ctx.c
> index 74d7019268cb6..11f5421e497aa 100644
> --- a/drivers/accel/amdxdna/aie2_ctx.c
> +++ b/drivers/accel/amdxdna/aie2_ctx.c
> @@ -122,7 +122,7 @@ static int aie2_hwctx_restart(struct amdxdna_dev *xdna, struct amdxdna_hwctx *hw
>  				heap->mem.size);
>  	if (ret) {
>  		XDNA_ERR(xdna, "Map host buf failed, ret %d", ret);
> -		goto out;
> +		goto destroy;
>  	}
>  
>  	xa_for_each_range(&hwctx->client->dev_heap_xa, heap_id, heap, 1,
> @@ -132,16 +132,20 @@ static int aie2_hwctx_restart(struct amdxdna_dev *xdna, struct amdxdna_hwctx *hw
>  					heap->mem.size);
>  		if (ret) {
>  			XDNA_ERR(xdna, "Add heap %ld failed ret %d", heap_id, ret);
> -			goto out;
> +			goto destroy;
>  		}
>  	}
>  
>  	ret = aie2_config_cu(hwctx, NULL);
>  	if (ret) {
>  		XDNA_ERR(xdna, "Config cu failed, ret %d", ret);
> -		goto out;
> +		goto destroy;
>  	}
>  
> +	return 0;
> +
> +destroy:
> +	aie2_hwctx_stop(xdna, hwctx, NULL);
>  out:
>  	XDNA_DBG(xdna, "%s restarted, ret %d", hwctx->name, ret);
>  	return ret;

[Severity: Critical]
This problem wasn't introduced by this patch, but can restarting the
scheduler in aie2_hwctx_fini() before destroying the entity issue pending
jobs to a destroyed hardware context [1]?

drivers/accel/amdxdna/aie2_ctx.c:aie2_hwctx_fini() {
    ...
	drm_sched_stop(hwctx->priv->sched, NULL);
	aie2_release_resource(hwctx);
	drm_sched_start(hwctx->priv->sched, 0);

	mutex_unlock(&xdna->dev_lock);
	drm_sched_entity_destroy(&hwctx->priv->entity);
    ...
}

drivers/accel/amdxdna/aie2_ctx.c:aie2_sched_job_run() {
    ...
	if (!hwctx->priv->mbox_chann)
		return NULL;
    ...
}

Because drm_sched_start() runs before drm_sched_entity_destroy(), the
scheduler thread pulls remaining pending jobs and passes them to
aie2_sched_job_run() after aie2_release_resource() has freed the hardware
context.  Since aie2_release_resource() clears hwctx->priv->mbox_chann,
aie2_sched_job_run() returns NULL instead of an error pointer such as
ERR_PTR(-ECANCELED), which the DRM scheduler treats as successful job
completion and silently drops the jobs while signaling success to
userspace.

[1] https://sashiko.dev/#/bug/linux-97541525-e808-4942-a13a-978ecaacc230

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117165731.132261-1-lizhi.hou@amd.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH V1] accel/amdxdna: Destroy the context when hwctx restart fails
  2026-11-17 16:57 [PATCH V1] accel/amdxdna: Destroy the context when hwctx restart fails Lizhi Hou
  2026-11-17 17:10 ` sashiko-bot
@ 2026-11-17 20:54 ` Max Zhen
  1 sibling, 0 replies; 3+ messages in thread
From: Max Zhen @ 2026-11-17 20:54 UTC (permalink / raw)
  To: Lizhi Hou, ogabbay, quic_jhugo, dri-devel, mario.limonciello,
	karol.wachowski
  Cc: linux-kernel, sonal.santan



On 11/17/2026 Tue 08:57, Lizhi Hou wrote:
> aie2_hwctx_restart() creates the firmware context first, then maps the
> host buffers and configures the CUs. When any of the later steps fails,
> the error is propagated to the caller but the context created at the
> beginning is left behind: hwctx->fw_ctx_id stays valid, the mailbox
> channel stays allocated and started, and ndev->hwctx_num is not
> decremented. The hardware context is reported as failed yet remains
> alive, and because aie2_sched_job_run() only tests hwctx->priv->mbox_chann
> the scheduler keeps submitting jobs to a context which has no host buffer
> mapped and no CU configured. Neither aie2_hwctx_resume() nor
> aie2_sched_job_timedout() looks at the return value, so the context is
> never cleaned up and the firmware context slot is leaked until the
> hardware context is destroyed.
> 
> Tear the context down on the error path.
> 
> Fixes: aac243092b70 ("accel/amdxdna: Add command execution")
> Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>
Reviewed-by: Max Zhen <max.zhen@amd.com>
> ---
>   drivers/accel/amdxdna/aie2_ctx.c | 10 +++++++---
>   1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/accel/amdxdna/aie2_ctx.c b/drivers/accel/amdxdna/aie2_ctx.c
> index 74d7019268cb..11f5421e497a 100644
> --- a/drivers/accel/amdxdna/aie2_ctx.c
> +++ b/drivers/accel/amdxdna/aie2_ctx.c
> @@ -122,7 +122,7 @@ static int aie2_hwctx_restart(struct amdxdna_dev *xdna, struct amdxdna_hwctx *hw
>   				heap->mem.size);
>   	if (ret) {
>   		XDNA_ERR(xdna, "Map host buf failed, ret %d", ret);
> -		goto out;
> +		goto destroy;
>   	}
>   
>   	xa_for_each_range(&hwctx->client->dev_heap_xa, heap_id, heap, 1,
> @@ -132,16 +132,20 @@ static int aie2_hwctx_restart(struct amdxdna_dev *xdna, struct amdxdna_hwctx *hw
>   					heap->mem.size);
>   		if (ret) {
>   			XDNA_ERR(xdna, "Add heap %ld failed ret %d", heap_id, ret);
> -			goto out;
> +			goto destroy;
>   		}
>   	}
>   
>   	ret = aie2_config_cu(hwctx, NULL);
>   	if (ret) {
>   		XDNA_ERR(xdna, "Config cu failed, ret %d", ret);
> -		goto out;
> +		goto destroy;
>   	}
>   
> +	return 0;
> +
> +destroy:
> +	aie2_hwctx_stop(xdna, hwctx, NULL);
>   out:
>   	XDNA_DBG(xdna, "%s restarted, ret %d", hwctx->name, ret);
>   	return ret;


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-17 20:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 16:57 [PATCH V1] accel/amdxdna: Destroy the context when hwctx restart fails Lizhi Hou
2026-11-17 17:10 ` sashiko-bot
2026-11-17 20:54 ` Max Zhen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.