Article View: linux.kernel.netdev
Article #24582[PATCH v3 2/4] net: thunderbolt: Stop queueing Rx buffers if allocation fails
From: 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
Technical Details
Message-ID:
<20261130113657.3440005-3-mika.westerberg@linux.intel.com>Path:
mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374141-linux-kernel-netdev=dewy.ruReferences:
<20261130113657.3440005-1-mika.westerberg@linux.intel.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374141-linux-kernel-netdev=dewy.ru From: mika.westerberg@linux.intel.com (Mika Westerberg) Newsgroups: linux.kernel.netdev Subject: [PATCH v3 2/4] net: thunderbolt: Stop queueing Rx buffers if allocation fails Date: Mon, 30 Nov 2026 12:36:55 +0100 Organization: Usenet Network Message-ID: <20261130113657.3440005-3-mika.westerberg@linux.intel.com> References: <20261130113657.3440005-1-mika.westerberg@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1414517"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:g4Sy91ObyP4AJzhTKo6zuPXSpuY= sha256:V5dWtT9xNKLfrZRt531tz9U1HTvDJCwa0/IkXQkS2Ms= sha1:OwWu/dzjKX26t4LMmqeUitcUCmU= sha256:+rhssEPs+oAJJR/k77+AgDbRDnQrUqZR4BcPc2IK6KA= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 207D5125FDD; Mon, 30 Nov 2026 14:37:26 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -2.952 X-Spam-Status: No, score=-2.952 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=intel.com X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xPnQ6RRJLBPZ; Mon, 30 Nov 2026 14:37:22 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-374141-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=linux.intel.com X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4]) by mail.dewy.ru (Postfix) with ESMTPS id 65C19125FE0; Mon, 30 Nov 2026 14:37:11 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sin.lore.kernel.org (Postfix) with ESMTP id 43F0821C19; Mon, 30 Nov 2026 11:37:08 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796038624; b=0UUO6tEbtSu3Ehak6WFhB+aEVFHOju9PkfwjV/Aq7ZhKN+KWIYS6fQSNZl97F+bh066B LDixm4MqxHpvPqhFlGmLKRZhXt0sN7n0jtqdQkXW0F7Gt0U0PBbqkPEGgRr/vIxuZ6zdi yvoQQUsgDmsYpz3lxoks7iqMmTNdzJ3baDnWlp/VVmlKSSioTeDEnDJ2fYB+y1yIndxFG mNnlfEj32rM7hPAwcfqFkcTjO8FOtZuvwKFvQxT6yikJYfk6zV9kWGo4WPcXW7nvQLZ2a 1aFiKOcKVMFgS7Xm9lPVUv6/0vmGWZZDGX6a4EZoCXd6BEXv6mdh9HbPtO29hxJz9rg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796038624; h=X-Original-To:Received:DMARC-Filter:DKIM-Signature: X-CSE-ConnectionGUID:X-CSE-MsgGUID:X-IronPort-AV:X-IronPort-AV: Received:X-CSE-ConnectionGUID:X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV: Received:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-Mailing-List:List-Id:List-Post:List-Help: List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding; bh=Ylwhu4nMS8/fvTxf1UQQjHUAJVuooQ8LwTZLXOadFJo=; b=vcTiUObaE/DXnEuXxveJYY+CMB3oPNY+ej773i1VUz/G6rBDXmeMQPz+dsFWky6SG0kd 9feuiM0zl+PfMHvEYZLOR6eoTWXjbxZFLPx1fFajBhjPUyHGnLECAO00U+T1CX4xXkDwb TyT2yOLTCfYHLijzglmDGBrlOWEZxEOSYb6lLQvoP/oiJbAgcIDyMUhVPI5pFdPQkqi4g dWvWTCwRqvas7H58jGKNr1R/ksylKPM4lY8VHzO0UwzePbDAsx3YChwflMIBfiZVDGxYu 4jVsyYisaWEOo+krXkcu4TKm98VUIsl+Gnqo83zM7V+TS7zIat5uA35Uu6Cn+EQWuyg== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=My858y6L; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4BE2A606A33A; Mon, 30 Nov 2026 11:37:04 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=My858y6L X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.12 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796038623; b=UG/26jyiq+2mRXMPhqN75NMcARIFWRy1E5iQ5KB59CBCr5i8api2Zh2NVaBYqz24zQ4B OO4OHBT+KlgrnrR0PyYFBpV4hTHK2FUnUknmyV33j/3I0kizhVq87iTQ7MSI9/AbZEm2y ijbVp1tydjcnXg/BwoJ4nRl8Lqm2zXVKvxCoL0J8vwSWH1RRqMJDPKnzPGdLGNRfTniKF V/W2/hTGjOHYgmOd0HczFsKDkdv16Nd3BpYxTApgIAdX8EJ2Szt+RRR4vW6ymAmyV2S5d LSn5WQbjDVWOsGqPFWZGGfKU6jpVOJSCTL769cL2mlojNQ33GOgGv11P7Dcu2hKE+tQ== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796038623; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Received:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Ylwhu4nMS8/fvTxf1UQQjHUAJVuooQ8LwTZLXOadFJo=; b=AyoV9dqh9NuSJxv1GAbWnBH5prnx54FzwAw436WbIM8vqbv/GYRXZ7dipUmX8sgAKtk7 0BcyJo+OhA0yr+lsQ+hUya7teFMxAjOpGZi2lGHynmGeWv5C/51HpvmKvUTrMIXWjwDv6 d1DWp3FE90XzrVWB7FFVuNuU5l8EHQpZRIhNyizxeuBu9nqAej2dVl4JLhV8cpEqS84uX fjB24FE+wF3xpml4KA4a9pTKg+ZBK5P3XIdH1aG599wJZRf/c3solJ74hZ7f333BQfO7w BFa3psFy9+ofBhV2KM67Nij0F4vnOx2lX8VwmgRVjE0m4+qF4iO2C8uYUrFTrq5J1pQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=My858y6L; arc=none smtp.remote-ip=198.175.65.12 X-Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 026DC606A68A for <netdev@vger.kernel.org>; Mon, 30 Nov 2026 11:37:03 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 026DC606A68A X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796038623; x=1827574623; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ylwhu4nMS8/fvTxf1UQQjHUAJVuooQ8LwTZLXOadFJo=; b=My858y6LKZ3qjZeJ5gyFFDfK+3D5EVIQ/NCV4OSbBwPJHqcBtKdArEsO zBLqLxu6Ejzm3E2iIBgWqk18PS8KwtRz3JZJ+ZPfWuleymxEVeZmXLE+T y4b65DLDkC0YrSpuv23oAIWXg3BITefTpzMTh5/GhIGkHVSmV6LP4c5e+ NWjCwv07mqsB9pp5yHcye0DocfgwsaW1GerN7MD+5NqCSwBXuAZgFO6IB Y4zUpmH1SXWrVclpD6Fng33kx1PSZGI9Nwh3Gz/U3DH3DuocZIOZWd2Bx pVR5mPN8IeqzhYthVu/Bd7gWEKTf0i/lf24YUlw5u9n9Af22gmgtofYC3 g==; X-CSE-ConnectionGUID: +Xgtc7lKQ3qXvKAlRo3TKw== X-CSE-MsgGUID: P5tucp/MRgmMGT8+bRC9IQ== X-IronPort-AV: E=McAfee;i="6800,10657,11981"; a="4769630" X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="4769630" X-Received: from fmviesa011.fm.intel.com ([10.60.135.151]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 03:37:01 -0800 X-CSE-ConnectionGUID: jMxGthm4RwyxiiSmDT7ZVg== X-CSE-MsgGUID: OwmzA7bkQ8inFGMNbrIDgw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="14671905" X-Received: from black.igk.intel.com ([10.91.253.5]) by fmviesa011.fm.intel.com with ESMTP; 30 Nov 2026 03:36:58 -0800 X-Received: by black.igk.intel.com (Postfix, from userid 1001) id A24959D; Mon, 30 Nov 2026 12:36:57 +0100 (CET) X-Cc: Yehezkel Bernat <YehezkelShB@gmail.com>, Andrew Lunn <andrew+netdev@lunn.ch>, "David S . Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@kernel.org>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Mika Westerberg <mika.westerberg@linux.intel.com>, Mika Westerberg <westeri@kernel.org> X-Mailer: git-send-email 2.50.1 X-In-Reply-To: <20261130113657.3440005-1-mika.westerberg@linux.intel.com> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list