can: enable NAPI before starting the controller in two drivers
Subsystems (5)
v3 cover can: enable NAPI before starting the controller in two drivers
Hi,
ctucanfd and xilinx_can open the device in the order "start the controller and unmask its RX interrupt, then napi_enable()". Each of them has an ISR that masks the RX interrupt before calling napi_schedule(), and a poll that is the only place the RX interrupt is unmasked again. A frame that arrives between the controller start and napi_enable() is handled by the ISR, which masks RX and calls napi_schedule() on a NAPI instance that is still disabled. napi_schedule_prep() sees NAPI_STATE_SCHED set and drops the schedule. napi_enable() clears SCHED without scheduling. Nothing ever polls, so RX stays masked and the interface receives no data frames until the next close. Transmit keeps working. ctucanfd counts no errors, so the failure is silent there. xilinx_can counts RX FIFO overflows once the FIFO fills.
The fix is the same in both: call napi_enable() before the controller start, so that an early RX interrupt can schedule the poll, and call napi_disable() on the error path if the start fails.
Patch 1 (ctucanfd) was tested on QEMU with the ctucan_pci device. Patch 2 (xilinx_can) was tested on a Versal board.
The bug has been present since each driver was added, so each patch has a Fixes: tag pointing at the commit that added the driver, and both are marked for stable backport. The patches are independent and can be applied separately.
I looked at the other NAPI users under drivers/net/can. at91_can had the same persistent-mask pattern in 6.6 but no longer masks RX in its ISR since the conversion to rx-offload, so I left it alone; ti_hecc and virtio_can start the device before napi_enable() but do not leave RX masked, so a dropped first schedule only delays the first frame. ifi_canfd has the same race; see the changes in v3. All the others enable NAPI before the device can interrupt.
Changes in v3:
- Drop the ifi_canfd patch. The netdev AI review of v2 showed that a safe fix has to reorder the reset, error counter and interrupt mask writes in ifi_canfd_start(), on the open and the bus-off restart paths. That change is no longer obviously correct by inspection, and I cannot test it without IFI CAN FD hardware. The race in ifi_canfd_open() remains.
- Patch 1: no changes
- Patch 2: add Reviewed-by from Harini
- v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Patch 1: add Acked-by and Tested-by from Pavel
- Patch 2: correct the commit message, which said no error counter moves during the stall; the RX overflow interrupt stays enabled and counts rx_over_errors
- Patch 3: split the reset out of ifi_canfd_start() and enable NAPI after it, with synchronize_irq() in between, so the poll cannot race with the reset (reported by the netdev AI review). This replaces the ifi_canfd_stop() call proposed in my reply to that review; see the patch notes
- Patches 2 and 3: name the related fixes by subject
- Rebased on current linux-can main
- v1: https://lore.kernel.org/r/[email protected]
Patrick Zhang (2):
can: ctucanfd: enable NAPI before starting the controller
can: xilinx_can: enable NAPI before starting the controller
drivers/net/can/ctucanfd/ctucanfd_base.c | 6 +++++- drivers/net/can/xilinx_can.c | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-)
base-commit: abeacca61eea4f6803df4a595b656ea45c2d3249
Signature · 1 line
2.50.1
PATCH v3 1/2 can: ctucanfd: enable NAPI before starting the controller
ctucan_open() starts the controller and unmasks RBNEI in ctucan_chip_start(), prints "ctu_can_fd device registered", and only then calls napi_enable(). A frame that arrives in that window raises RBNEI. ctucan_interrupt() masks RBNEI and calls napi_schedule(). NAPI is still disabled, so napi_schedule_prep() finds NAPI_STATE_SCHED set and the schedule is dropped. napi_enable() then clears SCHED without scheduling anything. Only ctucan_rx_poll() unmasks RBNEI again, and it never runs. The interface receives nothing until the next close. Transmit still works. The RX FIFO fills up and the core sets DOR, but the overrun is only accounted in ctucan_rx_poll(), so rx_errors and rx_over_errors stay at zero and nothing is logged. Every re-open (ip link down/up, a driver rebind) runs the same race again.
The netdev_info() in the window makes it much wider on a slow console. This can be reproduced by sending continuous traffic with cangen from another node and repeating "ip link set down/up" on the test device.
Enable NAPI before the controller starts, so that an early RX interrupt can schedule the poll. NAPI can be enabled while the controller is in reset, because nothing schedules it until an interrupt arrives. If ctucan_chip_start() fails, disable NAPI again. Otherwise the next open would call napi_enable() on an already enabled instance and trip the BUG_ON() in napi_enable_locked().
Tested on QEMU with the ctucan_pci device.
---
Notes:
v3:
- No changes
- v2: https://lore.kernel.org/r/[email protected]
v2:
- Add Acked-by and Tested-by from Pavel
- v1: https://lore.kernel.org/r/[email protected]
drivers/net/can/ctucanfd/ctucanfd_base.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/can/ctucanfd/ctucanfd_base.c +5 -1
@@ -1231,6 +1231,10 @@ static int ctucan_open(struct net_device *ndev) goto err_irq; } +/* Keep before ctucan_chip_start(): the ISR masks RBNEI and only the +* poll unmasks it. +*/ +napi_enable (&priv->napi); ret =ctucan_chip_start (ndev);if (ret <0 ) {netdev_err (ndev,"ctucan_chip_start failed!\n" );
@@ -1238,12 +1242,12 @@ static int ctucan_open(struct net_device *ndev) }netdev_info (ndev,"ctu_can_fd device registered\n" ); -napi_enable (&priv->napi);netif_start_queue (ndev);return 0 ; err_chip_start: +napi_disable (&priv->napi);free_irq (ndev->irq, ndev); err_irq:close_candev (ndev);
Signature · 1 line
2.50.1
PATCH v3 2/2 can: xilinx_can: enable NAPI before starting the controller
In reply to Patrick Zhangxcan_open() calls xcan_chip_start(), which writes XCAN_IER_OFFSET to unmask the RX interrupts and then sets XCAN_SRR_CEN_MASK to enable the controller, and only then calls napi_enable(). A frame that arrives in that window raises the RX interrupt. xcan_interrupt() clears the RX bits in XCAN_IER_OFFSET and calls napi_schedule(). NAPI is still disabled, so napi_schedule_prep() finds NAPI_STATE_SCHED set and the schedule is dropped. Only xcan_rx_poll() unmasks RX again, and it never runs. The interface receives no data frames until the next close. Transmit still works. The RX overflow interrupt stays enabled, so once the RX FIFO fills, xcan_err_interrupt() counts rx_over_errors and reports overflow error frames, but nothing reads the FIFO. Every re-open (ip link down/up, a driver rebind) runs the same race again.
The window is only the tail of xcan_chip_start(), so the failure is rare. Adding an mdelay(50) at the end of xcan_chip_start() makes it reproducible with continuous traffic from another node and a single "ip link set down/up" on the test device.
Enable NAPI before the controller starts, so that an early RX interrupt can schedule the poll. NAPI can be enabled while the controller is in reset, because nothing schedules it until an interrupt arrives. If xcan_chip_start() fails, disable NAPI again. Otherwise the next open would call napi_enable() on an already enabled instance and trip the BUG_ON() in napi_enable_locked().
This is the same race as in "can: ctucanfd: enable NAPI before starting the controller".
Tested on a Versal board.
---
Notes:
v3:
- Add Reviewed-by from Harini
- v2: https://lore.kernel.org/r/[email protected]
v2:
- Correct the commit message: the RX overflow interrupt stays enabled during the stall, so rx_over_errors is counted once the FIFO fills
- Name the ctucanfd fix by subject instead of "the previous patch"
- v1: https://lore.kernel.org/r/[email protected]
drivers/net/can/xilinx_can.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/can/xilinx_can.c +5 -1
@@ -1570,18 +1570,22 @@ static int xcan_open(struct net_device *ndev)if (ret) goto err_irq; +/* Keep before xcan_chip_start(): the ISR masks RX interrupts and only +* the poll unmasks them. +*/ +napi_enable (&priv->napi); ret =xcan_chip_start (ndev);if (ret <0 ) {netdev_err (ndev,"xcan_chip_start failed!\n" ); goto err_candev; } -napi_enable (&priv->napi);netif_start_queue (ndev);return 0 ; err_candev: +napi_disable (&priv->napi);close_candev (ndev); err_irq:free_irq (ndev->irq, ndev);
Signature · 1 line
2.50.1