From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=210.61.82.184 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796459650; b=xj5Eg0E3laF7ZU9mWSF6Uyc4jqtz3/Bd5fRuMe+u44El/eOkJ6WtzF5kqnB89bq3un1K JHvepadrYuv6GaDWM+yAUlemGFGFmnaENTNU2AJ2hEhKLRq9pyUhG0a4qf33hr5iK8uEU ezh+8UD9aw75bXceq8UClmhHsFc3XzCHqi52mjVOcXQ9RceW4m8GpEff1mw4Sgy3x0htz 1zpuGR76y6jtx9acbkteveyKG9r89I4TPRUJdizjNAEkcYVs8HZ4qvhr6DmVCkLz0dXlb INM+z1ixqJPbLBHkOikCbd6jezEzrVVGMl6oYTidmjCsEyMiupBzFKQkK6rfufRxGQg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796459650; h=DMARC-Filter:X-UUID:DKIM-Signature:X-CID-P-RULE:X-CID-O-INFO: X-CID-META:X-CID-BVR:X-CID-BAS:X-CID-FACTOR:X-CID-RHF:X-UUID:Received: Received:Received:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding: Content-Type:X-MTK; bh=2mlsBHxzhOMGBtGQK0hKOC/NQrUh3vWDV0hraU8hfjk=; b=Jkb+zzh7ind/qeu3eqPCo7JQvgYQQOOqDb42TY8x41TpQKgcyg77b7eSRCPjSAWg4hXu zUIk7nkXabMCn2IYv8VARsrUARTGec0snoPATzTiFSEQj5DBN9fHhH3muuDWBirCrGpCC pLN1ox1itMO8ebSzVeekQPk4O7b1dVBpMJjZvYPaAOjE0m3Ppz9d3u0OBJdC85dP+tiWN Cr1ZgaxOXqt6iweAQkiDeluD9t4plK1yjGCGO5tg99gacfL8U8MJEuelur/l+oSVCkBTC Vyjszs1YhYOmRp/iXe7lvGXh/cuHXihjpdNRz0Bh2piOf26C5qMR0lWSzt5Jda9R8xw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=mediatek.com; spf=pass smtp.mailfrom=mediatek.com; dkim=pass header.d=mediatek.com header.i=@mediatek.com header.a=rsa-sha256 header.s=dk header.b=hBkWOUDv; arc=none smtp.remote-ip=210.61.82.184 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1080606A330; Sat, 5 Dec 2026 08:34:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E1080606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mediatek.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.a=rsa-sha256 header.s=dk header.b=hBkWOUDv X-UUID: 9dfcd174f08611f1a7aecdb2598be72c-20261205 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=2mlsBHxzhOMGBtGQK0hKOC/NQrUh3vWDV0hraU8hfjk=; b=hBkWOUDvQdsNVSHRdxg+4+ZGrrm2IIMS8iUUjfJZfxYcEoA4OeuyQ0gpTkQP531PBfDN78ShvY6RuYloxmyAFaeSc3Vwp5vGNUN32pQMKQLkf9mxsSAF54M2a40FS1R6UVT9GrFYd6EuePNPcPirJFAptB8GBBzCQvCstIhMcwg=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.22,REQID:fbe8b180-d938-4975-9242-13b42ec3183e,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:d36851b,CLOUDID:5c63c631-f289-4656-bb36-27baf4489f11,B ulkID:nil,BulkQuantity:0,SF:81|82|102|136|836|865|888|898,TC:-5,Content:0| 15|50|99,EDM:-3|-100,IP:nil,URL:0,File:130,RT:0,Bulk:nil,QS:nil,BEC:nil,CO L:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 9dfcd174f08611f1a7aecdb2598be72c-20261205 Received: from mtkmbs14n2.mediatek.inc [(172.21.101.76)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1404641035; Sat, 05 Dec 2026 16:33:59 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by mtkmbs11n1.mediatek.inc (172.21.101.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.56; Sat, 5 Dec 2026 16:33:57 +0800 Received: from gcnsap21.gcn.mediatek.inc (10.17.81.22) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.2562.56 via Frontend Transport; Sat, 5 Dec 2026 16:33:57 +0800 From: Chunfeng Yun To: Mika Westerberg CC: Andreas Noever , Konrad Dybcio , Sven Peter , Yehezkel Bernat , Matthias Brugger , Greg Kroah-Hartman , AngeloGioacchino Del Regno , , , , , Chunfeng Yun Subject: [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe() Date: Sat, 5 Dec 2026 16:33:26 +0800 Message-ID: <20261205083342.4009278-2-chunfeng.yun@mediatek.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261205083342.4009278-1-chunfeng.yun@mediatek.com> References: <20261205083342.4009278-1-chunfeng.yun@mediatek.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-MTK: N 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 --- 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