Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2 0/2] net: bridge: switchdev: fix the hwdom state of a port that stops the offload
@ 2026-12-03  0:24 Koral Ilgun
  2026-12-03  0:24 ` [PATCH net v2 1/2] net: bridge: switchdev: count an offload only after its hwdom is set Koral Ilgun
  2026-12-03  0:24 ` [PATCH net v2 2/2] net: bridge: switchdev: clear the hwdom of a port that stops the offload Koral Ilgun
  0 siblings, 2 replies; 3+ messages in thread
From: Koral Ilgun @ 2026-12-03  0:24 UTC (permalink / raw)
  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, netdev,
	Koral Ilgun

From: Koral Ilgun <koral.ilgun@adtran.com>

When the last offload of a bridge port stops while the port stays in
the bridge, the bridge keeps the hwdom of the port. Patch 2 clears it,
as v1 did. The LLM review of v1 found that the skip in
nbp_switchdev_hwdom_set() lets a port that stays in the bridge get
-EBUSY, and that nbp_switchdev_add() then keeps offload_count. Patch 1
fixes the count first, so that no commit of the series has that fault.

Daniel Machon reported that the fault reproduces on lan969x and that
v1 clears it, while he prepares a LAG series for sparx5/lan969x:
https://lore.kernel.org/netdev/20261202123828.3dcba7xv7hah56yz@DEN-DL-M70577.microsemi.net/

Test: net/main c1c39137dcc3 plus a local change with empty LAG ops in
dsa_loop, x86_64 in QEMU (TCG), dsa_loop with tag protocol none. A
local trace printed offload_count and the hwdom; a local knob made
nbp_switchdev_hwdom_set() return -EBUSY as with all hwdoms busy.

- Leak, 70 rounds: base and patch 1 alone fail in round 64; patch 2
  alone and the series pass 70 of 70.
- A LAG that is a bridge port before its first switch port joins: base
  gives it hwdom 0 and a later port copies hwdom 0; with patch 2 both
  get hwdom 1.
- The same LAG with the knob on: patch 2 alone keeps offload_count 1
  after the EBUSY, accepts the retry with hwdom 0 and keeps hwdom 0
  after the knob is off. The series refuses both tries with count 0 and
  then gives hwdom 1.
- A ping between two veth ports of the bridge, with an offloaded
  dsa_loop port in it: 20 of 20 on each kernel.

W=1 builds of net/bridge/ with allmodconfig, allyesconfig and an
x86_64_defconfig-based config, and sparse (C=2), show no warnings on
base, patch 1 and patch 2. No WARNING or BUG in the logs.

Not tested: a frame with offload_fwd_mark, so also the egress refusal
and the LAG leave window; a port with TX forwarding offload (mv88e6xxx,
tag_8021q); a real -EBUSY (the knob replaces a full busy_hwdoms); an
offload from a second switch; dsa_port_change_conduit(); mlxsw and the
other drivers that call switchdev_bridge_port_unoffload(); team;
bridge VLAN filtering; the error path of br_switchdev_port_offload();
an unoffload while frames go through the bridge; the kernel selftests;
lockdep, KASAN and KCSAN.

Patch 1 changes one case on rocker. rocker_netdevice_event() only
warns when switchdev_bridge_port_offload() fails, so the port stays in
the bridge after -EBUSY, and ofdpa_port_bridge_leave() calls
switchdev_bridge_port_unoffload() without a check. Before patch 1 the
stale offload_count balanced that call. With patch 1,
nbp_switchdev_del() hits WARN_ON(!p->offload_count) and returns. It
needs all hwdoms busy, and it comes from a read of the source. Rocker
can fail the join on that error in a separate patch.

Stable: neither patch applies before v7.2. Both have context from
f76ae12b6ba3 ("net: bridge: use atomic ops to read/change p->flags
(III)"), so older trees need a backport that adapts that context.

v2:
- new patch 1: count an offload only after its hwdom is set
- patch 2: describe the receive side of the LAG leave window and the
  window in dsa_port_change_conduit(); read the hwdom once with
  READ_ONCE() in nbp_switchdev_frame_mark() and
  nbp_switchdev_allowed_egress(); name the mlxsw cases; add the tests
  of the self match and of frames through the bridge
- rebased on net/main c1c39137dcc3
v1: https://lore.kernel.org/netdev/20261124011639.990261-1-koralilgun@gmail.com/

Koral Ilgun (2):
  net: bridge: switchdev: count an offload only after its hwdom is set
  net: bridge: switchdev: clear the hwdom of a port that stops the
    offload

 net/bridge/br_switchdev.c | 70 +++++++++++++++++++++++++++------------
 1 file changed, 49 insertions(+), 21 deletions(-)


base-commit: c1c39137dcc3134010566ff29a81c93124efa539
-- 
2.43.0


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

* [PATCH net v2 1/2] net: bridge: switchdev: count an offload only after its hwdom is set
  2026-12-03  0:24 [PATCH net v2 0/2] net: bridge: switchdev: fix the hwdom state of a port that stops the offload Koral Ilgun
@ 2026-12-03  0:24 ` Koral Ilgun
  2026-12-03  0:24 ` [PATCH net v2 2/2] net: bridge: switchdev: clear the hwdom of a port that stops the offload Koral Ilgun
  1 sibling, 0 replies; 3+ messages in thread
From: Koral Ilgun @ 2026-12-03  0:24 UTC (permalink / raw)
  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, netdev,
	Koral Ilgun

From: Koral Ilgun <koral.ilgun@adtran.com>

nbp_switchdev_add() sets offload_count to 1 before it calls
nbp_switchdev_hwdom_set(), and keeps the count when that call fails
with -EBUSY. br_switchdev_port_offload() then returns the error
without nbp_switchdev_del(), so the port keeps a count for an offload
that did not happen. A later offload from the same switch takes the
offload_count branch, increments the count and returns 0, and the port
never gets a hwdom. An offload from another switch is refused.

On the current tree, only a port that br_add_if() is about to add can
get -EBUSY. br_add_if() frees that port when the driver passes the
error on. rocker only logs the error, so its port stays in the bridge
with the count, and its unoffload at the leave balances the count. With
this patch, that unoffload hits the WARN_ON() in nbp_switchdev_del().

A port that is already in the port list matches itself in
nbp_switchdev_hwdom_set(). The next patch makes that function skip a
port without a hwdom. Then a port that stays in the bridge can get
-EBUSY when all hwdoms are busy: a LAG that is already a bridge port
when its first switch port joins, or a VXLAN bridge port that
mlxsw_sp_bridge_vxlan_join() offloads.

Set offload_count only after nbp_switchdev_hwdom_set() succeeds.
nbp_switchdev_hwdom_set() compares the ppid, so the ppid is still set
before the call. The ppid stays set after the error. The next offload
overwrites it, and with the next patch nbp_switchdev_hwdom_set() skips
a port without a hwdom, so nothing reads it.

An AI coding agent found the fault in a read of the source while it
prepared the next patch. An AI code review agent then found that the
first version of that patch on the list makes the fault reachable for
a port that stays in the bridge. The refusal of a second switch, the
mlxsw case and the rocker case come from a read of the source.

Tested in a VM on dsa_loop with two local changes that do not go
upstream: empty port_lag_join and port_lag_leave ops in dsa_loop, and a
knob that makes nbp_switchdev_hwdom_set() return -EBUSY as if all
hwdoms were busy. With the knob on, lan1 joins a bond that is already a
bridge port, twice:

  ip link add br0 type bridge
  ip link add bond1 type bond mode balance-xor
  ip link set bond1 master br0
  ip link set lan1 down
  ip link set lan1 master bond1

With the next patch alone, the first join fails with EBUSY and the bond
keeps offload_count 1. The second join succeeds with hwdom 0, and the
bond keeps hwdom 0 after the knob is off. With this patch below the
next one, both joins fail with EBUSY and the count stays 0; with the
knob off, the bond gets hwdom 1. A local trace printed the count and
the hwdom. On the current tree, with or without this patch, the self
match gives the bond hwdom 0 and the knob changes nothing. No kernel
selftest ran.

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 <koral.ilgun@adtran.com>
---
 net/bridge/br_switchdev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/net/bridge/br_switchdev.c b/net/bridge/br_switchdev.c
index 10f3aea6a1f8..8de12d56452d 100644
--- a/net/bridge/br_switchdev.c
+++ b/net/bridge/br_switchdev.c
@@ -280,12 +280,13 @@ static int nbp_switchdev_add(struct net_bridge_port *p,
 	}
 
 	p->ppid = ppid;
-	p->offload_count = 1;
 
 	err = nbp_switchdev_hwdom_set(p);
 	if (err)
 		return err;
 
+	p->offload_count = 1;
+
 	if (tx_fwd_offload) {
 		set_bit(BR_TX_FWD_OFFLOAD_BIT, &p->flags);
 		static_branch_inc(&br_switchdev_tx_fwd_offload);
-- 
2.43.0


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

* [PATCH net v2 2/2] net: bridge: switchdev: clear the hwdom of a port that stops the offload
  2026-12-03  0:24 [PATCH net v2 0/2] net: bridge: switchdev: fix the hwdom state of a port that stops the offload Koral Ilgun
  2026-12-03  0:24 ` [PATCH net v2 1/2] net: bridge: switchdev: count an offload only after its hwdom is set Koral Ilgun
@ 2026-12-03  0:24 ` Koral Ilgun
  1 sibling, 0 replies; 3+ messages in thread
From: Koral Ilgun @ 2026-12-03  0:24 UTC (permalink / raw)
  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, netdev,
	Koral Ilgun

From: Koral Ilgun <koral.ilgun@adtran.com>

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 <koral.ilgun@adtran.com>
---
 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


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

end of thread, other threads:[~2026-12-03  0:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  0:24 [PATCH net v2 0/2] net: bridge: switchdev: fix the hwdom state of a port that stops the offload Koral Ilgun
2026-12-03  0:24 ` [PATCH net v2 1/2] net: bridge: switchdev: count an offload only after its hwdom is set Koral Ilgun
2026-12-03  0:24 ` [PATCH net v2 2/2] net: bridge: switchdev: clear the hwdom of a port that stops the offload Koral Ilgun

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