Skip to contentSkip to conversation

net: liquidio: Fix memory leaks in setup_nic_devices()

PATCH Applied Zilin Guan 3 participants 6 messages Jan 28 – Jan 30

Subsystems (3)
lore
Share & tools
lore

Zilin Guan

author
#0 lore raw

v4 cover net: liquidio: Fix memory leaks in setup_nic_devices()

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.

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(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v4 1/3 net: liquidio: Initialize netdev pointer before queue setup

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")
---
 .../net/ethernet/cavium/liquidio/lio_main.c   | 34 +++++++++----------
 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/net/ethernet/cavium/liquidio/lio_main.c +17 -17
index 0732440eeacd..1f10b1b22a1e 100644
@@ -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(struct lio));
+
+		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
+		 * interface belongs.
+		 */
+		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)
 			goto setup_nic_dev_free;
 		}
 
-		lio = GET_LIO(netdev);
-
-		memset(lio, 0, sizeof(struct lio));
-
-		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
-		 * interface belongs.
-		 */
-		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));
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v4 2/3 net: liquidio: Fix off-by-one error in PF setup_nic_devices() cleanup

In reply to Zilin Guan

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")
---
 drivers/net/ethernet/cavium/liquidio/lio_main.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/ethernet/cavium/liquidio/lio_main.c +3 -2
index 1f10b1b22a1e..c1a3df225254 100644
@@ -3750,6 +3750,7 @@ static int setup_nic_devices(struct octeon_device *octeon_dev)
 	if (!devlink) {
 		device_unlock(&octeon_dev->pci_dev->dev);
 		dev_err(&octeon_dev->pci_dev->dev, "devlink alloc failed\n");
+		i--;
 		goto setup_nic_dev_free;
 	}
 
@@ -3765,11 +3766,11 @@ static int setup_nic_devices(struct octeon_device *octeon_dev)
 
 setup_nic_dev_free:
 
-	while (i--) {
+	do {
 		dev_err(&octeon_dev->pci_dev->dev,
 			"NIC ifidx:%d Setup failed\n", i);
 		liquidio_destroy_nic_device(octeon_dev, i);
-	}
+	} while (i--);
 
 setup_nic_dev_done:
 
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v4 3/3 net: liquidio: Fix off-by-one error in VF setup_nic_devices() cleanup

In reply to Zilin Guan

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")
---
 drivers/net/ethernet/cavium/liquidio/lio_vf_main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/cavium/liquidio/lio_vf_main.c +2 -2
index e02942dbbcce..43c595f3b84e 100644
@@ -2212,11 +2212,11 @@ static int setup_nic_devices(struct octeon_device *octeon_dev)
 
 setup_nic_dev_free:
 
-	while (i--) {
+	do {
 		dev_err(&octeon_dev->pci_dev->dev,
 			"NIC ifidx:%d Setup failed\n", i);
 		liquidio_destroy_nic_device(octeon_dev, i);
-	}
+	} while (i--);
 
 setup_nic_dev_done:
 
Signature · 1 line
2.34.1
Reviewed-by on the v4 series
#4 lore raw
In reply to Zilin Guan
Quoted 8 lines · On Wed, 28 Jan 2026 15:44:37 +0000 Zilin Guan <[email protected]> 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.

Quoted 5 lines

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:

Thank you!

Signature · 3 lines
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com
#5 lore raw
In reply to Zilin Guan
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · 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.

[...]

Here is the summary with links:
- [net,v4,1/3] net: liquidio: Initialize netdev pointer before queue setup

    https://git.kernel.org/netdev/net/c/926ede0c85e1
  - [net,v4,2/3] net: liquidio: Fix off-by-one error in PF setup_nic_devices() cleanup
    https://git.kernel.org/netdev/net/c/8558aef4e8a1
  - [net,v4,3/3] net: liquidio: Fix off-by-one error in VF setup_nic_devices() cleanup
    https://git.kernel.org/netdev/net/c/6cbba46934ae

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html