* [PATCH net] bnxt_en: Fix memory leak in bnxt_alloc_mem()
@ 2026-01-22 9:26 Zilin Guan
2026-01-22 9:44 ` Pavan Chebbi
0 siblings, 1 reply; 4+ messages in thread
From: Zilin Guan @ 2026-01-22 9:26 UTC (permalink / raw)
To: michael.chan
Cc: pavan.chebbi, andrew+netdev, davem, edumazet, kuba, pabeni,
netdev, linux-kernel, jianhao.xu, Zilin Guan
In bnxt_alloc_mem(), the function allocates memory for bp->bnapi,
bp->rx_ring, bp->tx_ring, and bp->tx_ring_map.
However, if the allocation for rx_ring, tx_ring, or tx_ring_map fails, the
function currently returns -ENOMEM directly without freeing the previously
allocated memory. This leads to a memory leak.
Fix this by jumping to the alloc_mem_err label when allocation fails,
which ensures that bnxt_free_mem() is called to properly release all
allocated resources.
Compile tested only. Issue found using a prototype static analysis tool
and code review.
Fixes: a960dec98861 ("bnxt_en: Add tx ring mapping logic.")
Fixes: b6ab4b01f53b ("bnxt_en: Separate bnxt_{rx|tx}_ring_info structs from bnxt_napi struct.")
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
drivers/net/ethernet/broadcom/bnxt/bnxt.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/drivers/net/ethernet/broadcom/bnxt/bnxt.c b/drivers/net/ethernet/broadcom/bnxt/bnxt.c
index 8419d1eb4035..99dae75146b5 100644
--- a/drivers/net/ethernet/broadcom/bnxt/bnxt.c
+++ b/drivers/net/ethernet/broadcom/bnxt/bnxt.c
@@ -5491,8 +5491,10 @@ static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
bp->rx_ring = kcalloc(bp->rx_nr_rings,
sizeof(struct bnxt_rx_ring_info),
GFP_KERNEL);
- if (!bp->rx_ring)
- return -ENOMEM;
+ if (!bp->rx_ring) {
+ rc = -ENOMEM;
+ goto alloc_mem_err;
+ }
for (i = 0; i < bp->rx_nr_rings; i++) {
struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
@@ -5512,14 +5514,18 @@ static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
bp->tx_ring = kcalloc(bp->tx_nr_rings,
sizeof(struct bnxt_tx_ring_info),
GFP_KERNEL);
- if (!bp->tx_ring)
- return -ENOMEM;
+ if (!bp->tx_ring) {
+ rc = -ENOMEM;
+ goto alloc_mem_err;
+ }
bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
GFP_KERNEL);
- if (!bp->tx_ring_map)
- return -ENOMEM;
+ if (!bp->tx_ring_map) {
+ rc = -ENOMEM;
+ goto alloc_mem_err;
+ }
if (bp->flags & BNXT_FLAG_SHARED_RINGS)
j = 0;
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH net] bnxt_en: Fix memory leak in bnxt_alloc_mem()
2026-01-22 9:26 [PATCH net] bnxt_en: Fix memory leak in bnxt_alloc_mem() Zilin Guan
@ 2026-01-22 9:44 ` Pavan Chebbi
2026-01-22 10:05 ` Zilin Guan
2026-01-26 10:09 ` Leon Romanovsky
0 siblings, 2 replies; 4+ messages in thread
From: Pavan Chebbi @ 2026-01-22 9:44 UTC (permalink / raw)
To: Zilin Guan
Cc: michael.chan, andrew+netdev, davem, edumazet, kuba, pabeni,
netdev, linux-kernel, jianhao.xu
[-- Attachment #1: Type: text/plain, Size: 2983 bytes --]
On Thu, Jan 22, 2026 at 2:56 PM Zilin Guan <zilin@seu.edu.cn> wrote:
>
> In bnxt_alloc_mem(), the function allocates memory for bp->bnapi,
> bp->rx_ring, bp->tx_ring, and bp->tx_ring_map.
>
> However, if the allocation for rx_ring, tx_ring, or tx_ring_map fails, the
> function currently returns -ENOMEM directly without freeing the previously
> allocated memory. This leads to a memory leak.
>
> Fix this by jumping to the alloc_mem_err label when allocation fails,
> which ensures that bnxt_free_mem() is called to properly release all
> allocated resources.
This fix is not needed. The memory is freed by the caller of bnxt_alloc_mem().
>
> Compile tested only. Issue found using a prototype static analysis tool
> and code review.
>
> Fixes: a960dec98861 ("bnxt_en: Add tx ring mapping logic.")
> Fixes: b6ab4b01f53b ("bnxt_en: Separate bnxt_{rx|tx}_ring_info structs from bnxt_napi struct.")
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> ---
> drivers/net/ethernet/broadcom/bnxt/bnxt.c | 18 ++++++++++++------
> 1 file changed, 12 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/net/ethernet/broadcom/bnxt/bnxt.c b/drivers/net/ethernet/broadcom/bnxt/bnxt.c
> index 8419d1eb4035..99dae75146b5 100644
> --- a/drivers/net/ethernet/broadcom/bnxt/bnxt.c
> +++ b/drivers/net/ethernet/broadcom/bnxt/bnxt.c
> @@ -5491,8 +5491,10 @@ static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
> bp->rx_ring = kcalloc(bp->rx_nr_rings,
> sizeof(struct bnxt_rx_ring_info),
> GFP_KERNEL);
> - if (!bp->rx_ring)
> - return -ENOMEM;
> + if (!bp->rx_ring) {
> + rc = -ENOMEM;
> + goto alloc_mem_err;
> + }
>
> for (i = 0; i < bp->rx_nr_rings; i++) {
> struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
> @@ -5512,14 +5514,18 @@ static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
> bp->tx_ring = kcalloc(bp->tx_nr_rings,
> sizeof(struct bnxt_tx_ring_info),
> GFP_KERNEL);
> - if (!bp->tx_ring)
> - return -ENOMEM;
> + if (!bp->tx_ring) {
> + rc = -ENOMEM;
> + goto alloc_mem_err;
> + }
>
> bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
> GFP_KERNEL);
>
> - if (!bp->tx_ring_map)
> - return -ENOMEM;
> + if (!bp->tx_ring_map) {
> + rc = -ENOMEM;
> + goto alloc_mem_err;
> + }
>
> if (bp->flags & BNXT_FLAG_SHARED_RINGS)
> j = 0;
> --
> 2.34.1
>
[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 5469 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH net] bnxt_en: Fix memory leak in bnxt_alloc_mem()
2026-01-22 9:44 ` Pavan Chebbi
@ 2026-01-22 10:05 ` Zilin Guan
2026-01-26 10:09 ` Leon Romanovsky
1 sibling, 0 replies; 4+ messages in thread
From: Zilin Guan @ 2026-01-22 10:05 UTC (permalink / raw)
To: pavan.chebbi
Cc: andrew+netdev, davem, edumazet, jianhao.xu, kuba, linux-kernel,
michael.chan, netdev, pabeni, zilin
On Thu, Jan 22, 2026 at 03:14:08PM +0530, Pavan Chebbi wrote:
> On Thu, Jan 22, 2026 at 2:56 PM Zilin Guan <zilin@seu.edu.cn> wrote:
> >
> > In bnxt_alloc_mem(), the function allocates memory for bp->bnapi,
> > bp->rx_ring, bp->tx_ring, and bp->tx_ring_map.
> >
> > However, if the allocation for rx_ring, tx_ring, or tx_ring_map fails, the
> > function currently returns -ENOMEM directly without freeing the previously
> > allocated memory. This leads to a memory leak.
> >
> > Fix this by jumping to the alloc_mem_err label when allocation fails,
> > which ensures that bnxt_free_mem() is called to properly release all
> > allocated resources.
>
> This fix is not needed. The memory is freed by the caller of bnxt_alloc_mem().
Hi Pavan,
Thank you for your review and clarification.
I observed that most allocation failure paths in bnxt_alloc_mem() jump to
alloc_mem_err for error handling, which led me to incorrectly assume that
this function was strictly responsible for cleaning up its own allocated
memory upon failure. I failed to realize that the caller also handles the
cleanup for these specific cases.
Thanks for pointing this out. Sorry for the noise.
Best regards,
Zilin Guan
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net] bnxt_en: Fix memory leak in bnxt_alloc_mem()
2026-01-22 9:44 ` Pavan Chebbi
2026-01-22 10:05 ` Zilin Guan
@ 2026-01-26 10:09 ` Leon Romanovsky
1 sibling, 0 replies; 4+ messages in thread
From: Leon Romanovsky @ 2026-01-26 10:09 UTC (permalink / raw)
To: Pavan Chebbi
Cc: Zilin Guan, michael.chan, andrew+netdev, davem, edumazet, kuba,
pabeni, netdev, linux-kernel, jianhao.xu
On Thu, Jan 22, 2026 at 03:14:08PM +0530, Pavan Chebbi wrote:
> On Thu, Jan 22, 2026 at 2:56 PM Zilin Guan <zilin@seu.edu.cn> wrote:
> >
> > In bnxt_alloc_mem(), the function allocates memory for bp->bnapi,
> > bp->rx_ring, bp->tx_ring, and bp->tx_ring_map.
> >
> > However, if the allocation for rx_ring, tx_ring, or tx_ring_map fails, the
> > function currently returns -ENOMEM directly without freeing the previously
> > allocated memory. This leads to a memory leak.
> >
> > Fix this by jumping to the alloc_mem_err label when allocation fails,
> > which ensures that bnxt_free_mem() is called to properly release all
> > allocated resources.
>
> This fix is not needed. The memory is freed by the caller of bnxt_alloc_mem().
That is an anti-pattern as well. On error, the function should clean up
all resources it allocated.
Thanks
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-01-26 10:09 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-22 9:26 [PATCH net] bnxt_en: Fix memory leak in bnxt_alloc_mem() Zilin Guan
2026-01-22 9:44 ` Pavan Chebbi
2026-01-22 10:05 ` Zilin Guan
2026-01-26 10:09 ` Leon Romanovsky
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox