Netdev List
 help / color / mirror / Atom feed
* [PATCH v2 0/3] net: thunderbolt: Various fixes
@ 2026-11-16 14:26 Mika Westerberg
  2026-11-16 14:26 ` [PATCH v2 1/3] net: thunderbolt: Stop queueing Rx buffers if allocation fails Mika Westerberg
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Mika Westerberg @ 2026-11-16 14:26 UTC (permalink / raw)
  To: netdev
  Cc: Yehezkel Bernat, Andrew Lunn, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Mika Westerberg

Hi all,

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

The previous version of the series can be found:

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

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 (3):
  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 | 98 +++++++++++++++++++++++++---------
 1 file changed, 74 insertions(+), 24 deletions(-)

-- 
2.50.1


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

* [PATCH v2 1/3] net: thunderbolt: Stop queueing Rx buffers if allocation fails
  2026-11-16 14:26 [PATCH v2 0/3] net: thunderbolt: Various fixes Mika Westerberg
@ 2026-11-16 14:26 ` Mika Westerberg
  2026-11-20  5:29   ` netdev-bot+sashiko
  2026-11-16 14:26 ` [PATCH v2 2/3] net: thunderbolt: Prevent Tx descriptor leak in tbnet_start_xmit() Mika Westerberg
  2026-11-16 14:26 ` [PATCH v2 3/3] net: thunderbolt: Free partially completed skb when the link is torn down Mika Westerberg
  2 siblings, 1 reply; 6+ messages in thread
From: Mika Westerberg @ 2026-11-16 14:26 UTC (permalink / raw)
  To: netdev
  Cc: Yehezkel Bernat, Andrew Lunn, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Mika Westerberg

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 <mika.westerberg@linux.intel.com>
---
 drivers/net/thunderbolt/main.c | 84 +++++++++++++++++++++++++---------
 1 file changed, 62 insertions(+), 22 deletions(-)

diff --git a/drivers/net/thunderbolt/main.c b/drivers/net/thunderbolt/main.c
index cf51b9c39f4e..1593ad76e645 100644
--- a/drivers/net/thunderbolt/main.c
+++ b/drivers/net/thunderbolt/main.c
@@ -154,6 +154,7 @@ struct tbnet_ring {
  * @handler: ThunderboltIP configuration protocol handler
  * @dev: Networking device
  * @napi: NAPI structure for Rx polling
+ * @napi_enabled: Is NAPI currently enabled
  * @stats: Network statistics
  * @skb: Network packet that is currently processed on Rx path
  * @command_id: ID used for next configuration protocol packet
@@ -185,6 +186,7 @@ struct tbnet {
 	struct tb_protocol_handler handler;
 	struct net_device *dev;
 	struct napi_struct napi;
+	bool napi_enabled;
 	struct tbnet_stats stats;
 	struct sk_buff *skb;
 	atomic_t command_id;
@@ -366,6 +368,21 @@ static void tbnet_free_buffers(struct tbnet_ring *ring)
 	ring->prod = 0;
 }
 
+static inline void tbnet_napi_enable(struct tbnet *net)
+{
+	napi_enable(&net->napi);
+	net->napi_enabled = true;
+	/* Run once in case an Rx interrupt arrived while NAPI was disabled */
+	napi_schedule(&net->napi);
+}
+
+static inline void tbnet_napi_disable(struct tbnet *net)
+{
+	if (net->napi_enabled)
+		napi_disable(&net->napi);
+	net->napi_enabled = false;
+}
+
 static void tbnet_tear_down(struct tbnet *net, bool send_logout)
 {
 	netif_carrier_off(net->dev);
@@ -404,6 +421,8 @@ static void tbnet_tear_down(struct tbnet *net, bool send_logout)
 		if (ret)
 			netdev_warn(net->dev, "failed to disable DMA paths\n");
 
+		tbnet_napi_disable(net);
+
 		tb_ring_stop(net->rx_ring.ring);
 		tb_ring_stop(net->tx_ring.ring);
 		tbnet_free_buffers(&net->rx_ring);
@@ -506,7 +525,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);
@@ -524,14 +543,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;
@@ -540,16 +561,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;
 }
 
@@ -678,7 +702,9 @@ 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;
+
+	tbnet_napi_enable(net);
 
 	ret = tb_xdomain_enable_paths(net->xd, net->local_transmit_path,
 				      net->tx_ring.ring->hop,
@@ -686,7 +712,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);
@@ -695,13 +721,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:
+	tbnet_napi_disable(net);
 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);
 }
@@ -826,9 +852,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;
@@ -845,7 +878,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;
 		}
 
@@ -917,12 +952,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);
@@ -988,7 +1031,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;
@@ -998,8 +1040,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] 6+ messages in thread

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

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 <mika.westerberg@linux.intel.com>
---
 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 1593ad76e645..f76b4d134b33 100644
--- a/drivers/net/thunderbolt/main.c
+++ b/drivers/net/thunderbolt/main.c
@@ -1175,13 +1175,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);
@@ -1235,6 +1235,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);
@@ -1271,6 +1272,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;
 		}
 	}
@@ -1280,8 +1282,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);
@@ -1301,6 +1305,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] 6+ messages in thread

* [PATCH v2 3/3] net: thunderbolt: Free partially completed skb when the link is torn down
  2026-11-16 14:26 [PATCH v2 0/3] net: thunderbolt: Various fixes Mika Westerberg
  2026-11-16 14:26 ` [PATCH v2 1/3] net: thunderbolt: Stop queueing Rx buffers if allocation fails Mika Westerberg
  2026-11-16 14:26 ` [PATCH v2 2/3] net: thunderbolt: Prevent Tx descriptor leak in tbnet_start_xmit() Mika Westerberg
@ 2026-11-16 14:26 ` Mika Westerberg
  2026-11-20  5:29   ` netdev-bot+sashiko
  2 siblings, 1 reply; 6+ messages in thread
From: Mika Westerberg @ 2026-11-16 14:26 UTC (permalink / raw)
  To: netdev
  Cc: Yehezkel Bernat, Andrew Lunn, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Mika Westerberg

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 in tbnet_tear_down() after NAPI
has been disabled.

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

diff --git a/drivers/net/thunderbolt/main.c b/drivers/net/thunderbolt/main.c
index f76b4d134b33..2b95df73c6e5 100644
--- a/drivers/net/thunderbolt/main.c
+++ b/drivers/net/thunderbolt/main.c
@@ -428,6 +428,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;
 	}
-- 
2.50.1


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

* Re: [PATCH v2 1/3] net: thunderbolt: Stop queueing Rx buffers if allocation fails
  2026-11-16 14:26 ` [PATCH v2 1/3] net: thunderbolt: Stop queueing Rx buffers if allocation fails Mika Westerberg
@ 2026-11-20  5:29   ` netdev-bot+sashiko
  0 siblings, 0 replies; 6+ messages in thread
From: netdev-bot+sashiko @ 2026-11-20  5:29 UTC (permalink / raw)
  To: mika.westerberg
  Cc: netdev, YehezkelShB, andrew+netdev, davem, edumazet, kuba, pabeni

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

Critical: 0 · High: 0 · Medium: 1 · Low: 0

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).

- [Medium] This patch moves NAPI enable/disable into the connection
  lifecycle, but the new code is not serialized with teardown.

--

net: thunderbolt: Stop queueing Rx buffers if allocation fails

When Rx buffer allocation fails, tbnet_alloc_rx_buffers() no longer frees
buffers that are already queued, and the callers are updated to match.
If tbnet_poll() cannot refill the ring, it keeps polling so it can retry.
NAPI enable/disable moves out of ndo_open/ndo_stop and into the connection
lifecycle: it is enabled after the rings are primed and disabled before
they are stopped.

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 cf51b9c39f4ee..1593ad76e645a 100644
> --- a/drivers/net/thunderbolt/main.c
> +++ b/drivers/net/thunderbolt/main.c

[ ... ]

> @@ -366,6 +368,21 @@ static void tbnet_free_buffers(struct tbnet_ring *ring)
>  	ring->prod = 0;
>  }
>  
> +static inline void tbnet_napi_enable(struct tbnet *net)
> +{
> +	napi_enable(&net->napi);
> +	net->napi_enabled = true;
> +	/* Run once in case an Rx interrupt arrived while NAPI was disabled */
> +	napi_schedule(&net->napi);
> +}
> +
> +static inline void tbnet_napi_disable(struct tbnet *net)
> +{
> +	if (net->napi_enabled)
> +		napi_disable(&net->napi);
> +	net->napi_enabled = false;
> +}
> +
>  static void tbnet_tear_down(struct tbnet *net, bool send_logout)
>  {
>  	netif_carrier_off(net->dev);
> @@ -404,6 +421,8 @@ static void tbnet_tear_down(struct tbnet *net, bool send_logout)
>  		if (ret)
>  			netdev_warn(net->dev, "failed to disable DMA paths\n");
>  
> +		tbnet_napi_disable(net);
> +
>  		tb_ring_stop(net->rx_ring.ring);
>  		tb_ring_stop(net->tx_ring.ring);
>  		tbnet_free_buffers(&net->rx_ring);

[ ... ]

> @@ -678,7 +702,9 @@ 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;
> +
> +	tbnet_napi_enable(net);

[Severity: Medium]
Is this tbnet_napi_enable() serialized against tbnet_tear_down()?

tbnet_connected_work() reads login_sent && login_received once under
connection_lock and then drops the lock. Nothing checks the state again
before the rings are restarted and NAPI is enabled here.

On the teardown side, stop_login() calls
cancel_work_sync(&net->connected_work) before tbnet_tear_down() takes
connection_lock. A TBIP_LOGIN from the peer can still queue connected_work
after that point:

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

    queue_work(system_long_wq, &net->connected_work);

This can happen on the disconnect_work path (peer LOGOUT followed by
LOGIN), or during suspend while netif_running() is still true. If
connected_work gets connection_lock first, it sees connected == true,
because login_sent is still set from the old session.

If the peer reuses the same HopID, tbnet_tear_down() disables NAPI, stops
the rings, frees the buffers, releases the HopID and clears both login
flags. connected_work then allocates the HopID, restarts the rings and
calls tbnet_napi_enable(). NAPI is now enabled while login_sent and
login_received are both false.

The next tbnet_stop()->tbnet_tear_down() then skips the block guarded by:

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

So neither tbnet_napi_disable() nor tb_ring_stop() runs. Because this
patch removes the unconditional napi_disable() from tbnet_stop(),
tb_ring_free() runs and net->rx_ring.ring is set to NULL while NAPI is
still enabled.

Could an already scheduled or busy-poll tbnet_poll() then run against a
freed or NULL Rx ring? On the next ndo_open, would napi_enable() on an
already enabled NAPI hit the BUG_ON(!test_bit(NAPI_STATE_SCHED, &val)) in
napi_enable_locked()?

If the peer's new LOGIN carries a different transmit_path,
tb_xdomain_alloc_in_hopid() succeeds right away, and connected_work can
run at the same time as the locked part of tbnet_tear_down().
tbnet_napi_enable() can then run before tbnet_napi_disable(), which calls
napi_enable() on an enabled NAPI. Or it can run after it, in which case
napi_schedule() lets tbnet_poll() run at the same time as tb_ring_stop()
and tbnet_free_buffers(&net->rx_ring).

Does that match this part of the commit message?

    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

napi_enabled is a plain bool. It is written in tbnet_connected_work()
(including the err_napi path) without connection_lock, and read and
cleared in tbnet_tear_down() under connection_lock. Can it reliably track
whether NAPI is enabled?

The race between connected_work and tbnet_tear_down() itself seems to
predate this patch: rings could already be restarted with the login flags
cleared. This patch adds NAPI enable/disable to that unserialized handoff
and drops the unconditional napi_disable() from tbnet_stop().

Would it help to re-check the connection state and do the ring start and
NAPI enable under connection_lock (or with a generation/state check)? Or
to make the NAPI disable in tbnet_tear_down() independent of the login
flag test?

>  
>  	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/20261116142613.876208-1-mika.westerberg%40linux.intel.com

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

* Re: [PATCH v2 3/3] net: thunderbolt: Free partially completed skb when the link is torn down
  2026-11-16 14:26 ` [PATCH v2 3/3] net: thunderbolt: Free partially completed skb when the link is torn down Mika Westerberg
@ 2026-11-20  5:29   ` netdev-bot+sashiko
  0 siblings, 0 replies; 6+ messages in thread
From: netdev-bot+sashiko @ 2026-11-20  5:29 UTC (permalink / raw)
  To: mika.westerberg
  Cc: netdev, YehezkelShB, andrew+netdev, davem, edumazet, kuba, pabeni

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

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

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] The patch only partly closes the leak it claims to fix.

Pre-existing issues:
- [High] tbnet_connected_work() and tbnet_tear_down() are not serialized.
- [Medium] tbnet_tear_down() treats `login_sent && login_received` as
  proof that tbnet_connected_work() acquired the connection resources.

--

net: thunderbolt: Free partially completed skb when the link is torn down

This patch frees a partially assembled multi-frame Rx skb (net->skb) in
tbnet_tear_down(), after NAPI has been disabled and the rings stopped.
This keeps the skb from leaking when the link goes down mid-packet.

> diff --git a/drivers/net/thunderbolt/main.c b/drivers/net/thunderbolt/main.c
> index f76b4d134b330..2b95df73c6e51 100644
> --- a/drivers/net/thunderbolt/main.c
> +++ b/drivers/net/thunderbolt/main.c
> @@ -428,6 +428,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;
> +

[Severity: High]
Does the err_napi path in tbnet_connected_work() need the same free?

It runs the same Rx quiesce sequence but leaves net->skb set:

drivers/net/thunderbolt/main.c:tbnet_connected_work() {
    ...
err_napi:
	tbnet_napi_disable(net);
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);
}

tbnet_napi_enable() is called before tb_xdomain_enable_paths(). With the
software connection manager, tb_tunnel_alloc_dma() places the "DMA RX"
path before "DMA TX" in tunnel->paths[], and tb_tunnel_activate()
activates the paths in that order. Rx can therefore be live while Tx
activation is still running.

If the peer sends a multi-frame packet in that window, tbnet_poll()
stores the partial skb in net->skb. Suppose Tx path activation then fails
(a hop write error or a cable unplug). Control goes to err_napi. The
pages held by the skb were already detached from the ring
(tf->page = NULL), so tbnet_free_buffers() does not free them.

tbnet_connect_failed() then clears login_sent. After that, every
tbnet_tear_down() call from tbnet_stop(), tbnet_shutdown() or
tbnet_remove() skips the block that contains the new free:

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

Wouldn't free_netdev() then leak the skb along with up to
MAX_SKB_FRAGS+1 Rx pages?

If the peer logs in again instead, tbnet_poll() starts with the stale
net->skb and net->rx_hdr. Could tbnet_check_frame() then take the middle
of packet branch and drop the first valid frame of the new connection?

Would it be better to free the skb at the common Rx quiesce point, for
example in tbnet_napi_disable() after napi_disable(), or also at
err_napi?

[Severity: Medium]
This isn't a bug introduced by this patch, but the block that holds this
free treats login_sent && login_received as proof that
tbnet_connected_work() allocated the HopID and started the rings. Is
that always true?

tbnet_login_work() sets login_sent and queues connected_work.
tbnet_handle_packet() sets login_received and also queues it. If
tbnet_tear_down() runs from ndo_stop, shutdown, suspend or
disconnect_work before that work executes, stop_login() cancels it:

static void stop_login(struct tbnet *net)
{
	cancel_delayed_work_sync(&net->login_work);
	cancel_work_sync(&net->connected_work);
	...
}

Both flags are still true, so the teardown block runs anyway. Wouldn't
tb_ring_stop() then hit the "already stopped" dev_WARN for rings that
were never started? And wouldn't tb_xdomain_release_in_hopid() trigger
the ida_free() "which is not allocated" WARN for a HopID that was never
allocated?

The peer chooses remote_transmit_path. If another in-HopID user on the
same XDomain holds that ID, could ida_free() release that user's
allocation instead? That part is speculative.

The new dev_kfree_skb_any(NULL) itself is harmless in this case.

[Severity: High]
This is a pre-existing issue, but this free assumes two things: that NAPI
is quiesced, and that this block actually runs. Are
tbnet_connected_work() and tbnet_tear_down() serialized enough for that?

tbnet_tear_down() calls stop_login() before it takes connection_lock. The
protocol handler is still registered, so a TBIP_LOGIN that arrives in
that gap can requeue connected_work:

drivers/net/thunderbolt/main.c:tbnet_handle_packet() {
    ...
			mutex_unlock(&net->connection_lock);

			queue_work(system_long_wq, &net->connected_work);
    ...
}

tbnet_connected_work() checks the flags under the lock, then drops it.
The HopID allocation, tb_ring_start(), the buffer allocation,
tbnet_napi_enable() and tb_xdomain_enable_paths() all run without the
lock.

If the old connection still holds the HopID,
tb_xdomain_alloc_in_hopid() returns a different ID and
tbnet_connect_failed() clears login_sent. If that happens before
tbnet_tear_down() takes the lock, wouldn't the whole teardown block be
skipped? That would leak the paths, the NAPI state, the running rings,
the buffers, the HopID and net->skb. tbnet_stop() would then call
tb_ring_free() on running rings while the DMA paths are still active.

If connected_work gets past the HopID allocation instead, its
tb_ring_start(), tbnet_alloc_rx_buffers() and tbnet_napi_enable() calls
can run concurrently with the tb_ring_stop(), tbnet_free_buffers() and
dev_kfree_skb_any(net->skb) here.

tbnet_napi_disable() also checks a plain bool that connected_work writes
without the lock:

static inline void tbnet_napi_enable(struct tbnet *net)
{
	napi_enable(&net->napi);
	net->napi_enabled = true;
	...
}

static inline void tbnet_napi_disable(struct tbnet *net)
{
	if (net->napi_enabled)
		napi_disable(&net->napi);
	net->napi_enabled = false;
}

If tear_down reads napi_enabled before it is set, napi_disable() is
skipped. Can tbnet_poll() then run while the rings, buffers and net->skb
are being freed?

In that case connected_work also finishes bringing the link up with the
login flags already cleared, so later tear_down calls skip cleanup. If
NAPI was still enabled from the old connection, could napi_enable() also
hit its BUG_ON?

The peer controls when TBIP_LOGIN and TBIP_LOGOUT arrive. A LOGOUT also
leads to tbnet_tear_down() through disconnect_work.

The unlocked check-then-setup in connected_work and the stop_login()
call outside the lock predate this series. The napi_enabled flag was
added earlier in this series by "net: thunderbolt: Stop queueing Rx
buffers if allocation fails".

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

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

end of thread, other threads:[~2026-11-20  5:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 14:26 [PATCH v2 0/3] net: thunderbolt: Various fixes Mika Westerberg
2026-11-16 14:26 ` [PATCH v2 1/3] net: thunderbolt: Stop queueing Rx buffers if allocation fails Mika Westerberg
2026-11-20  5:29   ` netdev-bot+sashiko
2026-11-16 14:26 ` [PATCH v2 2/3] net: thunderbolt: Prevent Tx descriptor leak in tbnet_start_xmit() Mika Westerberg
2026-11-16 14:26 ` [PATCH v2 3/3] net: thunderbolt: Free partially completed skb when the link is torn down Mika Westerberg
2026-11-20  5:29   ` netdev-bot+sashiko

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