mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chunfeng Yun <chunfeng.yun@mediatek.com>
To: Mika Westerberg <westeri@kernel.org>
Cc: Andreas Noever <andreas.noever@gmail.com>,
	Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>,
	Sven Peter <sven@kernel.org>,
	Yehezkel Bernat <YehezkelShB@gmail.com>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	AngeloGioacchino Del Regno
	<angelogioacchino.delregno@collabora.com>,
	<linux-kernel@vger.kernel.org>, <linux-usb@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-mediatek@lists.infradead.org>,
	Chunfeng Yun <chunfeng.yun@mediatek.com>
Subject: [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe()
Date: Sat, 5 Dec 2026 16:33:26 +0800	[thread overview]
Message-ID: <20261205083342.4009278-2-chunfeng.yun@mediatek.com> (raw)
In-Reply-To: <20261205083342.4009278-1-chunfeng.yun@mediatek.com>

Currently, nhi_probe() has several issues in its error unwind
paths:

1. Incomplete cleanup on failure:
If an error occurs after interrupts are initialized, such as in
nhi->ops->init() or nhi_select_cm(), nhi_probe() immediately
returns without calling nhi_shutdown().
This leaves resources uncleaned:
- Hardware interrupt registers remain unmasked.
- Any scheduled nhi->interrupt_work is not flushed before the driver
  core frees the tb_nhi structure, risking a use-after-free.
- For Ice Lake PCIe controllers, icl_nhi_ops.init powers on the
  controller, but icl_nhi_shutdown() is never called to turn
  force-power back off, leaking power.

2. Fragile initialization order:
dma_set_mask_and_coherent() and spin_lock_init(&nhi->lock) are called
after nhi->ops->init_interrupts(). If a hardware interrupt fires
immediately upon registration, nhi_msi() schedules interrupt_work,
which acquires &nhi->lock before it is initialized. Furthermore, if
DMA mask configuration fails, interrupts have already been allocated
and must be torn down.

Improve the probe sequence and error handling:
- Move dma_set_mask_and_coherent() and spin_lock_init() before
  nhi->ops->init_interrupts(). If DMA mask setup fails, exit early
  before interrupts are enabled.
- Handle post-interrupt error paths by nhi_shutdown().

Fixes: e241d98e04ef ("thunderbolt: Separate out common NHI bits")

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
v4: no changes, but move the patch earlier in the series
v3: new patch
---
 drivers/thunderbolt/nhi.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index e99a3fcc4a29..d998d3caad75 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -1318,6 +1318,10 @@ int nhi_probe(struct tb_nhi *nhi)
 	if (!nhi->ops->init_interrupts)
 		return dev_err_probe(dev, -EINVAL, "missing required NHI ops\n");
 
+	res = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
+	if (res)
+		return dev_err_probe(dev, res, "failed to set DMA mask\n");
+
 	nhi->hop_count = ioread32(nhi->iobase + REG_CAPS) & 0x3ff;
 	dev_dbg(dev, "total paths: %d\n", nhi->hop_count);
 
@@ -1331,6 +1335,8 @@ int nhi_probe(struct tb_nhi *nhi)
 	if (!nhi->tx_rings || !nhi->rx_rings || !nhi->interrupt_mask)
 		return -ENOMEM;
 
+	spin_lock_init(&nhi->lock);
+
 	nhi_reset(nhi);
 
 	/* In case someone left them on. */
@@ -1340,24 +1346,22 @@ int nhi_probe(struct tb_nhi *nhi)
 	if (res)
 		return dev_err_probe(dev, res, "cannot enable interrupts, aborting\n");
 
-	spin_lock_init(&nhi->lock);
-
-	res = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
-	if (res)
-		return dev_err_probe(dev, res, "failed to set DMA mask\n");
-
 	if (nhi->ops->init) {
 		res = nhi->ops->init(nhi);
-		if (res)
-			return dev_err_probe(dev, res, "NHI specific init failed\n");
+		if (res) {
+			dev_err_probe(dev, res, "NHI specific init failed\n");
+			goto err;
+		}
 	}
 
 	init_completion(&nhi->domain_released);
 
 	tb = nhi_select_cm(nhi);
-	if (!tb)
-		return dev_err_probe(dev, -ENODEV,
-			"failed to determine connection manager, aborting\n");
+	if (!tb) {
+		res = dev_err_probe(dev, -ENODEV,
+				    "failed to determine connection manager, aborting\n");
+		goto err;
+	}
 
 	dev_dbg(dev, "NHI initialized, starting thunderbolt\n");
 
@@ -1371,8 +1375,8 @@ int nhi_probe(struct tb_nhi *nhi)
 		 */
 		tb_domain_put(tb);
 		wait_for_completion(&nhi->domain_released);
-		nhi_shutdown(nhi);
-		return dev_err_probe(dev, res, "failed to add domain\n");
+		dev_err_probe(dev, res, "failed to add domain\n");
+		goto err;
 	}
 	dev_set_drvdata(dev, tb);
 
@@ -1384,6 +1388,10 @@ int nhi_probe(struct tb_nhi *nhi)
 	pm_runtime_put_autosuspend(dev);
 
 	return 0;
+
+err:
+	nhi_shutdown(nhi);
+	return res;
 }
 
 /*
-- 
2.45.2


  reply	other threads:[~2026-12-05  8:34 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
2026-12-05  8:33 ` Chunfeng Yun [this message]
2026-12-05  8:33 ` [PATCH v4 02/14] thunderbolt: Prefix NHI core symbols with tb_ and tb_ring_ Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 03/14] thunderbolt: Internalize non-MSI-X interrupt request and work in NHI core Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 05/14] thunderbolt: Move rpm_complete bus walk to domain.c Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove() Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 07/14] thunderbolt: Move ICM mailbox functions to icm.c Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 08/14] thunderbolt: Decouple ICM probe from NHI core via .select_cm hook Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 09/14] thunderbolt: Make PCIe NHI support opt-in Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 10/14] thunderbolt: Make nhi->ops optional Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 11/14] thunderbolt: Add platform NHI driver Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 12/14] thunderbolt: Increase the lane adapter link-up timeout value Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 13/14] thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 14/14] thunderbolt: Drop WARN_ON() in tb_disconnect_pci() Chunfeng Yun

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=20261205083342.4009278-2-chunfeng.yun@mediatek.com \
    --to=chunfeng.yun@mediatek.com \
    --cc=YehezkelShB@gmail.com \
    --cc=andreas.noever@gmail.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=konrad.dybcio@oss.qualcomm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=matthias.bgg@gmail.com \
    --cc=sven@kernel.org \
    --cc=westeri@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

all inboxes | Powered by JetHome®