Skip to contentSkip to conversation

octeontx2-pf: handle cn20k_pfvf_mbox_alloc() failure in otx2_pfvf_mbox_init()

PATCH In review Elkin Cruz 2 participants 2 messages Nov 20 – Nov 24

Subsystems (3)
lore
Share & tools
lore

Elkin Cruz

author
#0 lore raw

PATCH 1/1 octeontx2-pf: handle cn20k_pfvf_mbox_alloc() failure in otx2_pfvf_mbox_init()

On CN20K, cn20k_pfvf_mbox_alloc() returns ERR_PTR(-ENOMEM) when qmem_alloc() fails, but otx2_pfvf_mbox_init() assigns hwbase without checking IS_ERR(hwbase). On allocation failure, the error pointer is passed directly into otx2_mbox_init(), which writes reset headers to hwbase + devid * MBOX_SIZE and crashes the kernel.

In addition, if otx2_mbox_init() fails on CN20K after cn20k_pfvf_mbox_alloc() succeeded, free_iomem: skips qmem_free(&pf->pdev->dev, pf->pfvf_mbox_addr), leaking the DMA mailbox allocation, and if the second otx2_mbox_init(&mbox->mbox_up, ...) call fails, &mbox->mbox is not destroyed.

Check IS_ERR(hwbase) and update error unwind in otx2_pfvf_mbox_init() to properly clean up &mbox->mbox, free mailbox memory on CN20K, and clear pf->mbox_pfvf_wq.

Fixes 70f8986ecef1 ("octeontx2-pf: CN20K mbox implementation between PF-VF")

---
I am trying out a new Clang static analysis linter for ERR_PTR/NULL handling, and it flagged this code as possibly buggy. The findings were triaged and fixed with an AI coding assistant, then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

 .../net/ethernet/marvell/octeontx2/nic/otx2_pf.c  | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c +13 -2
index cb602a4adcfb..e6722751de7b 100644
@@ -618,6 +618,10 @@ static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int numvfs)
 	 */
 	if (is_cn20k(pf->pdev)) {
 		hwbase = (void __iomem *)cn20k_pfvf_mbox_alloc(pf, numvfs);
+		if (IS_ERR(hwbase)) {
+			err = PTR_ERR(hwbase);
+			goto free_wq;
+		}
 	} else {
 		/* On CN10K platform, PF <-> VF mailbox region follows after
 		 * PF <-> AF mailbox region.
@@ -644,7 +648,7 @@ static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int numvfs)
 	err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
 			     MBOX_DIR_PFVF_UP, numvfs);
 	if (err)
-		goto free_iomem;
+		goto destroy_mbox;
 
 	for (vf = 0; vf < numvfs; vf++) {
 		mbox->pfvf = pf;
@@ -655,11 +659,18 @@ static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int numvfs)
 
 	return 0;
 
+destroy_mbox:
+	otx2_mbox_destroy(&mbox->mbox);
 free_iomem:
-	if (hwbase && !(is_cn20k(pf->pdev)))
+	if (is_cn20k(pf->pdev)) {
+		qmem_free(&pf->pdev->dev, pf->pfvf_mbox_addr);
+		pf->pfvf_mbox_addr = NULL;
+	} else if (hwbase) {
 		iounmap(hwbase);
+	}
 free_wq:
 	destroy_workqueue(pf->mbox_pfvf_wq);
+	pf->mbox_pfvf_wq = NULL;
 	return err;
 }
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 97 lines

-----Original Message-----
From: Elkin Cruz <[email protected]>
Sent: Saturday, November 21, 2026 12:54 AM
To: Sunil Kovvuri Goutham <[email protected]>; Geethasowjanya Akula <[email protected]>; Ratheesh Kannoth <[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Bharat Bhushan <[email protected]>; Andrew Lunn <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Elkin Cruz <[email protected]>
Subject: [EXTERNAL] [PATCH net] octeontx2-pf: handle cn20k_pfvf_mbox_alloc() failure in otx2_pfvf_mbox_init()

On CN20K, cn20k_pfvf_mbox_alloc() returns ERR_PTR(-ENOMEM) when qmem_alloc() fails, but otx2_pfvf_mbox_init() assigns hwbase without checking IS_ERR(hwbase). On allocation failure, the error pointer is passed directly into otx2_mbox_init(), which writes reset headers to hwbase + devid * MBOX_SIZE and crashes the kernel.

In addition, if otx2_mbox_init() fails on CN20K after cn20k_pfvf_mbox_alloc() succeeded, free_iomem: skips qmem_free(&pf-

Quoted 1 line

pdev->dev, pf->pfvf_mbox_addr), leaking the DMA mailbox allocation, and if

the second otx2_mbox_init(&mbox->mbox_up, ...) call fails, &mbox->mbox is not destroyed.

Check IS_ERR(hwbase) and update error unwind in otx2_pfvf_mbox_init() to properly clean up &mbox->mbox, free mailbox memory on CN20K, and clear pf->mbox_pfvf_wq.

Fixes 70f8986ecef1 ("octeontx2-pf: CN20K mbox implementation between PF-

VF")

---
I am trying out a new Clang static analysis linter for ERR_PTR/NULL handling, and it flagged this code as possibly buggy. The findings were triaged and fixed with an AI coding assistant, then checked against linux-next (not runtime- tested on hardware).

Thanks,
Elkin

.../net/ethernet/marvell/octeontx2/nic/otx2_pf.c | 15 +++++++++++++--

1 file changed, 13 insertions(+), 2 deletions(-)
drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c +4 -0
index cb602a4adcfb..e6722751de7b 100644
@@ -618,6 +618,10 @@ static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int
 numvfs)
 	 */
 	if (is_cn20k(pf->pdev)) {
 		hwbase = (void __iomem *)cn20k_pfvf_mbox_alloc(pf,
 numvfs);
+		if (IS_ERR(hwbase)) {
+			err = PTR_ERR(hwbase);
+			goto free_wq;
+		}
 	} else {
		/* On CN10K platform, PF <-> VF mailbox region follows after
		 * PF <-> AF mailbox region.
drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c +1 -1
@@ -644,7 +648,7 @@ static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int
 numvfs)
 	err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf-
 >reg_base,
 			     MBOX_DIR_PFVF_UP, numvfs);
 	if (err)
-		goto free_iomem;
+		goto destroy_mbox;
 
	for (vf = 0; vf < numvfs; vf++) {
		mbox->pfvf = pf;
drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c +2 -0
@@ -655,11 +659,18 @@ static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int
 numvfs)
 
 	return 0;
 
+destroy_mbox:
+	otx2_mbox_destroy(&mbox->mbox);

free_iomem:

drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c +4 -1
-	if (hwbase && !(is_cn20k(pf->pdev)))
+	if (is_cn20k(pf->pdev)) {
+		qmem_free(&pf->pdev->dev, pf->pfvf_mbox_addr);
+		pf->pfvf_mbox_addr = NULL;
+	} else if (hwbase) {
		iounmap(hwbase);
drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c +1 -0
+	}

free_wq:

	destroy_workqueue(pf->mbox_pfvf_wq);
drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c +1 -0
+	pf->mbox_pfvf_wq = NULL;
	return err;
}
Quoted 2 lines

--
2.56.1.750.gf70dc682cf-goog

Thanks for the patch.