In setup_nic_devices, the initialization loop jumps to the label
setup_nic_dev_free on failure. The current error handling path uses
a while loop that decrements the device index i. This logic skips
the release of resources for the device at the current index i where
the failure occurred.
Use a do-while loop instead. This guarantees that
liquidio_destroy_nic_device() is called for the failing device index i
and continues for all previously initialized devices.
Compile tested only. Issue found using code review.
Fixes: f21fb3ed364b ("Add support of Cavium Liquidio ethernet adapters")
Fixes: 846b46873eeb ("liquidio CN23XX: VF offload features")
Suggested-by: Simon Horman <horms@kernel.org>
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
drivers/net/ethernet/cavium/liquidio/lio_main.c | 4 ++--
drivers/net/ethernet/cavium/liquidio/lio_vf_main.c | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/net/ethernet/cavium/liquidio/lio_main.c b/drivers/net/ethernet/cavium/liquidio/lio_main.c
index 0732440eeacd..3ba2806f5d1e 100644
--- a/drivers/net/ethernet/cavium/liquidio/lio_main.c
+++ b/drivers/net/ethernet/cavium/liquidio/lio_main.c
@@ -3765,11 +3765,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:
diff --git a/drivers/net/ethernet/cavium/liquidio/lio_vf_main.c b/drivers/net/ethernet/cavium/liquidio/lio_vf_main.c
index e02942dbbcce..43c595f3b84e 100644
--- a/drivers/net/ethernet/cavium/liquidio/lio_vf_main.c
+++ b/drivers/net/ethernet/cavium/liquidio/lio_vf_main.c
@@ -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:
--
2.34.1
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
---
net: liquidio: Fix off-by-one error in setup_nic_devices() cleanup
This patch changes the cleanup loop from while(i--) to do{}while(i--)
to ensure that when a device fails to initialize, it gets cleaned up
along with all previously initialized devices.
> diff --git a/drivers/net/ethernet/cavium/liquidio/lio_main.c b/drivers/net/ethernet/cavium/liquidio/lio_main.c
> index 0732440eeacd..3ba2806f5d1e 100644
> --- a/drivers/net/ethernet/cavium/liquidio/lio_main.c
> +++ b/drivers/net/ethernet/cavium/liquidio/lio_main.c
> @@ -3765,11 +3765,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--);
In lio_main.c, setup_nic_devices() can also reach setup_nic_dev_free from
the devlink_alloc() failure path after the main for loop completes. At that
point i equals ifcount (the loop counter has been incremented past the last
valid device).
With the do-while change, the first iteration will call
liquidio_destroy_nic_device(octeon_dev, ifcount) for a device index that
was never initialized. The function has a guard for NULL netdev so it will
not crash, but it will print a spurious error message about a device that
never existed.
The fix works correctly for failures within the loop (which is the case
described in the commit message), but is there a way to handle the
post-loop devlink_alloc() failure case without printing the spurious
error message?
[ ... ]
--
pw-bot: cr
On Mon, Jan 26, 2026 at 03:50:39PM +0000, Simon Horman wrote:
> This is an AI-generated review of your patch. The human sending this
> email has considered the AI review valid, or at least plausible.
>
> For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
> ---
> net: liquidio: Fix off-by-one error in setup_nic_devices() cleanup
>
> This patch changes the cleanup loop from while(i--) to do{}while(i--)
> to ensure that when a device fails to initialize, it gets cleaned up
> along with all previously initialized devices.
>
> > diff --git a/drivers/net/ethernet/cavium/liquidio/lio_main.c b/drivers/net/ethernet/cavium/liquidio/lio_main.c
> > index 0732440eeacd..3ba2806f5d1e 100644
> > --- a/drivers/net/ethernet/cavium/liquidio/lio_main.c
> > +++ b/drivers/net/ethernet/cavium/liquidio/lio_main.c
> > @@ -3765,11 +3765,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--);
>
> In lio_main.c, setup_nic_devices() can also reach setup_nic_dev_free from
> the devlink_alloc() failure path after the main for loop completes. At that
> point i equals ifcount (the loop counter has been incremented past the last
> valid device).
>
> With the do-while change, the first iteration will call
> liquidio_destroy_nic_device(octeon_dev, ifcount) for a device index that
> was never initialized. The function has a guard for NULL netdev so it will
> not crash, but it will print a spurious error message about a device that
> never existed.
>
> The fix works correctly for failures within the loop (which is the case
> described in the commit message), but is there a way to handle the
> post-loop devlink_alloc() failure case without printing the spurious
> error message?
Sorry for not realising this when I made this suggestion in
my review of v1.
Also, I would suggest splitting this patch in two: one patch
per driver.
On Mon, Jan 26, 2026 at 03:56:07PM +0000, Simon Horman wrote:
> On Mon, Jan 26, 2026 at 03:50:39PM +0000, Simon Horman wrote:
> > This is an AI-generated review of your patch. The human sending this
> > email has considered the AI review valid, or at least plausible.
> >
> > For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
> > ---
> > net: liquidio: Fix off-by-one error in setup_nic_devices() cleanup
> >
> > This patch changes the cleanup loop from while(i--) to do{}while(i--)
> > to ensure that when a device fails to initialize, it gets cleaned up
> > along with all previously initialized devices.
> >
> > > diff --git a/drivers/net/ethernet/cavium/liquidio/lio_main.c b/drivers/net/ethernet/cavium/liquidio/lio_main.c
> > > index 0732440eeacd..3ba2806f5d1e 100644
> > > --- a/drivers/net/ethernet/cavium/liquidio/lio_main.c
> > > +++ b/drivers/net/ethernet/cavium/liquidio/lio_main.c
> > > @@ -3765,11 +3765,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--);
> >
> > In lio_main.c, setup_nic_devices() can also reach setup_nic_dev_free from
> > the devlink_alloc() failure path after the main for loop completes. At that
> > point i equals ifcount (the loop counter has been incremented past the last
> > valid device).
> >
> > With the do-while change, the first iteration will call
> > liquidio_destroy_nic_device(octeon_dev, ifcount) for a device index that
> > was never initialized. The function has a guard for NULL netdev so it will
> > not crash, but it will print a spurious error message about a device that
> > never existed.
> >
> > The fix works correctly for failures within the loop (which is the case
> > described in the commit message), but is there a way to handle the
> > post-loop devlink_alloc() failure case without printing the spurious
> > error message?
>
> Sorry for not realising this when I made this suggestion in
> my review of v1.
>
> Also, I would suggest splitting this patch in two: one patch
> per driver.
Hi Simon,
Apologies from my side as well; I overlooked the devlink_alloc failure
path myself.
As noted, the loop index i is indeed invalid at that point. I will fix
this by decrementing i in the error path to ensure it points to the last
successfully initialized device before cleanup.
I will also split this patch into two separate patches in v3 as requested.
Regards,
Zilin
© 2016 - 2026 Red Hat, Inc.