Re: [PATCH net-next] cxgb4: fix memory leak in cxgb4_init_ethtool_filters() error path

From: Simon Horman
Date: Fri Apr 11 2025 - 10:59:02 EST


On Wed, Apr 09, 2025 at 05:47:46PM +0200, Markus Elfring wrote:
>
> > +++ b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c
> > @@ -2270,6 +2270,7 @@ int cxgb4_init_ethtool_filters(struct adapter *adap)
> > eth_filter->port[i].bmap = bitmap_zalloc(nentries, GFP_KERNEL);
> > if (!eth_filter->port[i].bmap) {
> > ret = -ENOMEM;
> > + kvfree(eth_filter->port[i].loc_array);
> > goto free_eth_finfo;
> > }
> > }
>
> How do you think about to move the shown error code assignment behind the mentioned label
> (so that another bit of duplicate source code could be avoided)?

Hi Markus,

If you mean something like the following. Then I agree that it
is both in keeping with the existing error handling in this function
and addresses the problem at hand.

diff --git a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c
index 7f3f5afa864f..df26d3388c00 100644
--- a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c
+++ b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c
@@ -2270,13 +2270,15 @@ int cxgb4_init_ethtool_filters(struct adapter *adap)
eth_filter->port[i].bmap = bitmap_zalloc(nentries, GFP_KERNEL);
if (!eth_filter->port[i].bmap) {
ret = -ENOMEM;
- goto free_eth_finfo;
+ goto free_eth_finfo_loc_array;
}
}

adap->ethtool_filters = eth_filter;
return 0;

+free_eth_finfo_loc_array:
+ kvfree(eth_filter->port[i].loc_array);
free_eth_finfo:
while (i-- > 0) {
bitmap_free(eth_filter->port[i].bmap);


>
> Regards,
> Markus
>