Netdev List
 help / color / mirror / Atom feed
* [PATCH net v3 0/3] net: bridge: switchdev: fix the hwdom state of a port that stops the offload
@ 2026-12-05  2:49 Koral Ilgun
  2026-12-05  2:49 ` [PATCH net v3 1/3] rocker: fail the bridge join when the offload fails Koral Ilgun
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Koral Ilgun @ 2026-12-05  2:49 UTC (permalink / raw)
  To: Jiri Pirko, 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, 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 3 clears it,
as v1 and v2 did. Patch 2 keeps offload_count at 0 after -EBUSY, a case
that patch 3 makes reachable for a port that stays in the bridge.
Patch 1 makes rocker fail a bridge join whose offload fails, so the
count patch does not make the WARN_ON() in nbp_switchdev_del()
reachable on rocker. The other callers of
switchdev_bridge_port_offload() pass the error on (read of the
source).

For rocker, a flag in struct ofdpa_port that skips the unoffload after
a failed offload would be smaller. But the port would then stay in the
bridge without a hwdom, as on net/main. A failed join matches DSA and
mlxsw. Only a bridge master gets the error back, because
ofdpa_port_ovs_changed() sets bridge_dev before the calls that can fail
and has no clean undo.

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 e9d78fc6741a plus a local change with empty LAG ops in
dsa_loop, x86_64 in QEMU (TCG), dsa_loop with tag protocol none, and a
QEMU rocker device with one port, cabled to a virtio NIC. A local knob
made nbp_switchdev_hwdom_set() return -EBUSY as with all hwdoms busy,
and a local trace printed offload_count and the hwdom. The tests ran on
net/main, on v2, on patch 2 alone, on patch 3 alone, and after each
patch of the series; each commit message gives its results. W=1 builds
of net/bridge/ and drivers/net/ethernet/rocker/ with allmodconfig,
allyesconfig and an x86_64_defconfig-based config show no warnings on
net/main and after each patch, and neither does sparse (C=2) on the
three changed files with the x86_64_defconfig-based config.

Not tested: a frame that a switch marked with offload_fwd_mark, so also
the LAG leave window and the dsa_port_change_conduit() window; a port
with TX forwarding offload (mv88e6xxx, tag_8021q); a real -EBUSY (the
knob replaces a full busy_hwdoms); a failure of the first
ofdpa_port_vlan_del() or of ofdpa_port_vlan_add() in the rocker join;
rocker with OVS; 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.

Stable: patch 1 applies to v5.15 and later (git apply --check).
Patches 2 and 3 do not apply 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.

Claude Code (Anthropic) drafted this cover letter under my direction,
and I reviewed it.

v3:
- new patch 1: rocker fails the bridge join when the offload fails,
  undoes its join state, and ignores an STP state while the port is not
  bridged (Sashiko, Medium, on v2 1/2)
- patch 2: the rocker paragraph names patch 1
- not taken: Sashiko's Low point on v2 2/2, not to match a src_hwdom of
  0 in nbp_switchdev_allowed_egress(). adin1110, icssm, qtnfmac and
  icssg mark frames from ports without a hwdom, and the match of hwdom 0
  stops their duplicates.
- new test: the rocker join with the knob
- rebased on net/main e9d78fc6741a
v2: https://lore.kernel.org/netdev/20261203002451.517879-1-koralilgun@gmail.com/

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 (3):
  rocker: fail the bridge join when the offload fails
  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

 drivers/net/ethernet/rocker/rocker_main.c  |  8 ++-
 drivers/net/ethernet/rocker/rocker_ofdpa.c | 24 +++++++-
 net/bridge/br_switchdev.c                  | 70 +++++++++++++++-------
 3 files changed, 77 insertions(+), 25 deletions(-)


base-commit: e9d78fc6741ac06d438cf64e314c28e57014432a
-- 
2.43.0


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

* [PATCH net v3 1/3] rocker: fail the bridge join when the offload fails
  2026-12-05  2:49 [PATCH net v3 0/3] net: bridge: switchdev: fix the hwdom state of a port that stops the offload Koral Ilgun
@ 2026-12-05  2:49 ` Koral Ilgun
  2026-12-05  2:49 ` [PATCH net v3 2/3] net: bridge: switchdev: count an offload only after its hwdom is set Koral Ilgun
  2026-12-05  2:49 ` [PATCH net v3 3/3] net: bridge: switchdev: clear the hwdom of a port that stops the offload Koral Ilgun
  2 siblings, 0 replies; 4+ messages in thread
From: Koral Ilgun @ 2026-12-05  2:49 UTC (permalink / raw)
  To: Jiri Pirko, 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, Daniel Machon, bridge, netdev, Koral Ilgun

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

ofdpa_port_bridge_join() sets bridge_dev, moves the port to the internal
VLAN of the bridge, adds the untagged VLAN again and then calls
switchdev_bridge_port_offload(). When ofdpa_port_vlan_add() or the
offload fails, it returns the error and undoes nothing.
rocker_netdevice_event() only logs the error and returns NOTIFY_DONE,
so br_add_if() keeps the port in the bridge.

At the leave, ofdpa_port_bridge_leave() calls
switchdev_bridge_port_unoffload() for an offload that did not happen.
When ofdpa_port_vlan_add() failed, the bridge never counted an offload
of the port, and nbp_switchdev_del() hits WARN_ON(!p->offload_count).
When the offload failed with -EBUSY because all hwdoms are busy,
nbp_switchdev_add() keeps offload_count 1 today, so the leave is
balanced. The next patch fixes that count, and the leave then hits the
same WARN_ON().

Undo the join in ofdpa_port_bridge_join() when the VLAN add or the
offload fails: give the port its own internal VLAN and untagged VLAN
again, and clear bridge_dev. Return the error of a bridge join from
rocker_netdevice_event() with notifier_from_errno(), so that
__netdev_upper_dev_link() rolls back the link and br_add_if() fails.
That rollback sends no event for the unlink, so rocker must undo its own
state before it returns the error. A failure of the first
ofdpa_port_vlan_del() in ofdpa_port_bridge_join() now also fails the
join; that path has no undo, as before. The undo does not check the
errors of its own calls, because it already returns the error of the
join.

The undo alone does not leave a working port. br_add_if() calls
new_nbp() before it links the port, and new_nbp() sets the port state to
BLOCKING and then DISABLED with SWITCHDEV_F_DEFER. del_nbp() runs
switchdev_deferred_process() before the unlink, but the error path of
br_add_if() does not, so both states reach rocker after the undo.
ofdpa_port_attr_stp_state_set() applies them to the port that is no
longer bridged, and the standalone port loses the ACL flows that pass
ARP and link-local frames to the CPU. The deferred ageing time from
br_init_port() reaches rocker too, and it sets the ageing time of the
port, as each join does. Make ofdpa_port_attr_stp_state_set() ignore an
STP state for a port that is not bridged. DSA has the same check:
dsa_user_port_attr_set() returns -EOPNOTSUPP when
dsa_port_offloads_bridge_port() is false. rocker returns 0, and in both
cases the change is skipped without a message.

Keep the OVS case as it is. ofdpa_port_ovs_changed() sets bridge_dev
before the calls that can fail and does not undo it, so a returned
error would leave bridge_dev at a master that the port did not join.

rocker logged and dropped a join error already before the commit in
the Fixes tag. That commit put in the offload and unoffload calls, and
with them the WARN_ON() at the leave.

Tested in QEMU (TCG) with a rocker device of one port, sw1p1, cabled
through a QEMU hub to a virtio NIC in another network namespace. A
local knob made nbp_switchdev_hwdom_set() return -EBUSY as if all
hwdoms were busy:

  ip link add br1 type bridge
  ip link set sw1p1 master br1    # knob on
  ip link set sw1p1 nomaster

With the next patch but without this one, the join succeeds, rocker
logs "failed to reflect master linked (err -16)", and the leave hits
the WARN_ON() in nbp_switchdev_del(). With this patch, the join fails
with EBUSY, sw1p1 is not a port of br1, and the leave gives no WARN.
Then a ping from sw1p1 as a standalone port to the virtio NIC passes 5
of 5, and sw1p1 joins br1 with the knob off, passes a ping through br1
and leaves without a WARN. Each kernel ran the test in three boots.
Without the change to ofdpa_port_attr_stp_state_set(), the standalone
ping lost 5 of 5 in each boot, and a dump of the QEMU rocker tables
showed that the ARP and link-local ACL flows were gone.

The failures of the first ofdpa_port_vlan_del() and of
ofdpa_port_vlan_add(), and the OVS case, come from a read of the
source. No kernel selftest ran.

The Sashiko AI review of v2 of this series pointed out that the next
patch makes the WARN_ON() reachable on rocker and that no patch of the
series fixed it. Claude Code found the WARN_ON() after a failed
ofdpa_port_vlan_add() in a read of the source.

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>
---
 drivers/net/ethernet/rocker/rocker_main.c  |  8 +++++++-
 drivers/net/ethernet/rocker/rocker_ofdpa.c | 24 +++++++++++++++++++---
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/rocker/rocker_main.c b/drivers/net/ethernet/rocker/rocker_main.c
index 0fcd65fc0af7..e721b68625c6 100644
--- a/drivers/net/ethernet/rocker/rocker_main.c
+++ b/drivers/net/ethernet/rocker/rocker_main.c
@@ -3101,9 +3101,15 @@ static int rocker_netdevice_event(struct notifier_block *unused,
 			err = rocker_world_port_master_linked(rocker_port,
 							      info->upper_dev,
 							      extack);
-			if (err)
+			if (err) {
 				netdev_warn(dev, "failed to reflect master linked (err %d)\n",
 					    err);
+				/* Only the bridge join undoes its own state
+				 * on an error.
+				 */
+				if (netif_is_bridge_master(info->upper_dev))
+					return notifier_from_errno(err);
+			}
 		} else {
 			err = rocker_world_port_master_unlinked(rocker_port,
 								info->upper_dev);
diff --git a/drivers/net/ethernet/rocker/rocker_ofdpa.c b/drivers/net/ethernet/rocker/rocker_ofdpa.c
index 1e0558bd78c3..4d59ad76dd17 100644
--- a/drivers/net/ethernet/rocker/rocker_ofdpa.c
+++ b/drivers/net/ethernet/rocker/rocker_ofdpa.c
@@ -2471,6 +2471,9 @@ static int ofdpa_port_attr_stp_state_set(struct rocker_port *rocker_port,
 {
 	struct ofdpa_port *ofdpa_port = rocker_port->wpriv;
 
+	if (!ofdpa_port_is_bridged(ofdpa_port))
+		return 0;
+
 	return ofdpa_port_stp_update(ofdpa_port, 0, state);
 }
 
@@ -2583,10 +2586,25 @@ static int ofdpa_port_bridge_join(struct ofdpa_port *ofdpa_port,
 
 	err = ofdpa_port_vlan_add(ofdpa_port, OFDPA_UNTAGGED_VID, 0);
 	if (err)
-		return err;
+		goto err_vlan_add;
+
+	err = switchdev_bridge_port_offload(dev, dev, NULL, NULL, NULL,
+					    false, extack);
+	if (err)
+		goto err_bridge_port_offload;
+
+	return 0;
 
-	return switchdev_bridge_port_offload(dev, dev, NULL, NULL, NULL,
-					     false, extack);
+err_bridge_port_offload:
+	ofdpa_port_vlan_del(ofdpa_port, OFDPA_UNTAGGED_VID, 0);
+err_vlan_add:
+	ofdpa_port_internal_vlan_id_put(ofdpa_port, bridge->ifindex);
+	ofdpa_port->internal_vlan_id =
+		ofdpa_port_internal_vlan_id_get(ofdpa_port,
+						ofdpa_port->dev->ifindex);
+	ofdpa_port->bridge_dev = NULL;
+	ofdpa_port_vlan_add(ofdpa_port, OFDPA_UNTAGGED_VID, 0);
+	return err;
 }
 
 static int ofdpa_port_bridge_leave(struct ofdpa_port *ofdpa_port)
-- 
2.43.0


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

* [PATCH net v3 2/3] net: bridge: switchdev: count an offload only after its hwdom is set
  2026-12-05  2:49 [PATCH net v3 0/3] net: bridge: switchdev: fix the hwdom state of a port that stops the offload Koral Ilgun
  2026-12-05  2:49 ` [PATCH net v3 1/3] rocker: fail the bridge join when the offload fails Koral Ilgun
@ 2026-12-05  2:49 ` Koral Ilgun
  2026-12-05  2:49 ` [PATCH net v3 3/3] net: bridge: switchdev: clear the hwdom of a port that stops the offload Koral Ilgun
  2 siblings, 0 replies; 4+ messages in thread
From: Koral Ilgun @ 2026-12-05  2:49 UTC (permalink / raw)
  To: Jiri Pirko, 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, 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. Before the previous patch, rocker only logged the error and
kept its port in the bridge with the count, and its unoffload at the
leave balanced the count. With this patch alone, that unoffload hits
the WARN_ON() in nbp_switchdev_del(). The previous patch makes rocker
fail the join, so the port does not stay in the bridge.

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 and the
mlxsw 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.

In the same VM, with the knob on, a port of a QEMU rocker device joins
a bridge and then leaves it. With this patch alone, the join succeeds,
rocker logs the error, and the leave hits the WARN_ON() in
nbp_switchdev_del(). With the previous patch below this one, the join
fails with EBUSY and the leave gives no WARN.

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] 4+ messages in thread

* [PATCH net v3 3/3] net: bridge: switchdev: clear the hwdom of a port that stops the offload
  2026-12-05  2:49 [PATCH net v3 0/3] net: bridge: switchdev: fix the hwdom state of a port that stops the offload Koral Ilgun
  2026-12-05  2:49 ` [PATCH net v3 1/3] rocker: fail the bridge join when the offload fails Koral Ilgun
  2026-12-05  2:49 ` [PATCH net v3 2/3] net: bridge: switchdev: count an offload only after its hwdom is set Koral Ilgun
@ 2026-12-05  2:49 ` Koral Ilgun
  2 siblings, 0 replies; 4+ messages in thread
From: Koral Ilgun @ 2026-12-05  2:49 UTC (permalink / raw)
  To: Jiri Pirko, 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, 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.

When the last switch port leaves a LAG, 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 switch marked a frame.

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] 4+ messages in thread

end of thread, other threads:[~2026-12-05  2:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  2:49 [PATCH net v3 0/3] net: bridge: switchdev: fix the hwdom state of a port that stops the offload Koral Ilgun
2026-12-05  2:49 ` [PATCH net v3 1/3] rocker: fail the bridge join when the offload fails Koral Ilgun
2026-12-05  2:49 ` [PATCH net v3 2/3] net: bridge: switchdev: count an offload only after its hwdom is set Koral Ilgun
2026-12-05  2:49 ` [PATCH net v3 3/3] 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