linux-stable/drivers/net/can
Marc Kleine-Budde d7a220bf6b can: dev: can_restart(): fix race condition between controller restart and netif_carrier_on()
[ Upstream commit 6841cab8c4 ]

This race condition was discovered while updating the at91_can driver
to use can_bus_off(). The following scenario describes how the
converted at91_can driver would behave.

When a CAN device goes into BUS-OFF state, the driver usually
stops/resets the CAN device and calls can_bus_off().

This function sets the netif carrier to off, and (if configured by
user space) schedules a delayed work that calls can_restart() to
restart the CAN device.

The can_restart() function first checks if the carrier is off and
triggers an error message if the carrier is OK.

Then it calls the driver's do_set_mode() function to restart the
device, then it sets the netif carrier to on. There is a race window
between these two calls.

The at91 CAN controller (observed on the sama5d3, a single core 32 bit
ARM CPU) has a hardware limitation. If the device goes into bus-off
while sending a CAN frame, there is no way to abort the sending of
this frame. After the controller is enabled again, another attempt is
made to send it.

If the bus is still faulty, the device immediately goes back to the
bus-off state. The driver calls can_bus_off(), the netif carrier is
switched off and another can_restart is scheduled. This occurs within
the race window before the original can_restart() handler marks the
netif carrier as OK. This would cause the 2nd can_restart() to be
called with an OK netif carrier, resulting in an error message.

The flow of the 1st can_restart() looks like this:

can_restart()
    // bail out if netif_carrier is OK

    netif_carrier_ok(dev)
    priv->do_set_mode(dev, CAN_MODE_START)
        // enable CAN controller
        // sama5d3 restarts sending old message

        // CAN devices goes into BUS_OFF, triggers IRQ

// IRQ handler start
    at91_irq()
        at91_irq_err_line()
            can_bus_off()
                netif_carrier_off()
                schedule_delayed_work()
// IRQ handler end

    netif_carrier_on()

The 2nd can_restart() will be called with an OK netif carrier and the
error message will be printed.

To close the race window, first set the netif carrier to on, then
restart the controller. In case the restart fails with an error code,
roll back the netif carrier to off.

Fixes: 39549eef35 ("can: CAN Network device driver and Netlink interface")
Link: https://lore.kernel.org/all/20231005-can-dev-fix-can-restart-v2-2-91b5c1fd922c@pengutronix.de
Reviewed-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2023-11-20 11:06:45 +01:00
..
c_can
cc770 can: cc770: cc770_isa_probe(): add missing free_cc770dev() 2022-12-08 11:23:56 +01:00
dev can: dev: can_restart(): fix race condition between controller restart and netif_carrier_on() 2023-11-20 11:06:45 +01:00
ifi_canfd
m_can can: tcan4x5x: Remove invalid write in clear_interrupts 2023-01-14 10:15:44 +01:00
mscan
peak_canfd
rcar
sja1000
softing
spi
usb can: gs_usb: gs_usb_receive_bulk_callback(): count RX overflow errors also in case of OOM 2023-09-19 12:20:08 +02:00
at91_can.c
flexcan.c
grcan.c
janz-ican3.c
Kconfig
kvaser_pciefd.c can: kvaser_pciefd: Disable interrupts in probe error path 2023-05-30 12:57:54 +01:00
led.c
Makefile
pch_can.c
slcan.c
sun4i_can.c
ti_hecc.c
vcan.c
vxcan.c net: validate veth and vxcan peer ifindexes 2023-08-30 16:23:13 +02:00
xilinx_can.c