This series fixes memory leaks in the initialization paths of the
NIC devices.
Patch 1 moves the initialization of oct->props[i].netdev before queue
setup calls. This ensures that if queue setup fails, the cleanup function
can find and free the allocated netdev. It also initializes lio->oct_dev
early to prevent a crash in the cleanup path.
Patch 2 fixes an off-by-one error in the PF cleanup loop. It ensures
the current device index is cleaned up and correctly handles the
post-loop devlink_alloc failure case.
Patch 3 fixes the same off-by-one error in the VF cleanup loop.
Signed-off-by: Zilin Guan <redacted>
Changes in v4:
- Move the netdev initialization fix to patch 1, as it is a
prerequisite for the cleanup fixes.
- Change the cleanup loop to 'do { ... } while (i >= 0)' in PF and VF
setup_nic_devices(), as the caller guarantees octeon_dev->ifcount > 0,
ensuring i is never decremented below 0.
Changes in v3:
- Split the off-by-one fix into separate patches for PF and VF.
- Patch 2: Decrement i in the devlink_alloc error path before jumping
to cleanup to avoid accessing an invalid index.
- Patch 1: Initialize lio->oct_dev alongside props->netdev to prevent
NULL pointer dereference in cleanup_rx_oom_poll_fn().
Changes in v2:
- Add patch 2 to fix an off-by-one error in the error handling loop logic.
Zilin Guan (3):
net: liquidio: Initialize netdev pointer before queue setup
net: liquidio: Fix off-by-one error in PF setup_nic_devices() cleanup
net: liquidio: Fix off-by-one error in VF setup_nic_devices() cleanup
.../net/ethernet/cavium/liquidio/lio_main.c | 39 ++++++++++---------
.../ethernet/cavium/liquidio/lio_vf_main.c | 4 +-
2 files changed, 22 insertions(+), 21 deletions(-)
--
2.34.1
In setup_nic_devices(), the netdev is allocated using alloc_etherdev_mq().
However, the pointer to this structure is stored in oct->props[i].netdev
only after the calls to netif_set_real_num_rx_queues() and
netif_set_real_num_tx_queues().
If either of these functions fails, setup_nic_devices() returns an error
without freeing the allocated netdev. Since oct->props[i].netdev is still
NULL at this point, the cleanup function liquidio_destroy_nic_device()
will fail to find and free the netdev, resulting in a memory leak.
Fix this by initializing oct->props[i].netdev before calling the queue
setup functions. This ensures that the netdev is properly accessible for
cleanup in case of errors.
Compile tested only. Issue found using a prototype static analysis tool
and code review.
Fixes: c33c997346c3 ("liquidio: enhanced ethtool --set-channels feature")
Signed-off-by: Zilin Guan <redacted>
---
.../net/ethernet/cavium/liquidio/lio_main.c | 34 +++++++++----------
1 file changed, 17 insertions(+), 17 deletions(-)
@@ -3505,6 +3505,23 @@ static int setup_nic_devices(struct octeon_device *octeon_dev)*/netdev->netdev_ops=&lionetdevops;+lio=GET_LIO(netdev);++memset(lio,0,sizeof(structlio));++lio->ifidx=ifidx_or_pfnum;++props=&octeon_dev->props[i];+props->gmxport=resp->cfg_info.linfo.gmxport;+props->netdev=netdev;++/* Point to the properties for octeon device to which this+*interfacebelongs.+*/+lio->oct_dev=octeon_dev;+lio->octprops=props;+lio->netdev=netdev;+retval=netif_set_real_num_rx_queues(netdev,num_oqueues);if(retval){dev_err(&octeon_dev->pci_dev->dev,
@@ -3521,16 +3538,6 @@ static int setup_nic_devices(struct octeon_device *octeon_dev)gotosetup_nic_dev_free;}-lio=GET_LIO(netdev);--memset(lio,0,sizeof(structlio));--lio->ifidx=ifidx_or_pfnum;--props=&octeon_dev->props[i];-props->gmxport=resp->cfg_info.linfo.gmxport;-props->netdev=netdev;-lio->linfo.num_rxpciq=num_oqueues;lio->linfo.num_txpciq=num_iqueues;for(j=0;j<num_oqueues;j++){
@@ -3596,13 +3603,6 @@ static int setup_nic_devices(struct octeon_device *octeon_dev)netdev->min_mtu=LIO_MIN_MTU_SIZE;netdev->max_mtu=LIO_MAX_MTU_SIZE;-/* Point to the properties for octeon device to which this-*interfacebelongs.-*/-lio->oct_dev=octeon_dev;-lio->octprops=props;-lio->netdev=netdev;-dev_dbg(&octeon_dev->pci_dev->dev,"if%d gmx: %d hw_addr: 0x%llx\n",i,lio->linfo.gmxport,CVM_CAST64(lio->linfo.hw_addr));
In setup_nic_devices(), the initialization loop jumps to the label
setup_nic_dev_free on failure. The current cleanup loop while(i--)
skip the failing index i, causing a memory leak.
Fix this by changing the loop to iterate from the current index i
down to 0.
Also, decrement i in the devlink_alloc failure path to point to the
last successfully allocated index.
Compile tested only. Issue found using code review.
Fixes: f21fb3ed364b ("Add support of Cavium Liquidio ethernet adapters")
Suggested-by: Simon Horman <horms@kernel.org>
Signed-off-by: Zilin Guan <redacted>
---
drivers/net/ethernet/cavium/liquidio/lio_main.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
In setup_nic_devices(), the initialization loop jumps to the label
setup_nic_dev_free on failure. The current cleanup loop while(i--)
skip the failing index i, causing a memory leak.
Fix this by changing the loop to iterate from the current index i
down to 0.
Compile tested only. Issue found using code review.
Fixes: 846b46873eeb ("liquidio CN23XX: VF offload features")
Suggested-by: Simon Horman <horms@kernel.org>
Signed-off-by: Zilin Guan <redacted>
---
drivers/net/ethernet/cavium/liquidio/lio_vf_main.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
On Wed, 28 Jan 2026 15:44:37 +0000
Zilin Guan [off-list ref] wrote:
This series fixes memory leaks in the initialization paths of the
NIC devices.
Patch 1 moves the initialization of oct->props[i].netdev before queue
setup calls. This ensures that if queue setup fails, the cleanup function
can find and free the allocated netdev. It also initializes lio->oct_dev
early to prevent a crash in the cleanup path.
Patch 2 fixes an off-by-one error in the PF cleanup loop. It ensures
the current device index is cleaned up and correctly handles the
post-loop devlink_alloc failure case.
Patch 3 fixes the same off-by-one error in the VF cleanup loop.
For the series:
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Thank you!
--
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski [off-list ref]:
On Wed, 28 Jan 2026 15:44:37 +0000 you wrote:
This series fixes memory leaks in the initialization paths of the
NIC devices.
Patch 1 moves the initialization of oct->props[i].netdev before queue
setup calls. This ensures that if queue setup fails, the cleanup function
can find and free the allocated netdev. It also initializes lio->oct_dev
early to prevent a crash in the cleanup path.
[...]