public inbox for drm-ai-reviews@public-inbox.freedesktop.org
 help / color / mirror / Atom feed
From: Mario Limonciello <mario.limonciello@amd.com>
To: <mario.limonciello@amd.com>, <lizhi.hou@amd.com>,
	<mamin506@gmail.com>, <ogabbay@kernel.org>, <superm1@kernel.org>
Cc: <dri-devel@lists.freedesktop.org>
Subject: [PATCH 1/2] accel/amdxdna: Fix NULL pointer dereference in mailbox channel cleanup
Date: Tue, 10 Feb 2026 10:42:50 -0600	[thread overview]
Message-ID: <20260210164521.1094274-2-mario.limonciello@amd.com> (raw)
In-Reply-To: <20260210164521.1094274-1-mario.limonciello@amd.com>

aie2_destroy_context() is called during various cleanup paths, including
when context creation fails partially. If xdna_mailbox_create_channel()
fails during aie2_create_context(), the hwctx->priv->mbox_chann pointer
remains NULL. When cleanup occurs (e.g., during process termination via
amdxdna_hwctx_remove_all), aie2_destroy_context() is invoked and attempts
to stop and destroy the NULL mailbox channel, leading to a NULL pointer
dereference.

The issue was observed in the following call path:
  amdxdna_drm_close
    amdxdna_hwctx_remove_all
      aie2_hwctx_fini
        aie2_release_resource
          aie2_destroy_context
            xdna_mailbox_stop_channel <- NULL dereference

Add NULL checks in aie2_destroy_context() before calling mailbox channel
operations. Also add defensive NULL checks in aie2_hw_stop() for both
mgmt_chann and mbox to prevent similar issues during device shutdown.

Fixes: 97f27573837e ("accel/amdxdna: Fix potential NULL pointer dereference in context cleanup")
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 drivers/accel/amdxdna/aie2_message.c | 14 +++++++++-----
 drivers/accel/amdxdna/aie2_pci.c     | 14 +++++++++-----
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/drivers/accel/amdxdna/aie2_message.c b/drivers/accel/amdxdna/aie2_message.c
index 7d7dcfeaf7942..77e3cdf18658b 100644
--- a/drivers/accel/amdxdna/aie2_message.c
+++ b/drivers/accel/amdxdna/aie2_message.c
@@ -318,11 +318,15 @@ int aie2_destroy_context(struct amdxdna_dev_hdl *ndev, struct amdxdna_hwctx *hwc
 	struct amdxdna_dev *xdna = ndev->xdna;
 	int ret;
 
-	xdna_mailbox_stop_channel(hwctx->priv->mbox_chann);
-	ret = aie2_destroy_context_req(ndev, hwctx->fw_ctx_id);
-	xdna_mailbox_destroy_channel(hwctx->priv->mbox_chann);
-	XDNA_DBG(xdna, "Destroyed fw ctx %d", hwctx->fw_ctx_id);
-	hwctx->priv->mbox_chann = NULL;
+	if (hwctx->priv->mbox_chann) {
+		xdna_mailbox_stop_channel(hwctx->priv->mbox_chann);
+		ret = aie2_destroy_context_req(ndev, hwctx->fw_ctx_id);
+		xdna_mailbox_destroy_channel(hwctx->priv->mbox_chann);
+		XDNA_DBG(xdna, "Destroyed fw ctx %d", hwctx->fw_ctx_id);
+		hwctx->priv->mbox_chann = NULL;
+	} else {
+		ret = aie2_destroy_context_req(ndev, hwctx->fw_ctx_id);
+	}
 	hwctx->fw_ctx_id = -1;
 	ndev->hwctx_num--;
 
diff --git a/drivers/accel/amdxdna/aie2_pci.c b/drivers/accel/amdxdna/aie2_pci.c
index f70ccf0f3c019..9c2572706bf53 100644
--- a/drivers/accel/amdxdna/aie2_pci.c
+++ b/drivers/accel/amdxdna/aie2_pci.c
@@ -324,11 +324,15 @@ static void aie2_hw_stop(struct amdxdna_dev *xdna)
 	}
 
 	aie2_mgmt_fw_fini(ndev);
-	xdna_mailbox_stop_channel(ndev->mgmt_chann);
-	xdna_mailbox_destroy_channel(ndev->mgmt_chann);
-	ndev->mgmt_chann = NULL;
-	drmm_kfree(&xdna->ddev, ndev->mbox);
-	ndev->mbox = NULL;
+	if (ndev->mgmt_chann) {
+		xdna_mailbox_stop_channel(ndev->mgmt_chann);
+		xdna_mailbox_destroy_channel(ndev->mgmt_chann);
+		ndev->mgmt_chann = NULL;
+	}
+	if (ndev->mbox) {
+		drmm_kfree(&xdna->ddev, ndev->mbox);
+		ndev->mbox = NULL;
+	}
 	aie2_psp_stop(ndev->psp_hdl);
 	aie2_smu_fini(ndev);
 	aie2_error_async_events_free(ndev);
-- 
2.53.0


  reply	other threads:[~2026-02-10 16:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-10 16:42 [PATCH 0/2] amdxdna: fixes for closing a process Mario Limonciello
2026-02-10 16:42 ` Mario Limonciello [this message]
2026-02-10 17:17   ` [PATCH 1/2] accel/amdxdna: Fix NULL pointer dereference in mailbox channel cleanup Lizhi Hou
2026-02-11  6:21   ` Claude review: " Claude Code Review Bot
2026-02-10 16:42 ` [PATCH 2/2] accel/amdxdna: Reduce log noise during process termination Mario Limonciello
2026-02-10 17:20   ` Lizhi Hou
2026-02-11  6:21   ` Claude review: " Claude Code Review Bot
2026-02-11  6:21 ` Claude review: amdxdna: fixes for closing a process Claude Code Review Bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260210164521.1094274-2-mario.limonciello@amd.com \
    --to=mario.limonciello@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=lizhi.hou@amd.com \
    --cc=mamin506@gmail.com \
    --cc=ogabbay@kernel.org \
    --cc=superm1@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox