From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796127464; b=pwbTvwKI4B4J+dOLCT7J7ARD+dDZWfXr3xQpCMRTZ5/SvmcWUjGO9zjEY4bddjoRQbZY mx9ElYwL872HdJrMG7T82tQ0fqPxpxs739mY6pVR/eXGoeCpyN5SBOgM9idJqxe+2UC1o 9tM8Ntl0xrMQIOA4cXSFRlO+gW42bBZg/UibhE564pXquEQTNwNkX3+/jKRSres3Nud8f OgPaSbrAsfPayR+Sisy215xlDd754Iu+ESVdwjs/AEoN2pHzdnsBP7a3j228grBYIXHnG M0XUfwGEGCmau4PXs2O37T6gqaHL5dUa78ICmRnZNkydpsKjuv9Egd5RpMqf/ZRPWOA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796127464; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=07+GynOHviMVrfTbhcR20K9Nu7qrpSLTQDaHrOVO3RU=; b=yIcBLmTb1C0Bv3acguhRfRyMf44lPburvOUKsMl8SsYOzxXtifkavl4t7zpbK7eTQ9zB 58kbBAyb6fBVHwQGRwP6fdvZkrfCi6ZjDMrhEXPtYY80HlLBzpvM8LZFI0nsnHGltR9Za w157WScD6Hix4Zq8+s1WP+xA3aJ1lG0lVXhnLyGIOFt0eyRl2Ak1k6YGfSBQoXzFlFfyY pEA4j27PjO0JCjzojzAS6I21XboRYObct8QfuL0/Sdm8Eurhh1lPHIchwW+UYCJ5DRw+A qEJafS/XcqrvnzLp9J1YtdP2IRDxKZYfOigLGYWKA9ckjEU8v5zc8jriIk2o/KuxBIA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=k2MP8UZ8; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DCE5606A33C for ; Tue, 1 Dec 2026 12:17:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=k2MP8UZ8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1BA3F1F00897; Tue, 1 Dec 2026 12:17:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796127463; bh=07+GynOHviMVrfTbhcR20K9Nu7qrpSLTQDaHrOVO3RU=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=k2MP8UZ853GA7ws+dAktwUq/D3UOuCPRD5b+iDe2+8hreLuxtglgrk/rl37GTBTzn DTYgnZsSljY2QVDWlpIG/aCOM7X2VtYXEko8+qpEi8z0U9PZDhPJZkTPWXCh6WDeNJ Bk2tTN4v7lRg5j9rBZQ7tWILug2EwcSzkvHEQ1QwaYAIiSeDTRUsen1O0NuefFvjvt QPIojtyKsid82+8bgNJMUTIP9Uv8OOa8ZEhQXhqFnxRR3cYCAJwl6EqoQHiJdGAilq VGpHyjx+BDUlmm72AVzz2t6VRrRuNEg5d8NgV0Y+DvHRyxpTv2mgXye7jVrSeFW1dU zPZnnYBR/1kjQ== Subject: Re: [PATCH v3 1/4] net: thunderbolt: Fix race condition when the link is torn down From: netdev-bot+sashiko@kernel.org To: mika.westerberg@linux.intel.com Cc: netdev@vger.kernel.org, YehezkelShB@gmail.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, westeri@kernel.org Date: Tue, 01 Dec 2026 12:17:42 +0000 Message-ID: <179612746262.3146.10789958014998165383@kernel.org> In-Reply-To: <20261130113657.3440005-2-mika.westerberg@linux.intel.com> References: <20261130113657.3440005-2-mika.westerberg@linux.intel.com> X-sashiko-severity: High Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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