Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by Mika Westerberg
Mon, 30 Nov 2026 11:36
[PATCH v3 2/4] net: thunderbolt: Stop queueing Rx buffers if allocation fails
Author: Mika Westerberg
Date: Mon, 30 Nov 2026 11:36
Date: Mon, 30 Nov 2026 11:36
202 lines
16035 bytes
16035 bytes
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
Re: [PATCH v3 2/4] net: thunderbolt: Stop queueing Rx buffers if allocation fails
Author: netdev-bot+sashiko
Date: Tue, 01 Dec 2026 12:17
Date: Tue, 01 Dec 2026 12:17
81 lines
10556 bytes
10556 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads