Skip to contentSkip to conversation

Drop Tx network packet when Tx TmFIFO is full

PATCH Superseded Liming Sun 3 participants 11 messages Jan 4, 2024 – Jan 22, 2024

Subsystems (2)
lore
Share & tools
lore

Liming Sun

author
#0 lore raw

PATCH 1/1 Drop Tx network packet when Tx TmFIFO is full

Starting from Linux 5.16 kernel, Tx timeout mechanism was added into the virtio_net driver which prints the "Tx timeout" message when a packet is stuck in Tx queue for too long which could happen when external host driver is stuck or stopped and failed to read the FIFO.

Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

To avoid such "Tx timeout" messages, this commit adds a timeout mechanism to drop and release the pending Tx packet if not able to transmit for two seconds due to Tx FIFO full.

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 67 ++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
drivers/platform/mellanox/mlxbf-tmfifo.c +67 -0
index 5c683b4eaf10..47ed2a6027a6 100644
@@ -47,6 +47,9 @@
 /* Message with data needs at least two words (for header & data). */
 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
 
+/* Tx timeout in milliseconds. */
+#define TMFIFO_TX_TIMEOUT			2000
+
 /* ACPI UID for BlueField-3. */
 #define TMFIFO_BF3_UID				1
 
@@ -62,12 +65,14 @@ struct mlxbf_tmfifo;
  * @drop_desc: dummy desc for packet dropping
  * @cur_len: processed length of the current descriptor
  * @rem_len: remaining length of the pending packet
+ * @rem_padding: remaining bytes to send as paddings
  * @pkt_len: total length of the pending packet
  * @next_avail: next avail descriptor id
  * @num: vring size (number of descriptors)
  * @align: vring alignment size
  * @index: vring index
  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
+ * @tx_timeout: expire time of last tx packet
  * @fifo: pointer to the tmfifo structure
  */
 struct mlxbf_tmfifo_vring {
@@ -79,12 +84,14 @@ struct mlxbf_tmfifo_vring {
 	struct vring_desc drop_desc;
 	int cur_len;
 	int rem_len;
+	int rem_padding;
 	u32 pkt_len;
 	u16 next_avail;
 	int num;
 	int align;
 	int index;
 	int vdev_id;
+	unsigned long tx_timeout;
 	struct mlxbf_tmfifo *fifo;
 };
 
@@ -819,6 +826,50 @@ static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,
 	return true;
 }
 
+static void mlxbf_tmfifo_check_tx_timeout(struct mlxbf_tmfifo_vring *vring)
+{
+	unsigned long flags;
+
+	/* Only handle Tx timeout for network vdev. */
+	if (vring->vdev_id != VIRTIO_ID_NET)
+		return;
+
+	/* Initialize the timeout or return if not expired. */
+	if (!vring->tx_timeout) {
+		/* Initialize the timeout. */
+		vring->tx_timeout = jiffies +
+			msecs_to_jiffies(TMFIFO_TX_TIMEOUT);
+		return;
+	} else if (time_before(jiffies, vring->tx_timeout)) {
+		/* Return if not timeout yet. */
+		return;
+	}
+
+	/*
+         * Drop the packet after timeout. The outstanding packet is
+         * released and the remaining bytes will be sent with padding byte 0x00
+         * as a recovery. On the peer(host) side, the padding bytes 0x00 will be
+         * either dropped directly, or appended into existing outstanding packet
+         * thus dropped as corrupted network packet.
+         */
+	vring->rem_padding = round_up(vring->rem_len, sizeof(u64));
+	mlxbf_tmfifo_release_pkt(vring);
+	vring->cur_len = 0;
+	vring->rem_len = 0;
+	vring->fifo->vring[0] = NULL;
+
+	/*
+	 * Make sure the load/store are in order before
+	 * returning back to virtio.
+	 */
+	virtio_mb(false);
+
+	/* Notify upper layer. */
+	spin_lock_irqsave(&vring->fifo->spin_lock[0], flags);
+	vring_interrupt(0, vring->vq);
+	spin_unlock_irqrestore(&vring->fifo->spin_lock[0], flags);
+}
+
 /* Rx & Tx processing of a queue. */
 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 {
@@ -841,6 +892,7 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 		return;
 
 	do {
+retry:
 		/* Get available FIFO space. */
 		if (avail == 0) {
 			if (is_rx)
@@ -851,6 +903,17 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 				break;
 		}
 
+		/* Insert paddings for discarded Tx packet. */
+		if (!is_rx) {
+			vring->tx_timeout = 0;
+			while (vring->rem_padding >= sizeof(u64)) {
+				writeq(0, vring->fifo->tx.data);
+				vring->rem_padding -= sizeof(u64);
+				if (--avail == 0)
+					goto retry;
+			}
+		}
+
 		/* Console output always comes from the Tx buffer. */
 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
 			mlxbf_tmfifo_console_tx(fifo, avail);
@@ -860,6 +923,10 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 		/* Handle one descriptor. */
 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
 	} while (more);
+
+	/* Check Tx timeout. */
+	if (avail <= 0 && !is_rx)
+		mlxbf_tmfifo_check_tx_timeout(vring);
 }
 
 /* Handle Rx or Tx queues. */
Signature · 1 line
2.30.1
author
#1 lore raw

PATCH v2 1/1 Drop Tx network packet when Tx TmFIFO is full

Starting from Linux 5.16 kernel, Tx timeout mechanism was added into the virtio_net driver which prints the "Tx timeout" message when a packet is stuck in Tx queue for too long which could happen when external host driver is stuck or stopped and failed to read the FIFO.

Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

To avoid such "Tx timeout" messages, this commit adds a timeout mechanism to drop and release the pending Tx packet if not able to transmit for two seconds due to Tx FIFO full.

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 67 ++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
drivers/platform/mellanox/mlxbf-tmfifo.c +67 -0
index 5c683b4eaf10..f39b7b9d2bfe 100644
@@ -47,6 +47,9 @@
 /* Message with data needs at least two words (for header & data). */
 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
 
+/* Tx timeout in milliseconds. */
+#define TMFIFO_TX_TIMEOUT			2000
+
 /* ACPI UID for BlueField-3. */
 #define TMFIFO_BF3_UID				1
 
@@ -62,12 +65,14 @@ struct mlxbf_tmfifo;
  * @drop_desc: dummy desc for packet dropping
  * @cur_len: processed length of the current descriptor
  * @rem_len: remaining length of the pending packet
+ * @rem_padding: remaining bytes to send as paddings
  * @pkt_len: total length of the pending packet
  * @next_avail: next avail descriptor id
  * @num: vring size (number of descriptors)
  * @align: vring alignment size
  * @index: vring index
  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
+ * @tx_timeout: expire time of last tx packet
  * @fifo: pointer to the tmfifo structure
  */
 struct mlxbf_tmfifo_vring {
@@ -79,12 +84,14 @@ struct mlxbf_tmfifo_vring {
 	struct vring_desc drop_desc;
 	int cur_len;
 	int rem_len;
+	int rem_padding;
 	u32 pkt_len;
 	u16 next_avail;
 	int num;
 	int align;
 	int index;
 	int vdev_id;
+	unsigned long tx_timeout;
 	struct mlxbf_tmfifo *fifo;
 };
 
@@ -819,6 +826,50 @@ static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,
 	return true;
 }
 
+static void mlxbf_tmfifo_check_tx_timeout(struct mlxbf_tmfifo_vring *vring)
+{
+	unsigned long flags;
+
+	/* Only handle Tx timeout for network vdev. */
+	if (vring->vdev_id != VIRTIO_ID_NET)
+		return;
+
+	/* Initialize the timeout or return if not expired. */
+	if (!vring->tx_timeout) {
+		/* Initialize the timeout. */
+		vring->tx_timeout = jiffies +
+			msecs_to_jiffies(TMFIFO_TX_TIMEOUT);
+		return;
+	} else if (time_before(jiffies, vring->tx_timeout)) {
+		/* Return if not timeout yet. */
+		return;
+	}
+
+	/*
+	 * Drop the packet after timeout. The outstanding packet is
+	 * released and the remaining bytes will be sent with padding byte 0x00
+	 * as a recovery. On the peer(host) side, the padding bytes 0x00 will be
+	 * either dropped directly, or appended into existing outstanding packet
+	 * thus dropped as corrupted network packet.
+	 */
+	vring->rem_padding = round_up(vring->rem_len, sizeof(u64));
+	mlxbf_tmfifo_release_pkt(vring);
+	vring->cur_len = 0;
+	vring->rem_len = 0;
+	vring->fifo->vring[0] = NULL;
+
+	/*
+	 * Make sure the load/store are in order before
+	 * returning back to virtio.
+	 */
+	virtio_mb(false);
+
+	/* Notify upper layer. */
+	spin_lock_irqsave(&vring->fifo->spin_lock[0], flags);
+	vring_interrupt(0, vring->vq);
+	spin_unlock_irqrestore(&vring->fifo->spin_lock[0], flags);
+}
+
 /* Rx & Tx processing of a queue. */
 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 {
@@ -841,6 +892,7 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 		return;
 
 	do {
+retry:
 		/* Get available FIFO space. */
 		if (avail == 0) {
 			if (is_rx)
@@ -851,6 +903,17 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 				break;
 		}
 
+		/* Insert paddings for discarded Tx packet. */
+		if (!is_rx) {
+			vring->tx_timeout = 0;
+			while (vring->rem_padding >= sizeof(u64)) {
+				writeq(0, vring->fifo->tx.data);
+				vring->rem_padding -= sizeof(u64);
+				if (--avail == 0)
+					goto retry;
+			}
+		}
+
 		/* Console output always comes from the Tx buffer. */
 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
 			mlxbf_tmfifo_console_tx(fifo, avail);
@@ -860,6 +923,10 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 		/* Handle one descriptor. */
 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
 	} while (more);
+
+	/* Check Tx timeout. */
+	if (avail <= 0 && !is_rx)
+		mlxbf_tmfifo_check_tx_timeout(vring);
 }
 
 /* Handle Rx or Tx queues. */
Signature · 1 line
2.30.1
maintainer
#2 lore raw
Quoted 16 lines · Liming Sun

Starting from Linux 5.16 kernel, Tx timeout mechanism was added into the virtio_net driver which prints the "Tx timeout" message when a packet is stuck in Tx queue for too long which could happen when external host driver is stuck or stopped and failed to read the FIFO.

Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

To avoid such "Tx timeout" messages, this commit adds a timeout mechanism to drop and release the pending Tx packet if not able to transmit for two seconds due to Tx FIFO full.

Quoted 8 lines

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

This doesn't really explain how it helps (other than avoiding the message which sounds like just hiding the issue). That is, how this helps to resume Tx? Or does Tx resume? There's nothing to indicate either way.

Signature · 1 line
 i.
Quoted 137 lines
---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 67 ++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
drivers/platform/mellanox/mlxbf-tmfifo.c +67 -0
index 5c683b4eaf10..f39b7b9d2bfe 100644
@@ -47,6 +47,9 @@
 /* Message with data needs at least two words (for header & data). */
 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
 
+/* Tx timeout in milliseconds. */
+#define TMFIFO_TX_TIMEOUT			2000
+
 /* ACPI UID for BlueField-3. */
 #define TMFIFO_BF3_UID				1
 
@@ -62,12 +65,14 @@ struct mlxbf_tmfifo;
  * @drop_desc: dummy desc for packet dropping
  * @cur_len: processed length of the current descriptor
  * @rem_len: remaining length of the pending packet
+ * @rem_padding: remaining bytes to send as paddings
  * @pkt_len: total length of the pending packet
  * @next_avail: next avail descriptor id
  * @num: vring size (number of descriptors)
  * @align: vring alignment size
  * @index: vring index
  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
+ * @tx_timeout: expire time of last tx packet
  * @fifo: pointer to the tmfifo structure
  */
 struct mlxbf_tmfifo_vring {
@@ -79,12 +84,14 @@ struct mlxbf_tmfifo_vring {
 	struct vring_desc drop_desc;
 	int cur_len;
 	int rem_len;
+	int rem_padding;
 	u32 pkt_len;
 	u16 next_avail;
 	int num;
 	int align;
 	int index;
 	int vdev_id;
+	unsigned long tx_timeout;
 	struct mlxbf_tmfifo *fifo;
 };
 
@@ -819,6 +826,50 @@ static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,
 	return true;
 }
 
+static void mlxbf_tmfifo_check_tx_timeout(struct mlxbf_tmfifo_vring *vring)
+{
+	unsigned long flags;
+
+	/* Only handle Tx timeout for network vdev. */
+	if (vring->vdev_id != VIRTIO_ID_NET)
+		return;
+
+	/* Initialize the timeout or return if not expired. */
+	if (!vring->tx_timeout) {
+		/* Initialize the timeout. */
+		vring->tx_timeout = jiffies +
+			msecs_to_jiffies(TMFIFO_TX_TIMEOUT);
+		return;
+	} else if (time_before(jiffies, vring->tx_timeout)) {
+		/* Return if not timeout yet. */
+		return;
+	}
+
+	/*
+	 * Drop the packet after timeout. The outstanding packet is
+	 * released and the remaining bytes will be sent with padding byte 0x00
+	 * as a recovery. On the peer(host) side, the padding bytes 0x00 will be
+	 * either dropped directly, or appended into existing outstanding packet
+	 * thus dropped as corrupted network packet.
+	 */
+	vring->rem_padding = round_up(vring->rem_len, sizeof(u64));
+	mlxbf_tmfifo_release_pkt(vring);
+	vring->cur_len = 0;
+	vring->rem_len = 0;
+	vring->fifo->vring[0] = NULL;
+
+	/*
+	 * Make sure the load/store are in order before
+	 * returning back to virtio.
+	 */
+	virtio_mb(false);
+
+	/* Notify upper layer. */
+	spin_lock_irqsave(&vring->fifo->spin_lock[0], flags);
+	vring_interrupt(0, vring->vq);
+	spin_unlock_irqrestore(&vring->fifo->spin_lock[0], flags);
+}
+
 /* Rx & Tx processing of a queue. */
 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 {
@@ -841,6 +892,7 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 		return;
 
 	do {
+retry:
 		/* Get available FIFO space. */
 		if (avail == 0) {
 			if (is_rx)
@@ -851,6 +903,17 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 				break;
 		}
 
+		/* Insert paddings for discarded Tx packet. */
+		if (!is_rx) {
+			vring->tx_timeout = 0;
+			while (vring->rem_padding >= sizeof(u64)) {
+				writeq(0, vring->fifo->tx.data);
+				vring->rem_padding -= sizeof(u64);
+				if (--avail == 0)
+					goto retry;
+			}
+		}
+
 		/* Console output always comes from the Tx buffer. */
 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
 			mlxbf_tmfifo_console_tx(fifo, avail);
@@ -860,6 +923,10 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 		/* Handle one descriptor. */
 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
 	} while (more);
+
+	/* Check Tx timeout. */
+	if (avail <= 0 && !is_rx)
+		mlxbf_tmfifo_check_tx_timeout(vring);
 }
 
 /* Handle Rx or Tx queues. */
author
#3 lore raw
Quoted 38 lines

-----Original Message-----
From: Ilpo Järvinen <[email protected]>
Sent: Thursday, January 4, 2024 12:39 PM
To: Liming Sun <[email protected]>
Cc: Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Hans de Goede <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>; [email protected]; LKML <[email protected]>
Subject: Re: [PATCH v2 1/1] Drop Tx network packet when Tx TmFIFO is full

Quoted 23 lines · Liming Sun

Starting from Linux 5.16 kernel, Tx timeout mechanism was added into the virtio_net driver which prints the "Tx timeout" message when a packet is stuck in Tx queue for too long which could happen when external host driver is stuck or stopped and failed to read the FIFO.

Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

To avoid such "Tx timeout" messages, this commit adds a timeout mechanism to drop and release the pending Tx packet if not able to transmit for two seconds due to Tx FIFO full.

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

Quoted 5 lines
Quoted 1 line

This doesn't really explain how it helps (other than avoiding the message which sounds like just hiding the issue). That is, how this helps to resume Tx? Or does Tx resume? There's nothing to indicate either way.

As the commit message mentioned, the expired packet is discarded and the packet buffer is released (see changes of calling mlxbf_tmfifo_release_pkt()). The Tx will resume automatically once the FIFO space is available, such as when external host driver starts to drain the TMFIFO. No need for any other logic.

Quoted 151 lines

--
i.

Quoted 2 lines
---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 67

++++++++++++++++++++++++

Review

Quoted 3 lines
 1 file changed, 67 insertions(+)
(quoted diff) +0 -0

b/drivers/platform/mellanox/mlxbf-tmfifo.c

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct

Quoted 44 lines

index 5c683b4eaf10..f39b7b9d2bfe 100644

drivers/platform/mellanox/mlxbf-tmfifo.c +7 -0
@@ -47,6 +47,9 @@
 /* Message with data needs at least two words (for header & data). */
 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
 
+/* Tx timeout in milliseconds. */
+#define TMFIFO_TX_TIMEOUT			2000
+
 /* ACPI UID for BlueField-3. */
 #define TMFIFO_BF3_UID				1
 
@@ -62,12 +65,14 @@ struct mlxbf_tmfifo;
  * @drop_desc: dummy desc for packet dropping
  * @cur_len: processed length of the current descriptor
  * @rem_len: remaining length of the pending packet
+ * @rem_padding: remaining bytes to send as paddings
  * @pkt_len: total length of the pending packet
  * @next_avail: next avail descriptor id
  * @num: vring size (number of descriptors)
  * @align: vring alignment size
  * @index: vring index
  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
+ * @tx_timeout: expire time of last tx packet
  * @fifo: pointer to the tmfifo structure
  */
 struct mlxbf_tmfifo_vring {
@@ -79,12 +84,14 @@ struct mlxbf_tmfifo_vring {
 	struct vring_desc drop_desc;
 	int cur_len;
 	int rem_len;
+	int rem_padding;
 	u32 pkt_len;
 	u16 next_avail;
 	int num;
 	int align;
 	int index;
 	int vdev_id;
+	unsigned long tx_timeout;
 	struct mlxbf_tmfifo *fifo;
 };
 
@@ -819,6 +826,50 @@ static bool mlxbf_tmfifo_rxtx_one_desc(struct

mlxbf_tmfifo_vring *vring,

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 4 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
 	return true;
 }
 
+static void mlxbf_tmfifo_check_tx_timeout(struct mlxbf_tmfifo_vring

*vring)

Quoted 11 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -0
+{
+	unsigned long flags;
+
+	/* Only handle Tx timeout for network vdev. */
+	if (vring->vdev_id != VIRTIO_ID_NET)
+		return;
+
+	/* Initialize the timeout or return if not expired. */
+	if (!vring->tx_timeout) {
+		/* Initialize the timeout. */
+		vring->tx_timeout = jiffies +

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 10 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +10 -0
+			msecs_to_jiffies(TMFIFO_TX_TIMEOUT);
+		return;
+	} else if (time_before(jiffies, vring->tx_timeout)) {
+		/* Return if not timeout yet. */
+		return;
+	}
+
+	/*
+	 * Drop the packet after timeout. The outstanding packet is
+	 * released and the remaining bytes will be sent with padding byte

0x00

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct · line 852

Quoted 2 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +2 -0
+	 * as a recovery. On the peer(host) side, the padding bytes 0x00 will be
+	 * either dropped directly, or appended into existing outstanding

packet

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 24 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +20 -0
+	 * thus dropped as corrupted network packet.
+	 */
+	vring->rem_padding = round_up(vring->rem_len, sizeof(u64));
+	mlxbf_tmfifo_release_pkt(vring);
+	vring->cur_len = 0;
+	vring->rem_len = 0;
+	vring->fifo->vring[0] = NULL;
+
+	/*
+	 * Make sure the load/store are in order before
+	 * returning back to virtio.
+	 */
+	virtio_mb(false);
+
+	/* Notify upper layer. */
+	spin_lock_irqsave(&vring->fifo->spin_lock[0], flags);
+	vring_interrupt(0, vring->vq);
+	spin_unlock_irqrestore(&vring->fifo->spin_lock[0], flags);
+}
+
 /* Rx & Tx processing of a queue. */
 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 {
@@ -841,6 +892,7 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 8 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
 		return;
 
 	do {
+retry:
 		/* Get available FIFO space. */
 		if (avail == 0) {
 			if (is_rx)
@@ -851,6 +903,17 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 18 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -0
 				break;
 		}
 
+		/* Insert paddings for discarded Tx packet. */
+		if (!is_rx) {
+			vring->tx_timeout = 0;
+			while (vring->rem_padding >= sizeof(u64)) {
+				writeq(0, vring->fifo->tx.data);
+				vring->rem_padding -= sizeof(u64);
+				if (--avail == 0)
+					goto retry;
+			}
+		}
+
 		/* Console output always comes from the Tx buffer. */
 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
 			mlxbf_tmfifo_console_tx(fifo, avail);
@@ -860,6 +923,10 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

Quoted 11 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +4 -0
 		/* Handle one descriptor. */
 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
 	} while (more);
+
+	/* Check Tx timeout. */
+	if (avail <= 0 && !is_rx)
+		mlxbf_tmfifo_check_tx_timeout(vring);
 }
 
 /* Handle Rx or Tx queues. */
#4 lore raw

Hi,

Quoted 40 lines · Liming Sun
Quoted 40 lines

-----Original Message-----
From: Ilpo Järvinen <[email protected]>
Sent: Thursday, January 4, 2024 12:39 PM
To: Liming Sun <[email protected]>
Cc: Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Hans de Goede <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>; [email protected]; LKML <[email protected]>
Subject: Re: [PATCH v2 1/1] Drop Tx network packet when Tx TmFIFO is full

Quoted 24 lines · Liming Sun

Starting from Linux 5.16 kernel, Tx timeout mechanism was added into the virtio_net driver which prints the "Tx timeout" message when a packet is stuck in Tx queue for too long which could happen when external host driver is stuck or stopped and failed to read the FIFO.

Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

To avoid such "Tx timeout" messages, this commit adds a timeout mechanism to drop and release the pending Tx packet if not able to transmit for two seconds due to Tx FIFO full.

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

Quoted 8 lines
Quoted 3 lines

This doesn't really explain how it helps (other than avoiding the message which sounds like just hiding the issue). That is, how this helps to resume Tx? Or does Tx resume? There's nothing to indicate either way.

As the commit message mentioned, the expired packet is discarded and the packet buffer is released (see changes of calling mlxbf_tmfifo_release_pkt()). The Tx will resume automatically once the FIFO space is available, such as when external host driver starts to drain the TMFIFO. No need for any other logic.

Hmm, it seems to me that the same (resuming on FIFO space available) will happen without this patch ?

So as Ilpo mentioned the only purpose here seems to be to avoid the warning getting logged? And things work properly without this too ?

I guess the advantage of this patch is that during a blocked FIFO packets get discarded rather the piling up ?

Regards,

Hans

Quoted 146 lines
Quoted 146 lines
Quoted 2 lines
---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 67

++++++++++++++++++++++++

Review

Quoted 3 lines
 1 file changed, 67 insertions(+)
(quoted diff) +0 -0

b/drivers/platform/mellanox/mlxbf-tmfifo.c

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct

Quoted 44 lines

index 5c683b4eaf10..f39b7b9d2bfe 100644

drivers/platform/mellanox/mlxbf-tmfifo.c +7 -0
@@ -47,6 +47,9 @@
 /* Message with data needs at least two words (for header & data). */
 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
 
+/* Tx timeout in milliseconds. */
+#define TMFIFO_TX_TIMEOUT			2000
+
 /* ACPI UID for BlueField-3. */
 #define TMFIFO_BF3_UID				1
 
@@ -62,12 +65,14 @@ struct mlxbf_tmfifo;
  * @drop_desc: dummy desc for packet dropping
  * @cur_len: processed length of the current descriptor
  * @rem_len: remaining length of the pending packet
+ * @rem_padding: remaining bytes to send as paddings
  * @pkt_len: total length of the pending packet
  * @next_avail: next avail descriptor id
  * @num: vring size (number of descriptors)
  * @align: vring alignment size
  * @index: vring index
  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
+ * @tx_timeout: expire time of last tx packet
  * @fifo: pointer to the tmfifo structure
  */
 struct mlxbf_tmfifo_vring {
@@ -79,12 +84,14 @@ struct mlxbf_tmfifo_vring {
 	struct vring_desc drop_desc;
 	int cur_len;
 	int rem_len;
+	int rem_padding;
 	u32 pkt_len;
 	u16 next_avail;
 	int num;
 	int align;
 	int index;
 	int vdev_id;
+	unsigned long tx_timeout;
 	struct mlxbf_tmfifo *fifo;
 };
 
@@ -819,6 +826,50 @@ static bool mlxbf_tmfifo_rxtx_one_desc(struct

mlxbf_tmfifo_vring *vring,

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 4 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
 	return true;
 }
 
+static void mlxbf_tmfifo_check_tx_timeout(struct mlxbf_tmfifo_vring

*vring)

Quoted 11 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -0
+{
+	unsigned long flags;
+
+	/* Only handle Tx timeout for network vdev. */
+	if (vring->vdev_id != VIRTIO_ID_NET)
+		return;
+
+	/* Initialize the timeout or return if not expired. */
+	if (!vring->tx_timeout) {
+		/* Initialize the timeout. */
+		vring->tx_timeout = jiffies +

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 10 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +10 -0
+			msecs_to_jiffies(TMFIFO_TX_TIMEOUT);
+		return;
+	} else if (time_before(jiffies, vring->tx_timeout)) {
+		/* Return if not timeout yet. */
+		return;
+	}
+
+	/*
+	 * Drop the packet after timeout. The outstanding packet is
+	 * released and the remaining bytes will be sent with padding byte

0x00

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct · line 852

Quoted 2 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +2 -0
+	 * as a recovery. On the peer(host) side, the padding bytes 0x00 will be
+	 * either dropped directly, or appended into existing outstanding

packet

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 24 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +20 -0
+	 * thus dropped as corrupted network packet.
+	 */
+	vring->rem_padding = round_up(vring->rem_len, sizeof(u64));
+	mlxbf_tmfifo_release_pkt(vring);
+	vring->cur_len = 0;
+	vring->rem_len = 0;
+	vring->fifo->vring[0] = NULL;
+
+	/*
+	 * Make sure the load/store are in order before
+	 * returning back to virtio.
+	 */
+	virtio_mb(false);
+
+	/* Notify upper layer. */
+	spin_lock_irqsave(&vring->fifo->spin_lock[0], flags);
+	vring_interrupt(0, vring->vq);
+	spin_unlock_irqrestore(&vring->fifo->spin_lock[0], flags);
+}
+
 /* Rx & Tx processing of a queue. */
 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 {
@@ -841,6 +892,7 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 8 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
 		return;
 
 	do {
+retry:
 		/* Get available FIFO space. */
 		if (avail == 0) {
 			if (is_rx)
@@ -851,6 +903,17 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 18 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -0
 				break;
 		}
 
+		/* Insert paddings for discarded Tx packet. */
+		if (!is_rx) {
+			vring->tx_timeout = 0;
+			while (vring->rem_padding >= sizeof(u64)) {
+				writeq(0, vring->fifo->tx.data);
+				vring->rem_padding -= sizeof(u64);
+				if (--avail == 0)
+					goto retry;
+			}
+		}
+
 		/* Console output always comes from the Tx buffer. */
 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
 			mlxbf_tmfifo_console_tx(fifo, avail);
@@ -860,6 +923,10 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

Quoted 11 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +4 -0
 		/* Handle one descriptor. */
 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
 	} while (more);
+
+	/* Check Tx timeout. */
+	if (avail <= 0 && !is_rx)
+		mlxbf_tmfifo_check_tx_timeout(vring);
 }
 
 /* Handle Rx or Tx queues. */
author
#5 lore raw
Quoted 60 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Monday, January 8, 2024 9:23 AM
To: Liming Sun <[email protected]>; Ilpo Järvinen <[email protected]>
Cc: Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>; [email protected]; LKML <[email protected]>
Subject: Re: [PATCH v2 1/1] Drop Tx network packet when Tx TmFIFO is full

Hi,

Quoted 44 lines · Liming Sun
Quoted 43 lines

-----Original Message-----
From: Ilpo Järvinen <[email protected]>
Sent: Thursday, January 4, 2024 12:39 PM
To: Liming Sun <[email protected]>
Cc: Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Hans de Goede <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>; [email protected]; LKML <[email protected]>
Subject: Re: [PATCH v2 1/1] Drop Tx network packet when Tx TmFIFO is full

Quoted 16 lines · Liming Sun

Starting from Linux 5.16 kernel, Tx timeout mechanism was added into the virtio_net driver which prints the "Tx timeout" message when a packet is stuck in Tx queue for too long which could happen when external host driver is stuck or stopped and failed to read the FIFO.

Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

To avoid such "Tx timeout" messages, this commit adds a timeout mechanism to drop and release the pending Tx packet if not able to transmit for two seconds due to Tx FIFO full.

Quoted 8 lines

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

This doesn't really explain how it helps (other than avoiding the message which sounds like just hiding the issue). That is, how this helps to resume Tx? Or does Tx resume? There's nothing to indicate either way.

Quoted 8 lines
Quoted 3 lines

As the commit message mentioned, the expired packet is discarded and the packet buffer is released (see changes of calling mlxbf_tmfifo_release_pkt()). The Tx will resume automatically once the FIFO space is available, such as

when

Quoted 1 line

external host driver starts to drain the TMFIFO. No need for any other logic.

Hmm, it seems to me that the same (resuming on FIFO space available) will happen without this patch ?

Yes, it's true.

Quoted 10 lines

So as Ilpo mentioned the only purpose here seems to be to avoid the warning getting logged? And things work properly without this too ?

I guess the advantage of this patch is that during a blocked FIFO packets get discarded rather the piling up ?

Regards,

Hans

Probably I misunderstood Ilpo's comments.
Yes, the only purpose is to avoid the warning messages (since users and QA doesn't like these messages). It is only for networking packet, which seems reasonable to drop it if not able to complete the transmission in seconds.

Quoted 150 lines
Quoted 150 lines
Quoted 150 lines
Quoted 6 lines



---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 67

++++++++++++++++++++++++

Review

Quoted 3 lines
 1 file changed, 67 insertions(+)
(quoted diff) +0 -0

b/drivers/platform/mellanox/mlxbf-tmfifo.c

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct

Quoted 44 lines

index 5c683b4eaf10..f39b7b9d2bfe 100644

drivers/platform/mellanox/mlxbf-tmfifo.c +7 -0
@@ -47,6 +47,9 @@
 /* Message with data needs at least two words (for header & data). */
 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
 
+/* Tx timeout in milliseconds. */
+#define TMFIFO_TX_TIMEOUT			2000
+
 /* ACPI UID for BlueField-3. */
 #define TMFIFO_BF3_UID				1
 
@@ -62,12 +65,14 @@ struct mlxbf_tmfifo;
  * @drop_desc: dummy desc for packet dropping
  * @cur_len: processed length of the current descriptor
  * @rem_len: remaining length of the pending packet
+ * @rem_padding: remaining bytes to send as paddings
  * @pkt_len: total length of the pending packet
  * @next_avail: next avail descriptor id
  * @num: vring size (number of descriptors)
  * @align: vring alignment size
  * @index: vring index
  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
+ * @tx_timeout: expire time of last tx packet
  * @fifo: pointer to the tmfifo structure
  */
 struct mlxbf_tmfifo_vring {
@@ -79,12 +84,14 @@ struct mlxbf_tmfifo_vring {
 	struct vring_desc drop_desc;
 	int cur_len;
 	int rem_len;
+	int rem_padding;
 	u32 pkt_len;
 	u16 next_avail;
 	int num;
 	int align;
 	int index;
 	int vdev_id;
+	unsigned long tx_timeout;
 	struct mlxbf_tmfifo *fifo;
 };
 
@@ -819,6 +826,50 @@ static bool mlxbf_tmfifo_rxtx_one_desc(struct

mlxbf_tmfifo_vring *vring,

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 4 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
 	return true;
 }
 
+static void mlxbf_tmfifo_check_tx_timeout(struct mlxbf_tmfifo_vring

*vring)

Quoted 11 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -0
+{
+	unsigned long flags;
+
+	/* Only handle Tx timeout for network vdev. */
+	if (vring->vdev_id != VIRTIO_ID_NET)
+		return;
+
+	/* Initialize the timeout or return if not expired. */
+	if (!vring->tx_timeout) {
+		/* Initialize the timeout. */
+		vring->tx_timeout = jiffies +

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 10 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +10 -0
+			msecs_to_jiffies(TMFIFO_TX_TIMEOUT);
+		return;
+	} else if (time_before(jiffies, vring->tx_timeout)) {
+		/* Return if not timeout yet. */
+		return;
+	}
+
+	/*
+	 * Drop the packet after timeout. The outstanding packet is
+	 * released and the remaining bytes will be sent with padding byte

0x00

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct · line 852

Quoted 2 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +2 -0
+	 * as a recovery. On the peer(host) side, the padding bytes 0x00 will be
+	 * either dropped directly, or appended into existing outstanding

packet

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 24 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +20 -0
+	 * thus dropped as corrupted network packet.
+	 */
+	vring->rem_padding = round_up(vring->rem_len, sizeof(u64));
+	mlxbf_tmfifo_release_pkt(vring);
+	vring->cur_len = 0;
+	vring->rem_len = 0;
+	vring->fifo->vring[0] = NULL;
+
+	/*
+	 * Make sure the load/store are in order before
+	 * returning back to virtio.
+	 */
+	virtio_mb(false);
+
+	/* Notify upper layer. */
+	spin_lock_irqsave(&vring->fifo->spin_lock[0], flags);
+	vring_interrupt(0, vring->vq);
+	spin_unlock_irqrestore(&vring->fifo->spin_lock[0], flags);
+}
+
 /* Rx & Tx processing of a queue. */
 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 {
@@ -841,6 +892,7 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 8 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
 		return;
 
 	do {
+retry:
 		/* Get available FIFO space. */
 		if (avail == 0) {
 			if (is_rx)
@@ -851,6 +903,17 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 18 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -0
 				break;
 		}
 
+		/* Insert paddings for discarded Tx packet. */
+		if (!is_rx) {
+			vring->tx_timeout = 0;
+			while (vring->rem_padding >= sizeof(u64)) {
+				writeq(0, vring->fifo->tx.data);
+				vring->rem_padding -= sizeof(u64);
+				if (--avail == 0)
+					goto retry;
+			}
+		}
+
 		/* Console output always comes from the Tx buffer. */
 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
 			mlxbf_tmfifo_console_tx(fifo, avail);
@@ -860,6 +923,10 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

Quoted 11 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +4 -0
 		/* Handle one descriptor. */
 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
 	} while (more);
+
+	/* Check Tx timeout. */
+	if (avail <= 0 && !is_rx)
+		mlxbf_tmfifo_check_tx_timeout(vring);
 }
 
 /* Handle Rx or Tx queues. */
maintainer
#6 lore raw
Quoted 76 lines · Liming Sun
Quoted 58 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Monday, January 8, 2024 9:23 AM
To: Liming Sun <[email protected]>; Ilpo Järvinen <[email protected]>
Cc: Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>; [email protected]; LKML <[email protected]>
Subject: Re: [PATCH v2 1/1] Drop Tx network packet when Tx TmFIFO is full

Hi,

Quoted 44 lines · Liming Sun
Quoted 43 lines

-----Original Message-----
From: Ilpo Järvinen <[email protected]>
Sent: Thursday, January 4, 2024 12:39 PM
To: Liming Sun <[email protected]>
Cc: Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Hans de Goede <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>; [email protected]; LKML <[email protected]>
Subject: Re: [PATCH v2 1/1] Drop Tx network packet when Tx TmFIFO is full

Quoted 16 lines · Liming Sun

Starting from Linux 5.16 kernel, Tx timeout mechanism was added into the virtio_net driver which prints the "Tx timeout" message when a packet is stuck in Tx queue for too long which could happen when external host driver is stuck or stopped and failed to read the FIFO.

Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

To avoid such "Tx timeout" messages, this commit adds a timeout mechanism to drop and release the pending Tx packet if not able to transmit for two seconds due to Tx FIFO full.

Quoted 8 lines

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

This doesn't really explain how it helps (other than avoiding the message which sounds like just hiding the issue). That is, how this helps to resume Tx? Or does Tx resume? There's nothing to indicate either way.

Quoted 8 lines
Quoted 3 lines

As the commit message mentioned, the expired packet is discarded and the packet buffer is released (see changes of calling mlxbf_tmfifo_release_pkt()). The Tx will resume automatically once the FIFO space is available, such as

when

Quoted 1 line

external host driver starts to drain the TMFIFO. No need for any other logic.

Hmm, it seems to me that the same (resuming on FIFO space available) will happen without this patch ?

Yes, it's true.

Quoted 7 lines

So as Ilpo mentioned the only purpose here seems to be to avoid the warning getting logged? And things work properly without this too ?

I guess the advantage of this patch is that during a blocked FIFO packets get discarded rather the piling up ?

Quoted 7 lines
Quoted 3 lines

Regards,

Hans

Probably I misunderstood Ilpo's comments.
Yes, the only purpose is to avoid the warning messages (since users and QA doesn't like these messages). It is only for networking packet, which seems reasonable to drop it if not able to complete the transmission in seconds.

Okay, makes much more sense now as I couldn't see anything that would have helped to resume from "stuck" or "failed" state.

I think the first paragraph of the commit message misleads the reader to think something is "stuck" or "failed". And since this doesn't exactly fix such an issue that is not there in the first place it leads to confusion.

Perhaps it would be good to try to rephrase the first paragraph such that it wouldn't hint something is stuck when there is only a delay spike in FIFO's consumer side (if I now understand the problem space correctly?).

Signature · 1 line
 i.
Quoted 148 lines
Quoted 146 lines
Quoted 146 lines
Quoted 146 lines
Quoted 2 lines
---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 67

++++++++++++++++++++++++

Review

Quoted 3 lines
 1 file changed, 67 insertions(+)
(quoted diff) +0 -0

b/drivers/platform/mellanox/mlxbf-tmfifo.c

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct

Quoted 44 lines

index 5c683b4eaf10..f39b7b9d2bfe 100644

drivers/platform/mellanox/mlxbf-tmfifo.c +7 -0
@@ -47,6 +47,9 @@
 /* Message with data needs at least two words (for header & data). */
 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
 
+/* Tx timeout in milliseconds. */
+#define TMFIFO_TX_TIMEOUT			2000
+
 /* ACPI UID for BlueField-3. */
 #define TMFIFO_BF3_UID				1
 
@@ -62,12 +65,14 @@ struct mlxbf_tmfifo;
  * @drop_desc: dummy desc for packet dropping
  * @cur_len: processed length of the current descriptor
  * @rem_len: remaining length of the pending packet
+ * @rem_padding: remaining bytes to send as paddings
  * @pkt_len: total length of the pending packet
  * @next_avail: next avail descriptor id
  * @num: vring size (number of descriptors)
  * @align: vring alignment size
  * @index: vring index
  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
+ * @tx_timeout: expire time of last tx packet
  * @fifo: pointer to the tmfifo structure
  */
 struct mlxbf_tmfifo_vring {
@@ -79,12 +84,14 @@ struct mlxbf_tmfifo_vring {
 	struct vring_desc drop_desc;
 	int cur_len;
 	int rem_len;
+	int rem_padding;
 	u32 pkt_len;
 	u16 next_avail;
 	int num;
 	int align;
 	int index;
 	int vdev_id;
+	unsigned long tx_timeout;
 	struct mlxbf_tmfifo *fifo;
 };
 
@@ -819,6 +826,50 @@ static bool mlxbf_tmfifo_rxtx_one_desc(struct

mlxbf_tmfifo_vring *vring,

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 4 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
 	return true;
 }
 
+static void mlxbf_tmfifo_check_tx_timeout(struct mlxbf_tmfifo_vring

*vring)

Quoted 11 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -0
+{
+	unsigned long flags;
+
+	/* Only handle Tx timeout for network vdev. */
+	if (vring->vdev_id != VIRTIO_ID_NET)
+		return;
+
+	/* Initialize the timeout or return if not expired. */
+	if (!vring->tx_timeout) {
+		/* Initialize the timeout. */
+		vring->tx_timeout = jiffies +

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 10 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +10 -0
+			msecs_to_jiffies(TMFIFO_TX_TIMEOUT);
+		return;
+	} else if (time_before(jiffies, vring->tx_timeout)) {
+		/* Return if not timeout yet. */
+		return;
+	}
+
+	/*
+	 * Drop the packet after timeout. The outstanding packet is
+	 * released and the remaining bytes will be sent with padding byte

0x00

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct · line 852

Quoted 2 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +2 -0
+	 * as a recovery. On the peer(host) side, the padding bytes 0x00 will be
+	 * either dropped directly, or appended into existing outstanding

packet

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 24 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +20 -0
+	 * thus dropped as corrupted network packet.
+	 */
+	vring->rem_padding = round_up(vring->rem_len, sizeof(u64));
+	mlxbf_tmfifo_release_pkt(vring);
+	vring->cur_len = 0;
+	vring->rem_len = 0;
+	vring->fifo->vring[0] = NULL;
+
+	/*
+	 * Make sure the load/store are in order before
+	 * returning back to virtio.
+	 */
+	virtio_mb(false);
+
+	/* Notify upper layer. */
+	spin_lock_irqsave(&vring->fifo->spin_lock[0], flags);
+	vring_interrupt(0, vring->vq);
+	spin_unlock_irqrestore(&vring->fifo->spin_lock[0], flags);
+}
+
 /* Rx & Tx processing of a queue. */
 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 {
@@ -841,6 +892,7 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 8 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
 		return;
 
 	do {
+retry:
 		/* Get available FIFO space. */
 		if (avail == 0) {
 			if (is_rx)
@@ -851,6 +903,17 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 18 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -0
 				break;
 		}
 
+		/* Insert paddings for discarded Tx packet. */
+		if (!is_rx) {
+			vring->tx_timeout = 0;
+			while (vring->rem_padding >= sizeof(u64)) {
+				writeq(0, vring->fifo->tx.data);
+				vring->rem_padding -= sizeof(u64);
+				if (--avail == 0)
+					goto retry;
+			}
+		}
+
 		/* Console output always comes from the Tx buffer. */
 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
 			mlxbf_tmfifo_console_tx(fifo, avail);
@@ -860,6 +923,10 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

Quoted 11 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +4 -0
 		/* Handle one descriptor. */
 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
 	} while (more);
+
+	/* Check Tx timeout. */
+	if (avail <= 0 && !is_rx)
+		mlxbf_tmfifo_check_tx_timeout(vring);
 }
 
 /* Handle Rx or Tx queues. */
author
#7 lore raw
Quoted 111 lines

-----Original Message-----
From: Ilpo Järvinen <[email protected]>
Sent: Monday, January 8, 2024 2:03 PM
To: Liming Sun <[email protected]>
Cc: Hans de Goede <[email protected]>; Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>; [email protected]; LKML <[email protected]>
Subject: RE: [PATCH v2 1/1] Drop Tx network packet when Tx TmFIFO is full

Quoted 8 lines · Liming Sun
Quoted 8 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Monday, January 8, 2024 9:23 AM
To: Liming Sun <[email protected]>; Ilpo Järvinen <[email protected]>
Cc: Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>; platform-driver-

[email protected];

Quoted 8 lines
Quoted 8 lines

LKML <[email protected]>
Subject: Re: [PATCH v2 1/1] Drop Tx network packet when Tx TmFIFO is full

Hi,

Quoted 2 lines · Liming Sun
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Ilpo Järvinen <[email protected]>
Sent: Thursday, January 4, 2024 12:39 PM
To: Liming Sun <[email protected]>
Cc: Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Hans de Goede

<[email protected]>;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Mark Gross <[email protected]>; Dan Carpenter <[email protected]>; [email protected];

LKML

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: Re: [PATCH v2 1/1] Drop Tx network packet when Tx TmFIFO is

full

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines · Liming Sun
Quoted 24 lines

Starting from Linux 5.16 kernel, Tx timeout mechanism was added into the virtio_net driver which prints the "Tx timeout" message when a packet is stuck in Tx queue for too long which could happen when external host driver is stuck or stopped and failed to read the FIFO.

Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

To avoid such "Tx timeout" messages, this commit adds a timeout mechanism to drop and release the pending Tx packet if not able to transmit for two seconds due to Tx FIFO full.

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

This doesn't really explain how it helps (other than avoiding the message which sounds like just hiding the issue). That is, how this helps to resume Tx? Or does Tx resume? There's nothing to indicate either

way.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

As the commit message mentioned, the expired packet is discarded and

the

Quoted 1 line
Quoted 1 line
Quoted 1 line

packet buffer is released (see changes of calling

mlxbf_tmfifo_release_pkt()).

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

The Tx will resume automatically once the FIFO space is available, such as

when

Quoted 1 line

external host driver starts to drain the TMFIFO. No need for any other

logic.

Quoted 8 lines
Quoted 3 lines

Hmm, it seems to me that the same (resuming on FIFO space available) will happen without this patch ?

Yes, it's true.

Quoted 2 lines

So as Ilpo mentioned the only purpose here seems to be to avoid the

warning

Quoted 11 lines
Quoted 8 lines

getting logged? And things work properly without this too ?

I guess the advantage of this patch is that during a blocked FIFO packets get discarded rather the piling up ?

Regards,

Hans

Probably I misunderstood Ilpo's comments.
Yes, the only purpose is to avoid the warning messages (since users and QA

doesn't like these messages).

Quoted 1 line

It is only for networking packet, which seems reasonable to drop it if not able

to complete the transmission in seconds.

Okay, makes much more sense now as I couldn't see anything that would have helped to resume from "stuck" or "failed" state.

Quoted 7 lines

I think the first paragraph of the commit message misleads the reader to think something is "stuck" or "failed". And since this doesn't exactly fix such an issue that is not there in the first place it leads to confusion.

Perhaps it would be good to try to rephrase the first paragraph such that it wouldn't hint something is stuck when there is only a delay spike in FIFO's consumer side (if I now understand the problem space correctly?).

This problem could happen in several conditions:

1. The host driver (the consumer side of the FIFO) is slow.
2. The host driver (the consumer side of the FIFO) is stuck or stopped (not running).

There is no keepalive mechanism yet (which could be added later). So for case #2, it'll be packet stuck in the FIFO.

Sure, I could rephrase the first paragraph to avoid the 'stuck' related misleading.

Quoted 159 lines

--
i.

Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 2 lines
---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 67

++++++++++++++++++++++++

Review

Quoted 3 lines
 1 file changed, 67 insertions(+)
(quoted diff) +0 -0

b/drivers/platform/mellanox/mlxbf-tmfifo.c

Quoted 43 lines

index 5c683b4eaf10..f39b7b9d2bfe 100644

drivers/platform/mellanox/mlxbf-tmfifo.c +7 -0
@@ -47,6 +47,9 @@
 /* Message with data needs at least two words (for header & data). */
 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
 
+/* Tx timeout in milliseconds. */
+#define TMFIFO_TX_TIMEOUT			2000
+
 /* ACPI UID for BlueField-3. */
 #define TMFIFO_BF3_UID				1
 
@@ -62,12 +65,14 @@ struct mlxbf_tmfifo;
  * @drop_desc: dummy desc for packet dropping
  * @cur_len: processed length of the current descriptor
  * @rem_len: remaining length of the pending packet
+ * @rem_padding: remaining bytes to send as paddings
  * @pkt_len: total length of the pending packet
  * @next_avail: next avail descriptor id
  * @num: vring size (number of descriptors)
  * @align: vring alignment size
  * @index: vring index
  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
+ * @tx_timeout: expire time of last tx packet
  * @fifo: pointer to the tmfifo structure
  */
 struct mlxbf_tmfifo_vring {
@@ -79,12 +84,14 @@ struct mlxbf_tmfifo_vring {
 	struct vring_desc drop_desc;
 	int cur_len;
 	int rem_len;
+	int rem_padding;
 	u32 pkt_len;
 	u16 next_avail;
 	int num;
 	int align;
 	int index;
 	int vdev_id;
+	unsigned long tx_timeout;
 	struct mlxbf_tmfifo *fifo;
 };
 
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/platform/mellanox/mlxbf-tmfifo.c static bool

Quoted 1 line
drivers/platform/mellanox/mlxbf-tmfifo.c +0 -0
@@ -819,6 +826,50 @@ static bool

mlxbf_tmfifo_rxtx_one_desc(struct

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

mlxbf_tmfifo_vring *vring,

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 4 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
 	return true;
 }
 
+static void mlxbf_tmfifo_check_tx_timeout(struct mlxbf_tmfifo_vring

*vring)

Quoted 13 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +13 -0
+{
+	unsigned long flags;
+
+	/* Only handle Tx timeout for network vdev. */
+	if (vring->vdev_id != VIRTIO_ID_NET)
+		return;
+
+	/* Initialize the timeout or return if not expired. */
+	if (!vring->tx_timeout) {
+		/* Initialize the timeout. */
+		vring->tx_timeout = jiffies +
+			msecs_to_jiffies(TMFIFO_TX_TIMEOUT);
+		return;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 8 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +8 -0
+	} else if (time_before(jiffies, vring->tx_timeout)) {
+		/* Return if not timeout yet. */
+		return;
+	}
+
+	/*
+	 * Drop the packet after timeout. The outstanding packet is
+	 * released and the remaining bytes will be sent with padding

byte

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

0x00

drivers/platform/mellanox/mlxbf-tmfifo.c static bool · line 851

Quoted 1 line
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
+	 * as a recovery. On the peer(host) side, the padding bytes

0x00 will be

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/platform/mellanox/mlxbf-tmfifo.c static bool · line 852

Quoted 1 line
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
+	 * either dropped directly, or appended into existing

outstanding

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

packet

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 3 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +3 -0
+	 * thus dropped as corrupted network packet.
+	 */
+	vring->rem_padding = round_up(vring->rem_len,

sizeof(u64));

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -0
+	mlxbf_tmfifo_release_pkt(vring);
+	vring->cur_len = 0;
+	vring->rem_len = 0;
+	vring->fifo->vring[0] = NULL;
+
+	/*
+	 * Make sure the load/store are in order before
+	 * returning back to virtio.
+	 */
+	virtio_mb(false);
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/platform/mellanox/mlxbf-tmfifo.c static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,

Quoted 8 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +6 -0
+	/* Notify upper layer. */
+	spin_lock_irqsave(&vring->fifo->spin_lock[0], flags);
+	vring_interrupt(0, vring->vq);
+	spin_unlock_irqrestore(&vring->fifo->spin_lock[0], flags);
+}
+
 /* Rx & Tx processing of a queue. */
 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool

is_rx)

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 2 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +0 -0
 {
@@ -841,6 +892,7 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 8 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -0
 		return;
 
 	do {
+retry:
 		/* Get available FIFO space. */
 		if (avail == 0) {
 			if (is_rx)
@@ -851,6 +903,17 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct

Quoted 18 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -0
 				break;
 		}
 
+		/* Insert paddings for discarded Tx packet. */
+		if (!is_rx) {
+			vring->tx_timeout = 0;
+			while (vring->rem_padding >= sizeof(u64)) {
+				writeq(0, vring->fifo->tx.data);
+				vring->rem_padding -= sizeof(u64);
+				if (--avail == 0)
+					goto retry;
+			}
+		}
+
 		/* Console output always comes from the Tx buffer. */
 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
 			mlxbf_tmfifo_console_tx(fifo, avail);
@@ -860,6 +923,10 @@ static void mlxbf_tmfifo_rxtx(struct

mlxbf_tmfifo_vring *vring, bool is_rx)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx(struct · line 924

Quoted 2 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +0 -0
 		/* Handle one descriptor. */
 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx,

&avail);

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +4 -0
 	} while (more);
+
+	/* Check Tx timeout. */
+	if (avail <= 0 && !is_rx)
+		mlxbf_tmfifo_check_tx_timeout(vring);
 }
 
 /* Handle Rx or Tx queues. */
author
#8 lore raw

PATCH v3 1/1 Drop Tx network packet when Tx TmFIFO is full

In reply to Liming Sun

Starting from Linux 5.16 kernel, Tx timeout mechanism was added in the virtio_net driver which prints the "Tx timeout" warning message when a packet stays in Tx queue for too long. Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

This issue could happen when external host driver which drains the FIFO is restared, stopped or upgraded. To avoid such confusing "Tx timeout" messages, this commit adds logic to drop the outstanding Tx packet if it's not able to transmit in two seconds due to Tx FIFO full, which can be considered as congestion or out-of-resource drop.

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

---
v2->v3:
Updates for Ilpo's comments:
- Revises commit message to avoid confusion.
v2: Fixed formatting warning
v1: Initial version

---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 67 ++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
drivers/platform/mellanox/mlxbf-tmfifo.c +67 -0
index 5c683b4eaf10..f39b7b9d2bfe 100644
@@ -47,6 +47,9 @@
 /* Message with data needs at least two words (for header & data). */
 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
 
+/* Tx timeout in milliseconds. */
+#define TMFIFO_TX_TIMEOUT			2000
+
 /* ACPI UID for BlueField-3. */
 #define TMFIFO_BF3_UID				1
 
@@ -62,12 +65,14 @@ struct mlxbf_tmfifo;
  * @drop_desc: dummy desc for packet dropping
  * @cur_len: processed length of the current descriptor
  * @rem_len: remaining length of the pending packet
+ * @rem_padding: remaining bytes to send as paddings
  * @pkt_len: total length of the pending packet
  * @next_avail: next avail descriptor id
  * @num: vring size (number of descriptors)
  * @align: vring alignment size
  * @index: vring index
  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
+ * @tx_timeout: expire time of last tx packet
  * @fifo: pointer to the tmfifo structure
  */
 struct mlxbf_tmfifo_vring {
@@ -79,12 +84,14 @@ struct mlxbf_tmfifo_vring {
 	struct vring_desc drop_desc;
 	int cur_len;
 	int rem_len;
+	int rem_padding;
 	u32 pkt_len;
 	u16 next_avail;
 	int num;
 	int align;
 	int index;
 	int vdev_id;
+	unsigned long tx_timeout;
 	struct mlxbf_tmfifo *fifo;
 };
 
@@ -819,6 +826,50 @@ static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,
 	return true;
 }
 
+static void mlxbf_tmfifo_check_tx_timeout(struct mlxbf_tmfifo_vring *vring)
+{
+	unsigned long flags;
+
+	/* Only handle Tx timeout for network vdev. */
+	if (vring->vdev_id != VIRTIO_ID_NET)
+		return;
+
+	/* Initialize the timeout or return if not expired. */
+	if (!vring->tx_timeout) {
+		/* Initialize the timeout. */
+		vring->tx_timeout = jiffies +
+			msecs_to_jiffies(TMFIFO_TX_TIMEOUT);
+		return;
+	} else if (time_before(jiffies, vring->tx_timeout)) {
+		/* Return if not timeout yet. */
+		return;
+	}
+
+	/*
+	 * Drop the packet after timeout. The outstanding packet is
+	 * released and the remaining bytes will be sent with padding byte 0x00
+	 * as a recovery. On the peer(host) side, the padding bytes 0x00 will be
+	 * either dropped directly, or appended into existing outstanding packet
+	 * thus dropped as corrupted network packet.
+	 */
+	vring->rem_padding = round_up(vring->rem_len, sizeof(u64));
+	mlxbf_tmfifo_release_pkt(vring);
+	vring->cur_len = 0;
+	vring->rem_len = 0;
+	vring->fifo->vring[0] = NULL;
+
+	/*
+	 * Make sure the load/store are in order before
+	 * returning back to virtio.
+	 */
+	virtio_mb(false);
+
+	/* Notify upper layer. */
+	spin_lock_irqsave(&vring->fifo->spin_lock[0], flags);
+	vring_interrupt(0, vring->vq);
+	spin_unlock_irqrestore(&vring->fifo->spin_lock[0], flags);
+}
+
 /* Rx & Tx processing of a queue. */
 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 {
@@ -841,6 +892,7 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 		return;
 
 	do {
+retry:
 		/* Get available FIFO space. */
 		if (avail == 0) {
 			if (is_rx)
@@ -851,6 +903,17 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 				break;
 		}
 
+		/* Insert paddings for discarded Tx packet. */
+		if (!is_rx) {
+			vring->tx_timeout = 0;
+			while (vring->rem_padding >= sizeof(u64)) {
+				writeq(0, vring->fifo->tx.data);
+				vring->rem_padding -= sizeof(u64);
+				if (--avail == 0)
+					goto retry;
+			}
+		}
+
 		/* Console output always comes from the Tx buffer. */
 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
 			mlxbf_tmfifo_console_tx(fifo, avail);
@@ -860,6 +923,10 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 		/* Handle one descriptor. */
 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
 	} while (more);
+
+	/* Check Tx timeout. */
+	if (avail <= 0 && !is_rx)
+		mlxbf_tmfifo_check_tx_timeout(vring);
 }
 
 /* Handle Rx or Tx queues. */
Signature · 1 line
2.30.1
maintainer Reviewed-by on v3 1/1
#9 lore raw
Quoted 22 lines · Liming Sun

Starting from Linux 5.16 kernel, Tx timeout mechanism was added in the virtio_net driver which prints the "Tx timeout" warning message when a packet stays in Tx queue for too long. Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

This issue could happen when external host driver which drains the FIFO is restared, stopped or upgraded. To avoid such confusing "Tx timeout" messages, this commit adds logic to drop the outstanding Tx packet if it's not able to transmit in two seconds due to Tx FIFO full, which can be considered as congestion or out-of-resource drop.

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

Quoted 7 lines

---
v2->v3:
Updates for Ilpo's comments:
- Revises commit message to avoid confusion.
v2: Fixed formatting warning
v1: Initial version

Thanks, the commit message makes much more sense now!

Signature · 1 line
 i.
#10 lore raw
In reply to Liming Sun

Hi,

Quoted 15 lines · Liming Sun

Starting from Linux 5.16 kernel, Tx timeout mechanism was added in the virtio_net driver which prints the "Tx timeout" warning message when a packet stays in Tx queue for too long. Below is an example of the reported message:

"[494105.316739] virtio_net virtio1 tmfifo_net0: TX timeout on
queue: 0, sq: output.0, vq: 0×1, name: output.0, usecs since last trans: 3079892256".

This issue could happen when external host driver which drains the FIFO is restared, stopped or upgraded. To avoid such confusing "Tx timeout" messages, this commit adds logic to drop the outstanding Tx packet if it's not able to transmit in two seconds due to Tx FIFO full, which can be considered as congestion or out-of-resource drop.

Quoted 8 lines

This commit also handles the special case that the packet is half- transmitted into the Tx FIFO. In such case, the packet is discarded with remaining length stored in vring->rem_padding. So paddings with zeros can be sent out when Tx space is available to maintain the integrity of the packet format. The padded packet will be dropped on the receiving side.

Thank you for your patch/series, I've applied this patch (series) to my review-hans branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=review-hans

Note it will show up in the pdx86 review-hans branch once I've pushed my local branch there, which might take a while.

I will include this patch in my next fixes pull-req to Linus for the current kernel development cycle.

Regards,

Hans

Quoted 142 lines

---
v2->v3:
Updates for Ilpo's comments:
- Revises commit message to avoid confusion.
v2: Fixed formatting warning
v1: Initial version

---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 67 ++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
drivers/platform/mellanox/mlxbf-tmfifo.c +67 -0
index 5c683b4eaf10..f39b7b9d2bfe 100644
@@ -47,6 +47,9 @@
 /* Message with data needs at least two words (for header & data). */
 #define MLXBF_TMFIFO_DATA_MIN_WORDS		2
 
+/* Tx timeout in milliseconds. */
+#define TMFIFO_TX_TIMEOUT			2000
+
 /* ACPI UID for BlueField-3. */
 #define TMFIFO_BF3_UID				1
 
@@ -62,12 +65,14 @@ struct mlxbf_tmfifo;
  * @drop_desc: dummy desc for packet dropping
  * @cur_len: processed length of the current descriptor
  * @rem_len: remaining length of the pending packet
+ * @rem_padding: remaining bytes to send as paddings
  * @pkt_len: total length of the pending packet
  * @next_avail: next avail descriptor id
  * @num: vring size (number of descriptors)
  * @align: vring alignment size
  * @index: vring index
  * @vdev_id: vring virtio id (VIRTIO_ID_xxx)
+ * @tx_timeout: expire time of last tx packet
  * @fifo: pointer to the tmfifo structure
  */
 struct mlxbf_tmfifo_vring {
@@ -79,12 +84,14 @@ struct mlxbf_tmfifo_vring {
 	struct vring_desc drop_desc;
 	int cur_len;
 	int rem_len;
+	int rem_padding;
 	u32 pkt_len;
 	u16 next_avail;
 	int num;
 	int align;
 	int index;
 	int vdev_id;
+	unsigned long tx_timeout;
 	struct mlxbf_tmfifo *fifo;
 };
 
@@ -819,6 +826,50 @@ static bool mlxbf_tmfifo_rxtx_one_desc(struct mlxbf_tmfifo_vring *vring,
 	return true;
 }
 
+static void mlxbf_tmfifo_check_tx_timeout(struct mlxbf_tmfifo_vring *vring)
+{
+	unsigned long flags;
+
+	/* Only handle Tx timeout for network vdev. */
+	if (vring->vdev_id != VIRTIO_ID_NET)
+		return;
+
+	/* Initialize the timeout or return if not expired. */
+	if (!vring->tx_timeout) {
+		/* Initialize the timeout. */
+		vring->tx_timeout = jiffies +
+			msecs_to_jiffies(TMFIFO_TX_TIMEOUT);
+		return;
+	} else if (time_before(jiffies, vring->tx_timeout)) {
+		/* Return if not timeout yet. */
+		return;
+	}
+
+	/*
+	 * Drop the packet after timeout. The outstanding packet is
+	 * released and the remaining bytes will be sent with padding byte 0x00
+	 * as a recovery. On the peer(host) side, the padding bytes 0x00 will be
+	 * either dropped directly, or appended into existing outstanding packet
+	 * thus dropped as corrupted network packet.
+	 */
+	vring->rem_padding = round_up(vring->rem_len, sizeof(u64));
+	mlxbf_tmfifo_release_pkt(vring);
+	vring->cur_len = 0;
+	vring->rem_len = 0;
+	vring->fifo->vring[0] = NULL;
+
+	/*
+	 * Make sure the load/store are in order before
+	 * returning back to virtio.
+	 */
+	virtio_mb(false);
+
+	/* Notify upper layer. */
+	spin_lock_irqsave(&vring->fifo->spin_lock[0], flags);
+	vring_interrupt(0, vring->vq);
+	spin_unlock_irqrestore(&vring->fifo->spin_lock[0], flags);
+}
+
 /* Rx & Tx processing of a queue. */
 static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 {
@@ -841,6 +892,7 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 		return;
 
 	do {
+retry:
 		/* Get available FIFO space. */
 		if (avail == 0) {
 			if (is_rx)
@@ -851,6 +903,17 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 				break;
 		}
 
+		/* Insert paddings for discarded Tx packet. */
+		if (!is_rx) {
+			vring->tx_timeout = 0;
+			while (vring->rem_padding >= sizeof(u64)) {
+				writeq(0, vring->fifo->tx.data);
+				vring->rem_padding -= sizeof(u64);
+				if (--avail == 0)
+					goto retry;
+			}
+		}
+
 		/* Console output always comes from the Tx buffer. */
 		if (!is_rx && devid == VIRTIO_ID_CONSOLE) {
 			mlxbf_tmfifo_console_tx(fifo, avail);
@@ -860,6 +923,10 @@ static void mlxbf_tmfifo_rxtx(struct mlxbf_tmfifo_vring *vring, bool is_rx)
 		/* Handle one descriptor. */
 		more = mlxbf_tmfifo_rxtx_one_desc(vring, is_rx, &avail);
 	} while (more);
+
+	/* Check Tx timeout. */
+	if (avail <= 0 && !is_rx)
+		mlxbf_tmfifo_check_tx_timeout(vring);
 }
 
 /* Handle Rx or Tx queues. */