[PATCH v3 4/8] thunderbolt: Fix error handling and unwind paths in tb_nhi_probe()

From: Chunfeng Yun

Date: Mon Nov 23 2026 - 06:38:10 EST


Currently, tb_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
dma_set_mask_and_coherent(), tb_nhi_ops.init(), or nhi_select_cm(),
tb_nhi_probe() immediately returns without calling tb_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 tb_nhi_ops.init_interrupts(). If a hardware interrupt fires
immediately upon registration, tb_nhi_isr() 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
tb_nhi_ops.init_interrupts(). If DMA mask setup fails, exit early
without needing any interrupt teardown.
- Consolidate post-interrupt error paths by tb_nhi_shutdown(nhi).

Signed-off-by: Chunfeng Yun <chunfeng.yun@xxxxxxxxxxxx>
---
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 69783e81dd3b..f687b37aac91 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -1256,6 +1256,10 @@ int tb_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);

@@ -1269,6 +1273,8 @@ int tb_nhi_probe(struct tb_nhi *nhi)
if (!nhi->tx_rings || !nhi->rx_rings || !nhi->interrupt_mask)
return -ENOMEM;

+ spin_lock_init(&nhi->lock);
+
INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);

nhi_reset(nhi);
@@ -1280,24 +1286,22 @@ int tb_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");

@@ -1311,8 +1315,8 @@ int tb_nhi_probe(struct tb_nhi *nhi)
*/
tb_domain_put(tb);
wait_for_completion(&nhi->domain_released);
- tb_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);

@@ -1324,6 +1328,10 @@ int tb_nhi_probe(struct tb_nhi *nhi)
pm_runtime_put_autosuspend(dev);

return 0;
+
+err:
+ tb_nhi_shutdown(nhi);
+ return res;
}
EXPORT_SYMBOL_GPL(tb_nhi_probe);

--
2.45.2