Netdev List
 help / color / mirror / Atom feed
* [PATCH v3 0/4] net: thunderbolt: Various fixes
@ 2026-11-30 11:36 Mika Westerberg
  2026-11-30 11:36 ` [PATCH v3 1/4] net: thunderbolt: Fix race condition when the link is torn down Mika Westerberg
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Mika Westerberg @ 2026-11-30 11:36 UTC (permalink / raw)
  To: netdev
  Cc: Yehezkel Bernat, Andrew Lunn, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Mika Westerberg, Mika Westerberg

Hi all,

This series fixes various issues. Tree of them was pointed out by Sashiko
during code review, one was found during development of those fixes.

The previous versions of the series can be found:

  v2: https://lore.kernel.org/netdev/20261116142613.876208-1-mika.westerberg@linux.intel.com/
  v1: https://lore.kernel.org/netdev/20261103125621.1111198-1-mika.westerberg@linux.intel.com/

Changes from v2:

  - Release partially completed skb also on the error path.
  - New patch fixing race condition between tbnet_tear_down() and
    tbnet_connected_work().

Changes from v1:

  - Prevent unintended refilling the Rx ring if poll triggers during
    tear down.
  - Keep polling if we fail to allocate new Rx descriptors to avoid
    permanent stop.
  - New patch that releases partially completed skb.

Mika Westerberg (Intel) (4):
  net: thunderbolt: Fix race condition when the link is torn down
  net: thunderbolt: Stop queueing Rx buffers if allocation fails
  net: thunderbolt: Prevent Tx descriptor leak in tbnet_start_xmit()
  net: thunderbolt: Free partially completed skb when the link is torn down

 drivers/net/thunderbolt/main.c | 108 ++++++++++++++++++++++++---------
 1 file changed, 79 insertions(+), 29 deletions(-)

-- 
2.50.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3 1/4] net: thunderbolt: Fix race condition when the link is torn down
  2026-11-30 11:36 [PATCH v3 0/4] net: thunderbolt: Various fixes Mika Westerberg
@ 2026-11-30 11:36 ` Mika Westerberg
  2026-12-01 12:17   ` netdev-bot+sashiko
  2026-11-30 11:36 ` [PATCH v3 2/4] net: thunderbolt: Stop queueing Rx buffers if allocation fails Mika Westerberg
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 8+ messages in thread
From: Mika Westerberg @ 2026-11-30 11:36 UTC (permalink / raw)
  To: netdev
  Cc: Yehezkel Bernat, Andrew Lunn, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Mika Westerberg, Mika Westerberg

From: "Mika Westerberg (Intel)" <westeri@kernel.org>

When the link is torn down tbnet_tear_down() cancels connected_work
before taking connection_lock, but a ThunderboltIP login from the peer
can queue it again right after. That connected_work can run concurrently
with the teardown and restart the rings after they were stopped and the
buffers released.

Fix this by disabling login_work and connected_work for the duration of
the teardown, and release the data path resources only when the carrier
is on, which tbnet_connected_work() sets once they all are set up.

Fixes: e69b6c02b4c3 ("net: Add support for networking over Thunderbolt cable")
Cc: stable@vger.kernel.org
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/net/thunderbolt/main.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/drivers/net/thunderbolt/main.c b/drivers/net/thunderbolt/main.c
index cf51b9c39f4e..ede105cdc3a3 100644
--- a/drivers/net/thunderbolt/main.c
+++ b/drivers/net/thunderbolt/main.c
@@ -368,15 +368,22 @@ static void tbnet_free_buffers(struct tbnet_ring *ring)
 
 static void tbnet_tear_down(struct tbnet *net, bool send_logout)
 {
-	netif_carrier_off(net->dev);
-	netif_stop_queue(net->dev);
+	bool connected;
+	int ret;
 
-	stop_login(net);
+	disable_delayed_work_sync(&net->login_work);
+	disable_work_sync(&net->connected_work);
 
 	mutex_lock(&net->connection_lock);
 
+	connected = netif_carrier_ok(net->dev) && net->login_sent &&
+		    net->login_received;
+
+	netif_carrier_off(net->dev);
+	netif_stop_queue(net->dev);
+
 	if (net->login_sent && net->login_received) {
-		int ret, retries = TBNET_LOGOUT_RETRIES;
+		int retries = TBNET_LOGOUT_RETRIES;
 
 		while (send_logout && retries-- > 0) {
 			netdev_dbg(net->dev, "sending logout request %u\n",
@@ -385,7 +392,9 @@ static void tbnet_tear_down(struct tbnet *net, bool send_logout)
 			if (ret != -ETIMEDOUT)
 				break;
 		}
+	}
 
+	if (connected) {
 		/* Tear the paths down before stopping the rings.  This mirrors
 		 * tbnet_connected_work(), which enables the paths last so the
 		 * Rx ring is primed before packets can arrive.  Stopping a
@@ -419,6 +428,9 @@ static void tbnet_tear_down(struct tbnet *net, bool send_logout)
 
 	netdev_dbg(net->dev, "network traffic stopped\n");
 
+	enable_work(&net->connected_work);
+	enable_delayed_work(&net->login_work);
+
 	mutex_unlock(&net->connection_lock);
 }
 
@@ -645,7 +657,8 @@ static void tbnet_connected_work(struct work_struct *work)
 		return;
 
 	mutex_lock(&net->connection_lock);
-	connected = net->login_sent && net->login_received;
+	connected = net->login_sent && net->login_received &&
+		   netif_running(net->dev) && netif_device_present(net->dev);
 	mutex_unlock(&net->connection_lock);
 
 	if (!connected)
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v3 2/4] net: thunderbolt: Stop queueing Rx buffers if allocation fails
  2026-11-30 11:36 [PATCH v3 0/4] net: thunderbolt: Various fixes Mika Westerberg
  2026-11-30 11:36 ` [PATCH v3 1/4] net: thunderbolt: Fix race condition when the link is torn down Mika Westerberg
@ 2026-11-30 11:36 ` Mika Westerberg
  2026-12-01 12:17   ` netdev-bot+sashiko
  2026-11-30 11:36 ` [PATCH v3 3/4] net: thunderbolt: Prevent Tx descriptor leak in tbnet_start_xmit() Mika Westerberg
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 8+ messages in thread
From: Mika Westerberg @ 2026-11-30 11:36 UTC (permalink / raw)
  To: netdev
  Cc: Yehezkel Bernat, Andrew Lunn, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Mika Westerberg, Mika Westerberg

From: "Mika Westerberg (Intel)" <westeri@kernel.org>

Sashiko pointed out that there is a bug in the driver when Rx buffer
allocation fails, we silently release the memory but leave the
descriptor in the hardware ring. This could cause all kinds of memory
corruption to happen. Fix this by making tbnet_alloc_rx_buffers() not
release the buffers already queued and modify the call sites accordingly.

If tbnet_poll() runs out of Rx buffers and was not able to post new ones
it keeps the polling enabled to allow retrying the allocation in the
next pass. We prevent tbnet_poll() from being called when the connection
is torn down and buffers are released by disabling NAPI before the rings
are stopped (and enabling it only after the rings are started and Rx
buffers are filled in).

Fixes: e69b6c02b4c3 ("net: Add support for networking over Thunderbolt cable")
Closes: https://sashiko.dev/#/patchset/20260902082128.1148463-1-mika.westerberg%40linux.intel.com
Cc: stable@vger.kernel.org
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/net/thunderbolt/main.c | 69 +++++++++++++++++++++++-----------
 1 file changed, 47 insertions(+), 22 deletions(-)

diff --git a/drivers/net/thunderbolt/main.c b/drivers/net/thunderbolt/main.c
index ede105cdc3a3..d5e49bc38104 100644
--- a/drivers/net/thunderbolt/main.c
+++ b/drivers/net/thunderbolt/main.c
@@ -413,6 +413,8 @@ static void tbnet_tear_down(struct tbnet *net, bool send_logout)
 		if (ret)
 			netdev_warn(net->dev, "failed to disable DMA paths\n");
 
+		napi_disable(&net->napi);
+
 		tb_ring_stop(net->rx_ring.ring);
 		tb_ring_stop(net->tx_ring.ring);
 		tbnet_free_buffers(&net->rx_ring);
@@ -518,7 +520,7 @@ static unsigned int tbnet_available_buffers(const struct tbnet_ring *ring)
 static int tbnet_alloc_rx_buffers(struct tbnet *net, unsigned int nbuffers)
 {
 	struct tbnet_ring *ring = &net->rx_ring;
-	int ret;
+	int ret = 0;
 
 	while (nbuffers--) {
 		struct device *dma_dev = tb_ring_dma_device(ring->ring);
@@ -536,14 +538,16 @@ static int tbnet_alloc_rx_buffers(struct tbnet *net, unsigned int nbuffers)
 		tf->page = dev_alloc_pages(TBNET_RX_PAGE_ORDER);
 		if (!tf->page) {
 			ret = -ENOMEM;
-			goto err_free;
+			break;
 		}
 
 		dma_addr = dma_map_page(dma_dev, tf->page, 0,
 					TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
 		if (dma_mapping_error(dma_dev, dma_addr)) {
+			__free_pages(tf->page, TBNET_RX_PAGE_ORDER);
+			tf->page = NULL;
 			ret = -ENOMEM;
-			goto err_free;
+			break;
 		}
 
 		tf->frame.buffer_phy = dma_addr;
@@ -552,16 +556,19 @@ static int tbnet_alloc_rx_buffers(struct tbnet *net, unsigned int nbuffers)
 		trace_tbnet_alloc_rx_frame(index, tf->page, dma_addr,
 					   DMA_FROM_DEVICE);
 
-		tb_ring_rx_more(ring->ring, &tf->frame);
+		ret = tb_ring_rx_more(ring->ring, &tf->frame);
+		if (ret) {
+			dma_unmap_page(dma_dev, dma_addr, TBNET_RX_PAGE_SIZE,
+				       DMA_FROM_DEVICE);
+			__free_pages(tf->page, TBNET_RX_PAGE_ORDER);
+			tf->page = NULL;
+			break;
+		}
 
 		ring->prod++;
 	}
 
 	tb_ring_notify(ring->ring);
-	return 0;
-
-err_free:
-	tbnet_free_buffers(ring);
 	return ret;
 }
 
@@ -691,7 +698,11 @@ static void tbnet_connected_work(struct work_struct *work)
 
 	ret = tbnet_alloc_tx_buffers(net);
 	if (ret)
-		goto err_free_rx_buffers;
+		goto err_stop_rings;
+
+	napi_enable(&net->napi);
+	/* Unmasks the ring interrupt if it was masked */
+	napi_schedule(&net->napi);
 
 	ret = tb_xdomain_enable_paths(net->xd, net->local_transmit_path,
 				      net->tx_ring.ring->hop,
@@ -699,7 +710,7 @@ static void tbnet_connected_work(struct work_struct *work)
 				      net->rx_ring.ring->hop);
 	if (ret) {
 		netdev_err(net->dev, "failed to enable DMA paths\n");
-		goto err_free_tx_buffers;
+		goto err_napi;
 	}
 
 	netif_carrier_on(net->dev);
@@ -708,13 +719,13 @@ static void tbnet_connected_work(struct work_struct *work)
 	netdev_dbg(net->dev, "network traffic started\n");
 	return;
 
-err_free_tx_buffers:
-	tbnet_free_buffers(&net->tx_ring);
-err_free_rx_buffers:
-	tbnet_free_buffers(&net->rx_ring);
+err_napi:
+	napi_disable(&net->napi);
 err_stop_rings:
 	tb_ring_stop(net->rx_ring.ring);
 	tb_ring_stop(net->tx_ring.ring);
+	tbnet_free_buffers(&net->rx_ring);
+	tbnet_free_buffers(&net->tx_ring);
 	tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
 	tbnet_connect_failed(net);
 }
@@ -839,9 +850,16 @@ static bool tbnet_check_frame(struct tbnet *net, const struct tbnet_frame *tf,
 static int tbnet_poll(struct napi_struct *napi, int budget)
 {
 	struct tbnet *net = container_of(napi, struct tbnet, napi);
-	unsigned int cleaned_count = tbnet_available_buffers(&net->rx_ring);
 	struct device *dma_dev = tb_ring_dma_device(net->rx_ring.ring);
-	unsigned int rx_packets = 0;
+	unsigned int cleaned_count, rx_packets = 0;
+	bool no_buffers = false;
+
+	if (!budget)
+		return 0;
+
+	/* Buffers a previous run was not able to return to the ring */
+	cleaned_count = tb_ring_size(net->rx_ring.ring) -
+			tbnet_available_buffers(&net->rx_ring);
 
 	while (rx_packets < budget) {
 		const struct thunderbolt_ip_frame_header *hdr;
@@ -858,7 +876,9 @@ static int tbnet_poll(struct napi_struct *napi, int budget)
 		 * time.
 		 */
 		if (cleaned_count >= MAX_SKB_FRAGS) {
-			tbnet_alloc_rx_buffers(net, cleaned_count);
+			if (tbnet_alloc_rx_buffers(net, cleaned_count) ==
+			    -ENOMEM)
+				no_buffers = true;
 			cleaned_count = 0;
 		}
 
@@ -930,12 +950,20 @@ static int tbnet_poll(struct napi_struct *napi, int budget)
 		}
 	}
 
-	if (cleaned_count)
-		tbnet_alloc_rx_buffers(net, cleaned_count);
+	if (cleaned_count &&
+	    tbnet_alloc_rx_buffers(net, cleaned_count) == -ENOMEM)
+		no_buffers = true;
 
 	if (rx_packets >= budget)
 		return budget;
 
+	/*
+	 * We were not able to fill the RX ring for more buffers on this
+	 * run. Keep the polling enabled to retry the allocation.
+	 */
+	if (no_buffers && !tbnet_available_buffers(&net->rx_ring))
+		return budget;
+
 	napi_complete_done(napi, rx_packets);
 	/* Re-enable the ring interrupt */
 	tb_ring_poll_complete(net->rx_ring.ring);
@@ -1001,7 +1029,6 @@ static int tbnet_open(struct net_device *dev)
 	tb_ring_throttling(net->tx_ring.ring, TBNET_THROTTLING);
 	tb_ring_throttling(net->rx_ring.ring, TBNET_THROTTLING);
 
-	napi_enable(&net->napi);
 	start_login(net);
 
 	return 0;
@@ -1011,8 +1038,6 @@ static int tbnet_stop(struct net_device *dev)
 {
 	struct tbnet *net = netdev_priv(dev);
 
-	napi_disable(&net->napi);
-
 	cancel_work_sync(&net->disconnect_work);
 	tbnet_tear_down(net, true);
 
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v3 3/4] net: thunderbolt: Prevent Tx descriptor leak in tbnet_start_xmit()
  2026-11-30 11:36 [PATCH v3 0/4] net: thunderbolt: Various fixes Mika Westerberg
  2026-11-30 11:36 ` [PATCH v3 1/4] net: thunderbolt: Fix race condition when the link is torn down Mika Westerberg
  2026-11-30 11:36 ` [PATCH v3 2/4] net: thunderbolt: Stop queueing Rx buffers if allocation fails Mika Westerberg
@ 2026-11-30 11:36 ` Mika Westerberg
  2026-11-30 11:36 ` [PATCH v3 4/4] net: thunderbolt: Free partially completed skb when the link is torn down Mika Westerberg
  2026-11-30 11:39 ` [PATCH v3 0/4] net: thunderbolt: Various fixes netdev-bot+sinfo
  4 siblings, 0 replies; 8+ messages in thread
From: Mika Westerberg @ 2026-11-30 11:36 UTC (permalink / raw)
  To: netdev
  Cc: Yehezkel Bernat, Andrew Lunn, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Mika Westerberg, Mika Westerberg

From: "Mika Westerberg (Intel)" <westeri@kernel.org>

Sashiko pointed out that tbnet_start_xmit() may leak one Tx
descriptor on three error paths jumping to err_drop because frame_index
is not yet updated but the Tx descriptor is already used. This will
accumulate over time and in the worst case run out of Tx descriptors
completely.

Fix this by subtracting the correct number on error path.

Fixes: e69b6c02b4c3 ("net: Add support for networking over Thunderbolt cable")
Closes: https://sashiko.dev/#/patchset/20260902082128.1148463-1-mika.westerberg%40linux.intel.com
Cc: stable@vger.kernel.org
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/net/thunderbolt/main.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/net/thunderbolt/main.c b/drivers/net/thunderbolt/main.c
index d5e49bc38104..0bf4c8137f21 100644
--- a/drivers/net/thunderbolt/main.c
+++ b/drivers/net/thunderbolt/main.c
@@ -1173,13 +1173,13 @@ static netdev_tx_t tbnet_start_xmit(struct sk_buff *skb,
 	struct tbnet_frame *frames[MAX_SKB_FRAGS];
 	u16 frame_id = atomic_read(&net->frame_id);
 	struct thunderbolt_ip_frame_header *hdr;
+	bool unmap = false, index_used = false;
 	unsigned int len = skb_headlen(skb);
 	unsigned int data_len = skb->len;
 	unsigned int nframes, i;
 	unsigned int frag = 0;
 	void *src = skb->data;
 	u32 frame_index = 0;
-	bool unmap = false;
 	void *dest;
 
 	trace_tbnet_tx_skb(skb);
@@ -1233,6 +1233,7 @@ static netdev_tx_t tbnet_start_xmit(struct sk_buff *skb,
 				src = tbnet_kmap_frag(skb, frag++, &len);
 				unmap = true;
 			} else if (unlikely(size_left > 0)) {
+				index_used = true;
 				goto err_drop;
 			}
 		} while (size_left > 0);
@@ -1269,6 +1270,7 @@ static netdev_tx_t tbnet_start_xmit(struct sk_buff *skb,
 			src = tbnet_kmap_frag(skb, frag++, &len);
 			unmap = true;
 		} else if (unlikely(data_len > 0)) {
+			index_used = true;
 			goto err_drop;
 		}
 	}
@@ -1278,8 +1280,10 @@ static netdev_tx_t tbnet_start_xmit(struct sk_buff *skb,
 	if (unmap)
 		kunmap_local(src);
 
-	if (!tbnet_xmit_csum_and_map(net, skb, frames, frame_index + 1))
+	if (!tbnet_xmit_csum_and_map(net, skb, frames, frame_index + 1)) {
+		index_used = true;
 		goto err_drop;
+	}
 
 	for (i = 0; i < frame_index + 1; i++)
 		tb_ring_tx_more(net->tx_ring.ring, &frames[i]->frame);
@@ -1299,6 +1303,9 @@ static netdev_tx_t tbnet_start_xmit(struct sk_buff *skb,
 err_drop:
 	/* We can re-use the buffers */
 	net->tx_ring.cons -= frame_index;
+	/* Return back the current slot as well */
+	if (index_used)
+		net->tx_ring.cons--;
 
 	dev_kfree_skb_any(skb);
 	net->stats.tx_errors++;
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v3 4/4] net: thunderbolt: Free partially completed skb when the link is torn down
  2026-11-30 11:36 [PATCH v3 0/4] net: thunderbolt: Various fixes Mika Westerberg
                   ` (2 preceding siblings ...)
  2026-11-30 11:36 ` [PATCH v3 3/4] net: thunderbolt: Prevent Tx descriptor leak in tbnet_start_xmit() Mika Westerberg
@ 2026-11-30 11:36 ` Mika Westerberg
  2026-11-30 11:39 ` [PATCH v3 0/4] net: thunderbolt: Various fixes netdev-bot+sinfo
  4 siblings, 0 replies; 8+ messages in thread
From: Mika Westerberg @ 2026-11-30 11:36 UTC (permalink / raw)
  To: netdev
  Cc: Yehezkel Bernat, Andrew Lunn, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Mika Westerberg, Mika Westerberg

From: "Mika Westerberg (Intel)" <westeri@kernel.org>

If tbnet_tear_down() is called while there is a packet consisting of
multiple frames the net->skb may not be released in tbnet_poll() and it
will be leaked. Fix this by freeing it after NAPI has been disabled,
both in tbnet_tear_down() and in the tbnet_connected_work() error path.

Fixes: e69b6c02b4c3 ("net: Add support for networking over Thunderbolt cable")
Cc: stable@vger.kernel.org
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/net/thunderbolt/main.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/net/thunderbolt/main.c b/drivers/net/thunderbolt/main.c
index 0bf4c8137f21..0235016ebf16 100644
--- a/drivers/net/thunderbolt/main.c
+++ b/drivers/net/thunderbolt/main.c
@@ -420,6 +420,9 @@ static void tbnet_tear_down(struct tbnet *net, bool send_logout)
 		tbnet_free_buffers(&net->rx_ring);
 		tbnet_free_buffers(&net->tx_ring);
 
+		dev_kfree_skb_any(net->skb);
+		net->skb = NULL;
+
 		tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
 		net->remote_transmit_path = 0;
 	}
@@ -726,6 +729,8 @@ static void tbnet_connected_work(struct work_struct *work)
 	tb_ring_stop(net->tx_ring.ring);
 	tbnet_free_buffers(&net->rx_ring);
 	tbnet_free_buffers(&net->tx_ring);
+	dev_kfree_skb_any(net->skb);
+	net->skb = NULL;
 	tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
 	tbnet_connect_failed(net);
 }
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 0/4] net: thunderbolt: Various fixes
  2026-11-30 11:36 [PATCH v3 0/4] net: thunderbolt: Various fixes Mika Westerberg
                   ` (3 preceding siblings ...)
  2026-11-30 11:36 ` [PATCH v3 4/4] net: thunderbolt: Free partially completed skb when the link is torn down Mika Westerberg
@ 2026-11-30 11:39 ` netdev-bot+sinfo
  4 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sinfo @ 2026-11-30 11:39 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: netdev, Yehezkel Bernat, Andrew Lunn, David S . Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Mika Westerberg

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 1/4] net: thunderbolt: Fix race condition when the link is torn down
  2026-11-30 11:36 ` [PATCH v3 1/4] net: thunderbolt: Fix race condition when the link is torn down Mika Westerberg
@ 2026-12-01 12:17   ` netdev-bot+sashiko
  0 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01 12:17 UTC (permalink / raw)
  To: mika.westerberg
  Cc: netdev, YehezkelShB, andrew+netdev, davem, edumazet, kuba, pabeni,
	westeri

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 2 (3) · Medium: 1 · Low: 2

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] Because works are re-enabled at the end of every teardown, the
  patch relies on the new netif_running()/netif_device_present() check…
- [High] net: thunderbolt: tbnet_tear_down() re-enables works after stop
  or suspend
- [Medium] net: thunderbolt: race before connection_lock in
  tbnet_tear_down()
- [Low] The patch carries 'Cc: stable@vger.kernel.org' and 'Fixes:
  e69b6c02b4c3' (the v4.15 commit that added the driver).
- [Low] The commit message leaves out a behaviour change the fix depends
  on.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is a peer-
  triggerable use-after-free.
- [High] This problem wasn't introduced by this patch, but the TX path is
  not quiesced in tbnet_tear_down().
- [High] This problem wasn't introduced by this patch, but
  tbnet_handle_packet() overwrites net->remote_transmit_path with the…

--

net: thunderbolt: Fix race condition when the link is torn down

During the teardown, tbnet_tear_down() now disables login_work and
connected_work instead of only cancelling them. It releases the data
path resources only if the carrier was on. tbnet_connected_work() now
also bails out when the netdev is not running or not present.

> diff --git a/drivers/net/thunderbolt/main.c b/drivers/net/thunderbolt/main.c
> index cf51b9c39f4ee..ede105cdc3a37 100644
> --- a/drivers/net/thunderbolt/main.c
> +++ b/drivers/net/thunderbolt/main.c
> @@ -368,15 +368,22 @@ static void tbnet_free_buffers(struct tbnet_ring *ring)
>  
>  static void tbnet_tear_down(struct tbnet *net, bool send_logout)
>  {
> -	netif_carrier_off(net->dev);
> -	netif_stop_queue(net->dev);
> +	bool connected;
> +	int ret;
>  
> -	stop_login(net);
> +	disable_delayed_work_sync(&net->login_work);
> +	disable_work_sync(&net->connected_work);

[Severity: Medium]
Can a peer TBIP_LOGIN slip in between these disables and the
mutex_lock() below?

connection_lock is not held yet, and disable_work_sync() may have to
wait for an in-flight tbnet_connected_work(). Suppose
tbnet_handle_packet() processes a LOGIN in that window. It sends a
positive tbnet_login_response() and takes connection_lock. It then
sets login_received and overwrites net->remote_transmit_path. Its
queue_delayed_work() of login_work and queue_work() of
connected_work both return false because the works are disabled.

Once tbnet_tear_down() gets the lock, two things go wrong.

First, if the old session was connected, tb_xdomain_disable_paths()
and tb_xdomain_release_in_hopid() are given the HopID the peer just
sent. That is not the one tbnet_connected_work() allocated. The
original path stays enabled while the rings are stopped, ida_free()
is called for the wrong ID, and the real Rx HopID leaks.

Second, login_sent and login_received are cleared and the works are
re-enabled. enable_delayed_work() does not replay the queueing that
was refused while the work was disabled. The peer has already had its
login acknowledged, so its tbnet_login_work() sets login_sent and
stops retrying. On this side login_work is idle and login_received
is false. Neither end sends another LOGIN. Doesn't that leave the
link stuck with the carrier off until something else restarts the
login?

One way to reach this is the disconnect_work path, when the peer
sends TBIP_LOGOUT and then a new TBIP_LOGIN shortly after.

Moving the disables under connection_lock would deadlock, since both
works take that lock. How should a LOGIN that arrives during the
teardown be handled? Should tbnet_handle_packet() refuse it while a
teardown is in progress? Or should tbnet_tear_down() restart the
login after it enables the works again?

[Severity: Low]
This patch carries Cc: stable and Fixes: e69b6c02b4c3, which goes back to
v4.15. The disable_delayed_work_sync(), disable_work_sync(),
enable_work() and enable_delayed_work() helpers were added by commit
86898fa6b8cd ("workqueue: Implement disable/enable for (delayed) work
items"). They first appeared in v6.10.

Could the stable tag get a version qualifier such as "# 6.10+", or could
the commit message mention the dependency? As written, the patch won't
build on the 5.4, 5.10, 5.15, 6.1 and 6.6 stable trees that carry this
driver.

>  
>  	mutex_lock(&net->connection_lock);
>  
> +	connected = netif_carrier_ok(net->dev) && net->login_sent &&
> +		    net->login_received;
> +
> +	netif_carrier_off(net->dev);
> +	netif_stop_queue(net->dev);

[Severity: High]
This is a pre-existing issue, but is netif_stop_queue() enough to keep
tbnet_start_xmit() away from the TX buffers here?

On the disconnect_work path (peer sends TBIP_LOGOUT while the interface
is up), netif_stop_queue() only sets the stop bit and does not take the
TX lock. Later in tbnet_tear_down() this happens:

tb_ring_stop(net->tx_ring.ring)
  ring_work()
    tbnet_tx_callback()      /* for each canceled frame */
      net->tx_ring.prod++;
      if (tbnet_available_buffers(&net->tx_ring) >= TBNET_RING_SIZE / 2)
          netif_wake_queue(net->dev);
tbnet_free_buffers(&net->tx_ring)

tbnet_tx_callback() ignores the canceled argument, so the queue is woken
just before the TX pages are unmapped and freed. netif_carrier_off() only
fires a non-urgent linkwatch event, so the qdisc can still call
tbnet_start_xmit() on another CPU in this window.

tbnet_start_xmit() does not check carrier or ring state. It does a
memcpy() into page_address(frames[i]->page). Can that write into a freed
page, or dereference a NULL page once tbnet_free_buffers() clears
tf->page? It also races on tx_ring.cons/prod while they are reset to
zero.

Would netif_tx_disable(), which takes the TX lock, be more appropriate
here?

The NAPI side of this (tbnet_poll() racing with
tbnet_free_buffers(&net->rx_ring)) is handled later in this series by
"net: thunderbolt: Stop queueing Rx buffers if allocation fails". That
patch adds napi_disable() before the rings are stopped. The TX side is
still present at the end of the series.

> +
>  	if (net->login_sent && net->login_received) {

[ ... ]

> @@ -385,7 +392,9 @@ static void tbnet_tear_down(struct tbnet *net, bool send_logout)
>  			if (ret != -ETIMEDOUT)
>  				break;
>  		}
> +	}
>  
> +	if (connected) {
>  		/* Tear the paths down before stopping the rings.  This mirrors
>  		 * tbnet_connected_work(), which enables the paths last so the
>  		 * Rx ring is primed before packets can arrive.  Stopping a

[Severity: High]
This isn't a bug introduced by this patch, but does this release block
still assume that net->remote_transmit_path is the HopID that was
actually allocated?

tbnet_handle_packet() overwrites it with the peer-supplied value on every
TBIP_LOGIN, including while the carrier is on:

    mutex_lock(&net->connection_lock);
    net->login_received = true;
    net->remote_transmit_path = pkg->transmit_path;

The only validation is the one tb_xdomain_alloc_in_hopid() call in
tbnet_connected_work(). A connected_work queued by a repeat LOGIN returns
early because the carrier is already on.

Suppose the peer sends a second LOGIN with a different transmit_path, or
reboots without a LOGOUT and comes back with a different out HopID. The
next teardown then passes the new value to tb_xdomain_disable_paths()
and tb_xdomain_release_in_hopid(), with these effects:

  - The DMA path that was set up with the original HopID stays active
    while tb_ring_stop() and tbnet_free_buffers() run. The comment above
    warns that this state can leave __tb_path_deactivate_hop() waiting
    on the pending bit.

  - tb_xdomain_release_in_hopid() is a plain ida_free(). For an ID that
    was never allocated, this hits the "ida_free called for id=%d which
    is not allocated" WARN. For an ID owned by another user of the same
    XDomain, it frees that ID.

  - The original HopID leaks, so later tb_xdomain_alloc_in_hopid() calls
    fail with "failed to allocate Rx HopID".

tbnet_connected_work() also reads remote_transmit_path three times
without connection_lock: for the alloc, for enable_paths and on the
error-path release. Can a LOGIN arriving in between make those three
uses refer to different HopIDs?

> @@ -419,6 +428,9 @@ static void tbnet_tear_down(struct tbnet *net, bool send_logout)
>  
>  	netdev_dbg(net->dev, "network traffic stopped\n");
>  
> +	enable_work(&net->connected_work);
> +	enable_delayed_work(&net->login_work);

[Severity: High]
connected_work now bails out when the netdev is not running or not
present. These two calls also let login_work run again, and
tbnet_login_work() only looks at netif_carrier_ok(). Is that enough
when tbnet_tear_down() is called from tbnet_stop(), tbnet_suspend()
or tbnet_shutdown()?

tbnet_handle_packet() checks netif_running() before sending the login
response, but it does not check again under connection_lock. A
TBIP_LOGIN that passed that check just before ifdown can block on
connection_lock while this function holds it for the logout retries.
Once the works are re-enabled here and the lock is dropped, it finds
login_sent false and queues login_work.

On the tbnet_stop() path, tbnet_login_work() then keeps sending login
requests that advertise net->local_transmit_path. By then tbnet_stop()
has released that HopID and freed the rings. The work re-arms itself
up to TBNET_LOGIN_RETRIES times. If tbnet_stop() was reached through
unregister_netdev() in tbnet_remove(), nothing cancels login_work or
connected_work before free_netdev(). The work can then run on a freed
struct tbnet.

On the tbnet_suspend() path, netif_device_detach() clears the present
bit, but netif_running() stays true. The protocol handler is only
unregistered after tbnet_tear_down() returns. A LOGIN in that window
queues login_work, which then sends XDomain login requests while the
device is suspended.

tbnet_shutdown() keeps both netif_running() and netif_device_present()
true and leaves the handler registered. The new check in
tbnet_connected_work() therefore does not cover it. A peer LOGIN
after shutdown can run both works and re-enable the DMA paths.

Should tbnet_login_work() get the same running/present check, with
tbnet_handle_packet() re-checking under connection_lock? Or would it
be cleaner for the stop, suspend, shutdown and remove paths to leave
the works disabled instead of re-enabling them at the end of every
teardown?

[Severity: High]
This is a pre-existing issue, but what stops these works from running
after tbnet_remove() has freed struct tbnet? They are now re-enabled at
the end of every teardown, including the one unregister_netdev() runs
through tbnet_stop().

tbnet_remove() does:

    unregister_netdev(net->dev);
    tb_unregister_protocol_handler(&net->handler);
    free_netdev(net->dev);

It never cancels or disables disconnect_work, login_work or
connected_work.

The TBIP_LOGOUT branch of tbnet_handle_packet() queues disconnect_work
without checking netif_running():

    queue_work(system_long_wq, &net->disconnect_work);

A LOGOUT that arrives after tbnet_stop() has cancelled disconnect_work
can therefore queue it again. tbnet_disconnect_work()->tbnet_tear_down()
would then touch net->login_work, net->connection_lock and net->dev
after free_netdev().

Separately, tb_xdomain_handle_request() in drivers/thunderbolt/xdomain.c
drops xdomain_lock around the callback:

    mutex_unlock(&xdomain_lock);
    ret = handler->callback(buf, size, handler->data);
    mutex_lock(&xdomain_lock);

tb_unregister_protocol_handler() only does list_del_init() under that
lock. Can a tbnet_handle_packet() that is already running keep using
net->xd, net->dev and net->connection_lock, or queue works, after
free_netdev()?

> +
>  	mutex_unlock(&net->connection_lock);
>  }

[ ... ]

> @@ -645,7 +657,8 @@ static void tbnet_connected_work(struct work_struct *work)
>  		return;
>  
>  	mutex_lock(&net->connection_lock);
> -	connected = net->login_sent && net->login_received;
> +	connected = net->login_sent && net->login_received &&
> +		   netif_running(net->dev) && netif_device_present(net->dev);

[Severity: High]
tbnet_tear_down() now re-enables the works before it returns, so this
check is what keeps connected_work away from the rings outside their
lifetime. Does netif_running() track ring lifetime closely enough for
that?

__dev_open() sets __LINK_STATE_START before it calls ndo_open. So
netif_running() is already true before tbnet_open() allocates
net->tx_ring.ring and net->rx_ring.ring, and before start_login() resets
the login flags.

One possible sequence:

1. During ifdown, a peer TBIP_LOGIN handler has already passed the
   netif_running() check in tbnet_handle_packet(). It blocks on
   connection_lock while tbnet_tear_down() holds it for the logout
   retries. After teardown re-enables the works and unlocks, the handler
   sets login_received and queues login_work.

   tbnet_login_work() only checks the carrier, so it logs in and sets
   login_sent. connected_work then bails out on !netif_running(). Both
   flags are left set, while the rings are freed and set to NULL in
   tbnet_stop().

2. On ifup, a retried peer LOGIN arrives after __LINK_STATE_START is set.
   It passes netif_running() and queues connected_work. login_work is
   not queued because login_sent is already true.

3a. If connected_work runs before tbnet_open() assigns the rings,
    tb_ring_start(net->tx_ring.ring) dereferences a NULL ring.

3b. If it runs after the rings exist but before start_login() clears the
    flags, it turns the carrier on, and start_login() then clears
    login_sent and login_received. tbnet_login_work() returns early
    because the carrier is ok. On the next teardown this test is false:

    connected = netif_carrier_ok(net->dev) && net->login_sent &&
                net->login_received;

    That skips tb_xdomain_disable_paths(), tb_ring_stop(),
    tbnet_free_buffers() and tb_xdomain_release_in_hopid(). tbnet_stop()
    then calls tb_ring_free() on rings that are still running (the
    "still running" dev_WARN). The descriptors are freed with
    dma_free_coherent() while the hardware ring and DMA path stay
    enabled. The RX/TX pages and the Rx HopID leak as well.

With the rest of the series applied, napi_disable() and the net->skb
free are skipped too. NAPI stays enabled, and the next connected_work's
napi_enable() hits BUG_ON(!test_bit(NAPI_STATE_SCHED, ...)) in
napi_enable_locked().

The commit message says the carrier marks a fully set up data path. The
release test in tbnet_tear_down() also requires the login flags, and
that extra condition is what allows the leak in 3b.

Would keeping the works disabled from ndo_stop until ndo_open has
finished its setup close both windows?

[Severity: Low]
This isn't a bug, but the commit message doesn't mention the new
netif_running() and netif_device_present() check.

tbnet_tear_down() now re-enables login_work and connected_work at the
end. This check is what stops a re-queued connected_work from restarting
the rings after tbnet_stop() frees them, or after netif_device_detach()
on suspend.

Could the commit message describe it, so the check isn't dropped when
the fix is reworked or backported?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130113657.3440005-1-mika.westerberg%40linux.intel.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 2/4] net: thunderbolt: Stop queueing Rx buffers if allocation fails
  2026-11-30 11:36 ` [PATCH v3 2/4] net: thunderbolt: Stop queueing Rx buffers if allocation fails Mika Westerberg
@ 2026-12-01 12:17   ` netdev-bot+sashiko
  0 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01 12:17 UTC (permalink / raw)
  To: mika.westerberg
  Cc: netdev, YehezkelShB, andrew+netdev, davem, edumazet, kuba, pabeni,
	westeri

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] tbnet_connected_work() in drivers/net/thunderbolt/main.c gains a
  napi_enable()/napi_schedule() pair in this patch.

--

net: thunderbolt: Stop queueing Rx buffers if allocation fails

With this patch, tbnet_alloc_rx_buffers() stops and keeps the buffers it
has already queued when an allocation fails, instead of freeing them while
their descriptors are still in the hardware ring. tbnet_poll() keeps NAPI
polling so the allocation can be retried. NAPI enable and disable move into
tbnet_connected_work() and tbnet_tear_down() so that they bracket the time
the rings are running.

Closes: https://sashiko.dev/#/patchset/20260902082128.1148463-1-mika.westerberg%40linux.intel.com

> diff --git a/drivers/net/thunderbolt/main.c b/drivers/net/thunderbolt/main.c
> index ede105cdc3a37..d5e49bc381041 100644
> --- a/drivers/net/thunderbolt/main.c
> +++ b/drivers/net/thunderbolt/main.c

[ ... ]

> @@ -691,7 +698,11 @@ static void tbnet_connected_work(struct work_struct *work)
>  
>  	ret = tbnet_alloc_tx_buffers(net);
>  	if (ret)
> -		goto err_free_rx_buffers;
> +		goto err_stop_rings;
> +
> +	napi_enable(&net->napi);
> +	/* Unmasks the ring interrupt if it was masked */
> +	napi_schedule(&net->napi);

[Severity: Low]
tbnet_connected_work() is a work item queued on system_long_wq. It runs in
a kworker in process context with BH enabled.

Called from there, napi_schedule() leads to:

napi_schedule()
  __napi_schedule()
    ____napi_schedule()
      raise_softirq_irqoff(NET_RX_SOFTIRQ)

Because !in_interrupt(), raise_softirq_irqoff() only marks NET_RX_SOFTIRQ
pending and wakes ksoftirqd. The softirq does not run right away.

Does that mean the first tbnet_poll() waits for ksoftirqd or the next
irq_exit? If so, the tb_ring_poll_complete() call that unmasks the Rx ring
interrupt, which the comment above refers to, waits as well.

Other drivers wrap this call in a BH-disabled section, for example
virtnet_napi_do_enable() in drivers/net/virtio_net.c:

	napi_enable(napi);
	...
	 * Call local_bh_enable after to trigger softIRQ processing.
	 */
	local_bh_disable();
	virtqueue_napi_schedule(napi, vq);
	local_bh_enable();

Would wrapping this napi_schedule() in local_bh_disable() and
local_bh_enable() make the initial poll run as soon as BH is re-enabled?

>  
>  	ret = tb_xdomain_enable_paths(net->xd, net->local_transmit_path,
>  				      net->tx_ring.ring->hop,

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130113657.3440005-1-mika.westerberg%40linux.intel.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-12-01 12:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 11:36 [PATCH v3 0/4] net: thunderbolt: Various fixes Mika Westerberg
2026-11-30 11:36 ` [PATCH v3 1/4] net: thunderbolt: Fix race condition when the link is torn down Mika Westerberg
2026-12-01 12:17   ` netdev-bot+sashiko
2026-11-30 11:36 ` [PATCH v3 2/4] net: thunderbolt: Stop queueing Rx buffers if allocation fails Mika Westerberg
2026-12-01 12:17   ` netdev-bot+sashiko
2026-11-30 11:36 ` [PATCH v3 3/4] net: thunderbolt: Prevent Tx descriptor leak in tbnet_start_xmit() Mika Westerberg
2026-11-30 11:36 ` [PATCH v3 4/4] net: thunderbolt: Free partially completed skb when the link is torn down Mika Westerberg
2026-11-30 11:39 ` [PATCH v3 0/4] net: thunderbolt: Various fixes netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox