octeontx2-af: fix cn20k mailbox lifetime on repeated rvu_mbox_init()
Ratheesh Kannoth
maintainerPATCH v2 1/1 octeontx2-af: fix cn20k mailbox lifetime on repeated rvu_mbox_init()
From: Sai Krishna <[email protected]>
rvu_mbox_init() is called separately for AF-PF mailboxes during probe and for AF-VF mailboxes when SR-IOV is enabled. Each call used to allocate a new ng_rvu object, leaking the first allocation when the pointer was overwritten on the second call.
Sharing one ng_rvu across both paths exposed several teardown bugs:
the error path freed all cn20k mailbox DMA and kfree()d ng_rvu even when only the failing init type should be unwound, leaving live AF-PF mailbox memory in use after an AF-VF init failure. mutex_init() was also re-run on the AF-VF path while AF-PF mailbox handlers could still hold rvu->mbox_lock. Probe and SR-IOV failure paths did not release cn20k mailbox DMA either, since cleanup only happened in rvu_remove().
Allocate ng_rvu once with devm_kzalloc(), initialize mbox_lock in the same block, unwind only the mailbox memory for the failing init type, and free cn20k mailbox DMA from the probe and pci_enable_sriov() error paths.
---
v1 -> v2: Addressed sashiko comments
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260820053656.2614614-1-rkannoth%40marvell.com --- .../ethernet/marvell/octeontx2/af/cn20k/api.h | 1 + .../marvell/octeontx2/af/cn20k/mbox_init.c | 21 +++++++++- .../net/ethernet/marvell/octeontx2/af/rvu.c | 39 +++++++++++-------- 3 files changed, 42 insertions(+), 19 deletions(-)
drivers/net/ethernet/marvell/octeontx2/af/cn20k/api.h +1 -0
@@ -21,6 +21,7 @@ int cn20k_rvu_mbox_init(struct rvu *rvu, int type, int num);int cn20k_rvu_get_mbox_regions (struct rvu *rvu,void **mbox_addr,int num,int type,unsigned long *pf_bmap);void cn20k_free_mbox_memory (struct rvu *rvu); +void cn20k_free_mbox_memory_type (struct rvu *rvu,int type);int cn20k_register_afpf_mbox_intr (struct rvu *rvu);int cn20k_register_afvf_mbox_intr (struct rvu *rvu,int pf_vec_start);void cn20k_rvu_enable_mbox_intr (struct rvu *rvu);
drivers/net/ethernet/marvell/octeontx2/af/cn20k/mbox_init.c +19 -2
@@ -335,13 +335,30 @@ int cn20k_rvu_mbox_init(struct rvu *rvu, int type, int ndevs)return rvu_alloc_mbox_memory (rvu, type, ndevs,MBOX_SIZE ); } +void cn20k_free_mbox_memory_type (struct rvu *rvu,int type) +{ +if (!is_cn20k (rvu->pdev) || !rvu->ng_rvu) +return ; + +switch (type) { +case TYPE_AFPF : +qmem_free (rvu->dev, rvu->ng_rvu->pf_mbox_addr); + rvu->ng_rvu->pf_mbox_addr =NULL ; +break ; +case TYPE_AFVF : +qmem_free (rvu->dev, rvu->ng_rvu->vf_mbox_addr); + rvu->ng_rvu->vf_mbox_addr =NULL ; +break ; + } +} +void cn20k_free_mbox_memory (struct rvu *rvu) {if (!is_cn20k (rvu->pdev))return ; -qmem_free (rvu->dev, rvu->ng_rvu->pf_mbox_addr); -qmem_free (rvu->dev, rvu->ng_rvu->vf_mbox_addr); +cn20k_free_mbox_memory_type (rvu,TYPE_AFPF ); +cn20k_free_mbox_memory_type (rvu,TYPE_AFVF ); }void cn20k_rvu_disable_afvf_intr (struct rvu *rvu,int vfs)
drivers/net/ethernet/marvell/octeontx2/af/rvu.c +22 -17
@@ -2585,12 +2585,6 @@ static int rvu_mbox_init(struct rvu *rvu, struct mbox_wq_info *mw,if (!pf_bmap)return -ENOMEM ; - ng_rvu_mbox =kzalloc_obj (*ng_rvu_mbox); -if (!ng_rvu_mbox) { - err = -ENOMEM ; - goto free_bitmap; - } -/* RVU VFs */ if (type ==TYPE_AFVF )bitmap_set (pf_bmap,0 , num);
@@ -2604,15 +2598,22 @@ static int rvu_mbox_init(struct rvu *rvu, struct mbox_wq_info *mw, } } - rvu->ng_rvu = ng_rvu_mbox; +if (!rvu->ng_rvu) { + ng_rvu_mbox =devm_kzalloc (rvu->dev,sizeof (*ng_rvu_mbox),GFP_KERNEL ); +if (!ng_rvu_mbox) { + err = -ENOMEM ; + goto free_bitmap; + } + + rvu->ng_rvu = ng_rvu_mbox; - rvu->ng_rvu->rvu_mbox_ops = &rvu_mbox_ops; + rvu->ng_rvu->rvu_mbox_ops = &rvu_mbox_ops; +mutex_init (&rvu->mbox_lock); + } err =cn20k_rvu_mbox_init (rvu, type, num);if (err) - goto free_mem; - -mutex_init (&rvu->mbox_lock); + goto free_bitmap; mbox_regions =kcalloc (num,sizeof (void __iomem *),GFP_KERNEL );if (!mbox_regions) {
@@ -2702,14 +2703,18 @@ static int rvu_mbox_init(struct rvu *rvu, struct mbox_wq_info *mw, free_regions:kfree (mbox_regions); free_qmem: -cn20k_free_mbox_memory (rvu); -free_mem: -kfree (rvu->ng_rvu); +cn20k_free_mbox_memory_type (rvu, type); free_bitmap:bitmap_free (pf_bmap);return err; } +static void rvu_free_cn20k_mbox_memory (struct rvu *rvu) +{ +if (is_cn20k (rvu->pdev)) +cn20k_free_mbox_memory (rvu); +} +static void rvu_mbox_destroy (struct mbox_wq_info *mw) {struct otx2_mbox *mbox = &mw->mbox;
@@ -3519,6 +3524,7 @@ static int rvu_enable_sriov(struct rvu *rvu)if (err) {rvu_disable_afvf_intr (rvu);rvu_mbox_destroy (&rvu->afvf_wq_info); +cn20k_free_mbox_memory_type (rvu,TYPE_AFVF );return err; }
@@ -3681,6 +3687,7 @@ static int rvu_probe(struct pci_dev *pdev, const struct pci_device_id *id) err_mbox:rvu_mbox_destroy (&rvu->afpf_wq_info); err_hwsetup: +rvu_free_cn20k_mbox_memory (rvu);rvu_cgx_exit (rvu);rvu_fwdata_exit (rvu);rvu_mcs_exit (rvu);
@@ -3723,9 +3730,7 @@ static void rvu_remove(struct pci_dev *pdev)pci_set_drvdata (pdev,NULL );devm_kfree (&pdev->dev, rvu->hw); -if (is_cn20k (rvu->pdev)) -cn20k_free_mbox_memory (rvu); -kfree (rvu->ng_rvu); +rvu_free_cn20k_mbox_memory (rvu);devm_kfree (&pdev->dev, rvu);atomic_set (&device_bound,0 ); }
Signature · 1 line
2.43.0
Quoted 20 lines · Ratheesh Kannoth
From: Sai Krishna <[email protected]>
rvu_mbox_init()is called separately for AF-PF mailboxes during probe and for AF-VF mailboxes when SR-IOV is enabled. Each call used to allocate a newng_rvuobject, leaking the first allocation when the pointer was overwritten on the second call.Sharing one
ng_rvuacross both paths exposed several teardown bugs:
the error path freed all cn20k mailbox DMA and kfree()dng_rvueven when only the failing init type should be unwound, leaving live AF-PF mailbox memory in use after an AF-VF init failure.mutex_init()was also re-run on the AF-VF path while AF-PF mailbox handlers could still holdrvu->mbox_lock. Probe and SR-IOV failure paths did not release cn20k mailbox DMA either, since cleanup only happened inrvu_remove().Allocate
ng_rvuonce withdevm_kzalloc(), initializembox_lockin the same block, unwind only the mailbox memory for the failing init type, and free cn20k mailbox DMA from the probe andpci_enable_sriov()error paths.
Quoted 7 lines
Fixes e53ee4acb220 ("octeontx2-af: CN20k basic mbox operations and structures")Signed-off-by Sai Krishna <[email protected]>Signed-off-by Ratheesh Kannoth <[email protected]>---
v1 -> v2: Addressed sashiko commentshttps://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260820053656.2614614-1-rkannoth%40marvell.com
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Fri, 21 Aug 2026 15:53:37 +0530 you wrote:
From: Sai Krishna <[email protected]>
rvu_mbox_init()is called separately for AF-PF mailboxes during probe and for AF-VF mailboxes when SR-IOV is enabled. Each call used to allocate a newng_rvuobject, leaking the first allocation when the pointer was overwritten on the second call.[...]
Here is the summary with links:
- [v2,net] octeontx2-af: fix cn20k mailbox lifetime on repeated rvu_mbox_init()
https://git.kernel.org/netdev/net/c/3b11a77f6998
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html