can: mcp251xfd: fix TEF vs TX race condition introduced in UINC improvements
Subsystems (2)
Marc Kleine-Budde
maintainercover can: mcp251xfd: fix TEF vs TX race condition introduced in UINC improvements
Hello,
during the TEF readout UINC improvements, I introduced a TEF vs TX race condition to the driver. The first patch fixes the problem. The second patch fixes the same issue in the RX-path, which causes no problem, but now the code path look the same.
regards,
Marc
Marc Kleine-Budde
maintainerPATCH 1/2 can: mcp251xfd: mcp251xfd_handle_tefif(): fix TEF vs. TX race condition
The mcp251xfd driver uses a TX FIFO for sending CAN frames and a TX Event FIFO (TEF) for completed TX-requests.
The TEF event handling in the mcp251xfd_handle_tefif() function has a race condition. It first increments the tx-ring's tail counter to signal that there's room in the TX and TEF FIFO, then it increments the TEF FIFO in hardware.
A running mcp251xfd_start_xmit() on a different CPU might not stop the txqueue (as the tx-ring still shows free space). The next mcp251xfd_start_xmit() will push a message into the chip and the TX complete event might overflow the TEF FIFO.
This patch changes the order to fix the problem.
--- drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +4 -5
@@ -1368,13 +1368,10 @@ static int mcp251xfd_handle_tefif(struct mcp251xfd_priv *priv)struct mcp251xfd_tx_ring *tx_ring = priv->tx;struct spi_transfer *last_xfer; - tx_ring->tail += len; -/* Increment the TEF FIFO tail pointer 'len' times in * a single SPI message. -*/ - -/* Note: +* +* Note: * * "cs_change == 1" on the last transfer results in an * active chip select after the complete SPI
@@ -1391,6 +1388,8 @@ static int mcp251xfd_handle_tefif(struct mcp251xfd_priv *priv)if (err)return err; + tx_ring->tail += len; + err =mcp251xfd_check_tef_tail (priv);if (err)return err;
Signature · 1 line
2.29.2
Marc Kleine-Budde
maintainerPATCH 2/2 can: mcp251xfd: mcp251xfd_handle_rxif_ring(): first increment RX tail pointer in HW, then in driver
In reply to Marc Kleine-BuddeThe previous patch fixes a TEF vs. TX race condition, by first updating the TEF tail pointer in hardware, and then updating the driver internal pointer.
The same pattern exists in the RX-path, too. This should be no problem, as the driver accesses the RX-FIFO from the interrupt handler only, thus the access is properly serialized. Fix the order here, too, so that the TEF- and RX-path look similar.
--- drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +4 -4
@@ -1552,10 +1552,8 @@ mcp251xfd_handle_rxif_ring(struct mcp251xfd_priv *priv,/* Increment the RX FIFO tail pointer 'len' times in a * single SPI message. -*/ - ring->tail += len; - -/* Note: +* +* Note: * * "cs_change == 1" on the last transfer results in an * active chip select after the complete SPI
@@ -1571,6 +1569,8 @@ mcp251xfd_handle_rxif_ring(struct mcp251xfd_priv *priv, last_xfer->cs_change =1 ;if (err)return err; + + ring->tail += len; }return 0 ;
Signature · 1 line
2.29.2