From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796257497; b=qgorLqjbbYHkZ2TFFZx0y8bqJxK8F2y9mlk7eOKcOZ1Olx0MYzls4vhaZ800F/6fUd54 PTYypF0OA8/Vk+wbVZWAF2mK/G9YU1Tiv+unoL5lq8dxR4a9RvWGzVS1R3EcXXVulUST0 V2smbOrBSbGKWSdtNXiCiGfeN6xpqKv0hHy97KOK4bcenX4sJzYs3Efj2Lg9ERqIeVvHR oGwHijKduDFQ72bhlODOzk2iANhBEe4tR31jA7fEHdLIA3WKoigoTDOD+nnbNRo67xkXR E6xvZaKe5M3+9q2a1ORI+hwun7o1WDLeHna88PLkbzFuipb8/W2evkTG9DbjkAILGnw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796257497; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=jOrp88fGgTT16PcLGopftyZMgp5nwe8TpoWbDiiIgRY=; b=Xu7aBeEnwgsG0yuycamFySiJl+lXJ2988ntGDagy7iqWLJxa5OiYmiBFmvrB0eUhAXnL zwclJ6HhvRnRO9vCbArRLSmXDn4tGpWfTbvM970Tr0z711Zthzaihw+ZKHWmOv06OhHLU oR6DJ2EtcJWS5BLaqt42Y3TiWyfwnqYoe1w8bTOjEzLou5ao8DpVKLicqCICLLukZ6Lg3 WoEqbYOo2swtmFuwMkgkKZtf+9gQ1mitsebjHGe1mfJP3HB4+2BfKkXtrJtwWrB7cQ8gP J91INTz/qcW66OfmhaiocAUaK42JVVG6aC5iViJQ64U4Ym6OkNGPxnYF0CatYGQ+JpA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=WCLF7d+o; arc=none smtp.remote-ip=74.125.82.54 Received: from mail-dl1-f54.google.com (mail-dl1-f54.google.com [74.125.82.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36BCB606A334 for ; Thu, 3 Dec 2026 00:24:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 36BCB606A334 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=WCLF7d+o Received: by mail-dl1-f54.google.com with SMTP id a92af1059eb24-170135e43c7so8049649c88.0 for ; Wed, 02 Dec 2026 16:24:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796257496; x=1796862296; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jOrp88fGgTT16PcLGopftyZMgp5nwe8TpoWbDiiIgRY=; b=WCLF7d+o7yudU2Jtn1Xsk0FY/6gc0y/9zoHXGDMjzM9f3gN2p9ZKEVUX6L1TaLiGd+ rCxjUKk15agXTPLCfG9M6PfZ+GtT04gnLiXEjj12I4fmzYOFB3Q/NHRQ/6APOf8ErETY obBLiIm/QtM7FIGCGptnGQG7MlpyzbxhGThlqaKSqhcir7uZHu6bNxlH8oDPWZAlxQY3 kiDM4IPGv6E9cbfFMYCNiimim3yGQQlT40GQrfK1r85UWsb435+aW8hzmiJbeT+cI90i TnqucKaiUWu3lN755WYYGRW185PSdfoVhVMqpw1/14pFjldMNnSnA4Bo6aX2N/q/itzo xq2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796257496; x=1796862296; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=jOrp88fGgTT16PcLGopftyZMgp5nwe8TpoWbDiiIgRY=; b=Q5tH35/b8Y1typTuotIfMo3V/DDDxDvhXvrvZZEi+9oPPxNj8H3vcXdey/qclfX5ML Gt5HucYBZIFlwbCBItuxOzfVvNzYgbLCfEBrx37pv3nZwpx0Ucbb7he+JA1uhtRUcY4y 0qfJ8M1cC0qD6CcZSs3pmxYdNKj4qmQPXTY4ROhR986X9vCDHePmqX2rj+e8Cf6/k1v3 JETakhJB7eosqJVuEISn+wrY8kTNoWmJN04IKHXMMKz1zYZsp+aALYEvbOvSPtvqmhfX j5WoCeH2Jitwf3Oe6ncUIOLPACqsS7ENJJV0/RIU8OUULji/2o6eDoRylAN2pxZwHsZq EDfw== X-Forwarded-Encrypted: i=1; AL+hErEH99UFqapmAiOXwiFCuO2pYLSfRISJk4C+o1KrelvE3ikV/bw6HKBtIcs9yTFfX4hxxQXUKn8=@vger.kernel.org X-Gm-Message-State: APuU4Z9eJUT51ilKYR1Fz3bwY97j3bIdtknXiggTCHK8rYZGScAJR2uh tEIAL7Q943uyaYywcjovKgt2cDxSHKZwQUIuMptx+qY8TysFdH0Gg9PX X-Gm-Gg: AZFdTZcaPWsrNmgdoU2tkjsMo8Ed8+6RGluVwbX+YAzOeSq5fbyc7OmAPFKUjFoZBVq b/WiDwZlHyls2+/mTRb4SYAxUXrFZpsqvcMzJFLGpWzw1lo2HC5BdwECuHRm2zk6mh/oXAi7icd 5Vc3FJ3Qzc0SQvnmi7vjAepcaZF3jm7l2Fg/AHXUYCSAMtxIDo+bIFj+lwwY9O78amqeZR+FX9J 5IKtAzpM4PbmZ87Mi7mmW6qpnnXgSMa6DwWLJUCUwWqrnq5xho3H+MlqysnxtU0kPhZ5nfEGSXd x7Q/3xsaqZvTfj1BfzcEFZqPcGhwlMJ3ROIinF2FI7qEfzVIY+Z4OzmF0p6yR5O2wBn8/HU446a SXeMSbJKav4FqaNtH0qBFP/s3Ztw9VcVT8bmiKAueKKl2RO4Vh3UQAPDhD2gAUOwMkwQHRpxrdK jGWSd5Whtgs7o+gv7M4K1mn9aIODGtio7Nme7EmHTdjN9xPW9hzX1oIS7wRMvIRdHmBSExjJRdd Plk7UWGBJnDanv6HgAJKanmrZw6ncjhbWCawnz8ZSum9JK+zfLZ9/GmzddBMfy2d7hz+QtwR9d8 g6cIYS6Baw7lCsYCoWpUSilD5ljci+hj7H2Q46jPXvKy0G1YCPrQEDccDbqMqSRkaqDHnDUlVfX n X-Received: by 2002:a05:7022:ea8d:b0:1a7:216d:157c with SMTP id a92af1059eb24-1b556902db6mr39452731c88.34.1796257495998; Wed, 02 Dec 2026 16:24:55 -0800 (PST) Received: from samniyama.tailbcd61.ts.net ([76.20.4.3]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b55684c85fsm152631634c88.14.2026.12.02.16.24.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 16:24:55 -0800 (PST) From: Koral Ilgun To: Nikolay Aleksandrov , Ido Schimmel , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Willem de Bruijn , Simon Horman , Vladimir Oltean , Ioana Ciornei , Andrew Lunn , Jiri Pirko , Daniel Machon , bridge@lists.linux.dev, netdev@vger.kernel.org, Koral Ilgun Subject: [PATCH net v2 2/2] net: bridge: switchdev: clear the hwdom of a port that stops the offload Date: Wed, 2 Dec 2026 16:24:51 -0800 Message-ID: <20261203002451.517879-3-koralilgun@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261203002451.517879-1-koralilgun@gmail.com> References: <20261203002451.517879-1-koralilgun@gmail.com> 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 Content-Transfer-Encoding: 8bit From: Koral Ilgun A switchdev driver can stop the offload of a bridge port while the port stays in the bridge. DSA does this when the last switch port leaves a LAG that stays a bridge port. nbp_switchdev_del() then keeps the hwdom of the port, and nbp_switchdev_hwdom_put() finds the port itself in the port list, so the hwdom also stays busy. If the other ports with that hwdom leave the bridge before the port, nothing releases the hwdom. dsa_port_change_conduit() leaves a port in the same state when its new offload fails. mlxsw does the same to a VXLAN bridge port when the last mlxsw port leaves the bridge. After BR_HWDOM_MAX - 1 such leaks, nbp_switchdev_hwdom_set() returns -EBUSY for a port that needs a new hwdom, so the first port of a switch cannot join the bridge. While the stale hwdom stays, nbp_switchdev_allowed_egress() also refuses the port for each frame that another port of the switch forwarded in hardware, although the switch no longer forwards to the port. Clear the hwdom when the last offload of the port stops. Make nbp_switchdev_hwdom_set() skip a port without a hwdom, so that a port does not take hwdom 0 from itself or from another port that stopped the offload. The skip also fixes a LAG that is already a bridge port when its first switch port joins: nbp_switchdev_hwdom_set() can find the LAG itself in the list and give it hwdom 0. The bridge then refuses each port without a hwdom for a frame from the LAG with offload_fwd_mark, and a later port of the same switch copies hwdom 0. With the skip, such a port can get -EBUSY when all hwdoms are busy; the previous patch keeps offload_count right for that case. During a LAG leave, the LAG now has no hwdom from dsa_port_pre_lag_leave() until dsa_port_lag_leave(), while the switch still forwards between the trunk and its other ports. A frame that the switch already forwarded can then also go out through the LAG. A frame from the trunk that the tagger marks with offload_fwd_mark, as tag_dsa does, gets no src_hwdom: the bridge refuses it toward each port without a hwdom, and the other ports of the switch get a software copy of it. dsa_port_change_conduit() opens the same window for a port between dsa_port_pre_bridge_leave() and dsa_port_bridge_leave(). The window lasts for one RTNL operation. The forwarding path reads the hwdom under RCU only, and nbp_switchdev_del() clears the hwdom before BR_TX_FWD_OFFLOAD. A reader can now see the flag with hwdom 0, and would then mark the frame for TX forwarding offload to a port that stopped the offload, or set bit 0 of fwd_hwdoms. Replace nbp_switchdev_can_offload_tx_fwd() with nbp_switchdev_tx_fwd_hwdom(), which reads the hwdom once and returns 0 for a port without one, so that each caller uses the value of one read. nbp_switchdev_frame_mark() and nbp_switchdev_allowed_egress() also read the hwdom once, with READ_ONCE(). The fault was first seen on a downstream kernel. An out-of-tree DSA feature there stops the offload of an isolated bridge port while the port stays in the bridge, and frames to that port were lost. Mainline has no such feature. An AI code review agent then read the mainline source and found that the LAG case above reaches the same state. The hwdom leak and the self match were reproduced on mainline with DSA. The egress refusals, the reader race, the mlxsw cases, the state that dsa_port_change_conduit() leaves after a failed new offload, and both directions of the LAG leave window and of the dsa_port_change_conduit() window come from a read of the source. The leak was reproduced in a VM on dsa_loop, with a local change that adds empty port_lag_join and port_lag_leave ops to dsa_loop: ip link add br0 type bridge for i in $(seq 70); do ip link add bond1 type bond mode balance-xor ip link set bond1 master br0 ip link set lan2 master br0 || break ip link set lan1 down; ip link set lan1 master bond1 ip link set lan2 nomaster; ip link set lan1 nomaster ip link del bond1 done Without the fix, the join of lan2 fails with EBUSY in round 64. With the fix, all 70 rounds pass. In the same VM, a bond that is a bridge port before lan1 joins it gets hwdom 0 without the fix, and lan2 then copies hwdom 0; with the fix, both get hwdom 1. A local trace printed the hwdom. With the fix, a ping between two veth ports of a bridge that also holds an offloaded dsa_loop port passes 20 of 20. No kernel selftest ran, and no frame with offload_fwd_mark went through the bridge. This patch was prepared with Claude Code (Anthropic). It wrote the code change, this changelog and the reproducer, and ran the tests in the VM, under my direction; a separate model instance reviewed the patch. I reviewed the result and checked the test output. Fixes: 2f5dc00f7a3e ("net: bridge: switchdev: let drivers inform which bridge ports are offloaded") Signed-off-by: Koral Ilgun --- net/bridge/br_switchdev.c | 67 +++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/net/bridge/br_switchdev.c b/net/bridge/br_switchdev.c index 8de12d56452d..52b51b8753f4 100644 --- a/net/bridge/br_switchdev.c +++ b/net/bridge/br_switchdev.c @@ -11,17 +11,33 @@ static struct static_key_false br_switchdev_tx_fwd_offload; -static bool nbp_switchdev_can_offload_tx_fwd(const struct net_bridge_port *p, - const struct sk_buff *skb) +/* + * Returns the hwdom of the egress port if the frame can use TX forwarding + * offload to it, or 0. + */ +static int nbp_switchdev_tx_fwd_hwdom(const struct net_bridge_port *p, + const struct sk_buff *skb) { + int hwdom; + if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload)) - return false; + return 0; if (br_multicast_igmp_type(skb)) - return false; + return 0; + + if (!test_bit(BR_TX_FWD_OFFLOAD_BIT, &p->flags)) + return 0; + + /* + * nbp_switchdev_del() clears the hwdom before the flag, while the + * port can stay in the bridge. + */ + hwdom = READ_ONCE(p->hwdom); + if (hwdom == BR_INPUT_SKB_CB(skb)->src_hwdom) + return 0; - return test_bit(BR_TX_FWD_OFFLOAD_BIT, &p->flags) && - (p->hwdom != BR_INPUT_SKB_CB(skb)->src_hwdom); + return hwdom; } bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb) @@ -41,7 +57,7 @@ void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb) void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p, struct sk_buff *skb) { - if (nbp_switchdev_can_offload_tx_fwd(p, skb)) + if (nbp_switchdev_tx_fwd_hwdom(p, skb)) BR_INPUT_SKB_CB(skb)->tx_fwd_offload = true; } @@ -53,24 +69,29 @@ void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p, void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p, struct sk_buff *skb) { - if (nbp_switchdev_can_offload_tx_fwd(p, skb)) - set_bit(p->hwdom, &BR_INPUT_SKB_CB(skb)->fwd_hwdoms); + int hwdom = nbp_switchdev_tx_fwd_hwdom(p, skb); + + if (hwdom) + set_bit(hwdom, &BR_INPUT_SKB_CB(skb)->fwd_hwdoms); } void nbp_switchdev_frame_mark(const struct net_bridge_port *p, struct sk_buff *skb) { - if (p->hwdom) - BR_INPUT_SKB_CB(skb)->src_hwdom = p->hwdom; + int hwdom = READ_ONCE(p->hwdom); + + if (hwdom) + BR_INPUT_SKB_CB(skb)->src_hwdom = hwdom; } bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p, const struct sk_buff *skb) { struct br_input_skb_cb *cb = BR_INPUT_SKB_CB(skb); + int hwdom = READ_ONCE(p->hwdom); - return !test_bit(p->hwdom, &cb->fwd_hwdoms) && - (!skb->offload_fwd_mark || cb->src_hwdom != p->hwdom); + return !test_bit(hwdom, &cb->fwd_hwdoms) && + (!skb->offload_fwd_mark || cb->src_hwdom != hwdom); } /* Flags that can be offloaded to hardware */ @@ -221,10 +242,14 @@ static int nbp_switchdev_hwdom_set(struct net_bridge_port *joining) struct net_bridge_port *p; int hwdom; - /* joining is yet to be added to the port list. */ + /* + * joining can already be in the port list, and so can another port + * that stopped the offload but stays a member. Both have no hwdom. + */ list_for_each_entry(p, &br->port_list, list) { - if (netdev_phys_item_id_same(&joining->ppid, &p->ppid)) { - joining->hwdom = p->hwdom; + if (p->hwdom && + netdev_phys_item_id_same(&joining->ppid, &p->ppid)) { + WRITE_ONCE(joining->hwdom, p->hwdom); return 0; } } @@ -234,7 +259,7 @@ static int nbp_switchdev_hwdom_set(struct net_bridge_port *joining) return -EBUSY; set_bit(hwdom, &br->busy_hwdoms); - joining->hwdom = hwdom; + WRITE_ONCE(joining->hwdom, hwdom); return 0; } @@ -243,9 +268,9 @@ static void nbp_switchdev_hwdom_put(struct net_bridge_port *leaving) struct net_bridge *br = leaving->br; struct net_bridge_port *p; - /* leaving is no longer in the port list. */ + /* leaving is still in the port list if it stays a member. */ list_for_each_entry(p, &br->port_list, list) { - if (p->hwdom == leaving->hwdom) + if (p != leaving && p->hwdom == leaving->hwdom) return; } @@ -305,8 +330,10 @@ static void nbp_switchdev_del(struct net_bridge_port *p) if (p->offload_count) return; - if (p->hwdom) + if (p->hwdom) { nbp_switchdev_hwdom_put(p); + WRITE_ONCE(p->hwdom, 0); + } if (test_bit(BR_TX_FWD_OFFLOAD_BIT, &p->flags)) { clear_bit(BR_TX_FWD_OFFLOAD_BIT, &p->flags); -- 2.43.0