Skip to contentSkip to conversation

can: mcp251xfd: fix infinite loop when xmit fails

PATCH Superseded Vitor Soares 1 participant 2 messages Mar 8, 2024

Subsystems (5)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 can: mcp251xfd: fix infinite loop when xmit fails

From: Vitor Soares <[email protected]>

When the mcp251xfd_start_xmit() function fails, the driver stops processing messages and the interrupt routine does not return, running indefinitely even after killing the running application.

Error messages:

[  441.298819] mcp251xfd spi2.0 can0: ERROR in mcp251xfd_start_xmit: -16
[  441.306498] mcp251xfd spi2.0 can0: Transmit Event FIFO buffer not empty. (seq=0x000017c7, tef_tail=0x000017cf, tef_head=0x000017d0, tx_head=0x000017d3).

... and repeat forever.

The issue can be triggered when multiple devices share the same SPI interface. And there is concurrent access to the bus.

The problem occurs because tx_ring->head increments even if mcp251xfd_start_xmit() fails. Consequently, the driver skips one TX package while still expecting a response in mcp251xfd_handle_tefif_one().

This patch resolves the issue by decreasing tx_ring->head if mcp251xfd_start_xmit() fails. With the fix, if we trigger the issue and the err = -EBUSY, the driver returns NETDEV_TX_BUSY. The network stack retries to transmit the message.
Otherwise, it prints an error and discards the message.

Fixes 55e5b97f003e ("can: mcp25xxfd: add driver for Microchip MCP25xxFD SPI CAN")

---

V1->V2:
- Return NETDEV_TX_BUSY if mcp251xfd_tx_obj_write() == -EBUSY
- Rework the commit message to address the change above
- Change can_put_echo_skb() to be called after mcp251xfd_tx_obj_write() succeed. Otherwise, we get Kernel NULL pointer dereference error.

 drivers/net/can/spi/mcp251xfd/mcp251xfd-tx.c | 29 +++++++++++---------
 1 file changed, 16 insertions(+), 13 deletions(-)
drivers/net/can/spi/mcp251xfd/mcp251xfd-tx.c +16 -13
index 160528d3cc26..0fdaececebdd 100644
@@ -181,25 +181,28 @@ netdev_tx_t mcp251xfd_start_xmit(struct sk_buff *skb,
 	tx_obj = mcp251xfd_get_tx_obj_next(tx_ring);
 	mcp251xfd_tx_obj_from_skb(priv, tx_obj, skb, tx_ring->head);
 
-	/* Stop queue if we occupy the complete TX FIFO */
 	tx_head = mcp251xfd_get_tx_head(tx_ring);
-	tx_ring->head++;
-	if (mcp251xfd_get_tx_free(tx_ring) == 0)
-		netif_stop_queue(ndev);
-
 	frame_len = can_skb_get_frame_len(skb);
-	err = can_put_echo_skb(skb, ndev, tx_head, frame_len);
-	if (!err)
-		netdev_sent_queue(priv->ndev, frame_len);
+
+	tx_ring->head++;
 
 	err = mcp251xfd_tx_obj_write(priv, tx_obj);
-	if (err)
-		goto out_err;
+	if (err) {
+		tx_ring->head--;
 
-	return NETDEV_TX_OK;
+		if (err == -EBUSY)
+			return NETDEV_TX_BUSY;
 
- out_err:
-	netdev_err(priv->ndev, "ERROR in %s: %d\n", __func__, err);
+		netdev_err(priv->ndev, "ERROR in %s: %d\n", __func__, err);
+	} else {
+		can_put_echo_skb(skb, ndev, tx_head, frame_len);
+
+		/* Stop queue if we occupy the complete TX FIFO */
+		if (mcp251xfd_get_tx_free(tx_ring) == 0)
+			netif_stop_queue(ndev);
+
+		netdev_sent_queue(priv->ndev, frame_len);
+	}
 
 	return NETDEV_TX_OK;
 }
Signature · 1 line
2.34.1
author
#1 lore raw
Quoted 71 lines · Vitor Soares

From: Vitor Soares <[email protected]>

When the mcp251xfd_start_xmit() function fails, the driver stops processing messages and the interrupt routine does not return, running indefinitely even after killing the running application.

Error messages:
[  441.298819] mcp251xfd spi2.0 can0: ERROR in mcp251xfd_start_xmit:
-16
[  441.306498] mcp251xfd spi2.0 can0: Transmit Event FIFO buffer not empty. (seq=0x000017c7, tef_tail=0x000017cf, tef_head=0x000017d0, tx_head=0x000017d3).
... and repeat forever.

The issue can be triggered when multiple devices share the same SPI interface. And there is concurrent access to the bus.

The problem occurs because tx_ring->head increments even if mcp251xfd_start_xmit() fails. Consequently, the driver skips one TX package while still expecting a response in mcp251xfd_handle_tefif_one().

This patch resolves the issue by decreasing tx_ring->head if mcp251xfd_start_xmit() fails. With the fix, if we trigger the issue and
the err = -EBUSY, the driver returns NETDEV_TX_BUSY. The network stack
retries to transmit the message.
Otherwise, it prints an error and discards the message.

Fixes 55e5b97f003e ("can: mcp25xxfd: add driver for Microchip

MCP25xxFD SPI CAN")

---

V1->V2:
  - Return NETDEV_TX_BUSY if mcp251xfd_tx_obj_write() == -EBUSY
  - Rework the commit message to address the change above
  - Change can_put_echo_skb() to be called after mcp251xfd_tx_obj_write() succeed. Otherwise, we get Kernel NULL pointer dereference error.

 drivers/net/can/spi/mcp251xfd/mcp251xfd-tx.c | 29 +++++++++++------- --

 1 file changed, 16 insertions(+), 13 deletions(-)
drivers/net/can/spi/mcp251xfd/mcp251xfd-tx.c +0 -0
index 160528d3cc26..0fdaececebdd 100644
@@ -181,25 +181,28 @@ netdev_tx_t mcp251xfd_start_xmit(struct sk_buff
 *skb,
         tx_obj = mcp251xfd_get_tx_obj_next(tx_ring);
         mcp251xfd_tx_obj_from_skb(priv, tx_obj, skb, tx_ring->head);


-       /* Stop queue if we occupy the complete TX FIFO */ tx_head = mcp251xfd_get_tx_head(tx_ring);
-       tx_ring->head++;
-       if (mcp251xfd_get_tx_free(tx_ring) == 0)
-               netif_stop_queue(ndev);
-
        frame_len = can_skb_get_frame_len(skb);
-       err = can_put_echo_skb(skb, ndev, tx_head, frame_len); -       if (!err)
-               netdev_sent_queue(priv->ndev, frame_len); +
+       tx_ring->head++;

        err = mcp251xfd_tx_obj_write(priv, tx_obj);

Quoted 8 lines

-       if (err)
-               goto out_err;
+       if (err) {
+               tx_ring->head--;

-       return NETDEV_TX_OK;
+               if (err == -EBUSY)
+                       return NETDEV_TX_BUSY;

Missing the stats for dropped packages. I will add on v3.

Quoted 7 lines


- out_err:
-       netdev_err(priv->ndev, "ERROR in %s: %d\n", __func__, err); +               netdev_err(priv->ndev, "ERROR in %s: %d\n", __func__, err);
+       } else {
+               can_put_echo_skb(skb, ndev, tx_head, frame_len);

Not sure if it is save to call this here.

drivers/net/can/spi/mcp251xfd/mcp251xfd-tx.c netdev_tx_t mcp251xfd_start_xmit(struct sk_buff *skb, · line 204

Quoted 6 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-tx.c +6 -0
+
+               /* Stop queue if we occupy the complete TX FIFO */
+               if (mcp251xfd_get_tx_free(tx_ring) == 0)
+                       netif_stop_queue(ndev);
+
+               netdev_sent_queue(priv->ndev, frame_len);

This is not correct. Should be called only if can_put_echo_skb() succeed. I will fix this in v3.

Quoted 4 lines

+       }

        return NETDEV_TX_OK;
 }

Best regards,
Vitor Soares