* [PATCH net] net: bridge: switchdev: clear the hwdom of a port that stops the offload
@ 2026-11-24 1:16 Koral Ilgun
2026-11-25 1:38 ` netdev-bot+sashiko
0 siblings, 1 reply; 2+ messages in thread
From: Koral Ilgun @ 2026-11-24 1:16 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, bridge, netdev
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.
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.
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 to the leaving port. A frame that the switch already
forwarded can then also go out through the LAG. 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.
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.
Only the hwdom leak was reproduced on mainline. The egress refusal, the
self match in nbp_switchdev_hwdom_set() and the reader race 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. No kernel selftest ran, and no frame
went through the bridge in the test.
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>
---
Test: net/main 5677cf0e039b plus the dsa_loop change named above,
x86_64 in QEMU (TCG), dsa_loop with tag protocol none. Without the fix
the join fails in round 64; with the fix all 70 rounds pass, with no
WARNING or BUG in the log. W=1 builds of net/bridge/ with
allmodconfig, allyesconfig and an x86_64_defconfig-based config, and
sparse (C=2), show no new warnings. net/bridge and net/dsa did not
change between the tested base and 639a75a3539f.
Not tested: a frame through the bridge, so also the egress refusal,
the TX forwarding offload path (mv88e6xxx, tag_8021q) and the LAG
leave window; dsa_port_change_conduit(); other drivers that call
switchdev_bridge_port_unoffload() (mlxsw, ocelot, lan966x, prestera,
dpaa2-switch, sparx5, rocker, am65-cpsw-nuss, cpsw_new); team; the
kernel selftests; lockdep, KASAN and KCSAN.
nbp_switchdev_frame_mark() and nbp_switchdev_allowed_egress() keep
plain reads of p->hwdom: each mix of the old and the new hwdom gives
the result of one of the two states.
Stable: the patch does not apply before v7.2, because of f76ae12b6ba3
("net: bridge: use atomic ops to read/change p->flags (III)"). Older
trees need a backport of the first hunk.
net/bridge/br_switchdev.c | 56 ++++++++++++++++++++++++++++-----------
1 file changed, 40 insertions(+), 16 deletions(-)
diff --git a/net/bridge/br_switchdev.c b/net/bridge/br_switchdev.c
index 10f3aea6a1f8..7b368fa82f76 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,8 +69,10 @@ 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,
@@ -221,10 +239,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 +256,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 +265,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;
}
@@ -304,8 +326,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);
base-commit: 639a75a3539f621e4437ffeec7ab51bf8a1fb170
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH net] net: bridge: switchdev: clear the hwdom of a port that stops the offload
2026-11-24 1:16 [PATCH net] net: bridge: switchdev: clear the hwdom of a port that stops the offload Koral Ilgun
@ 2026-11-25 1:38 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-25 1:38 UTC (permalink / raw)
To: koralilgun
Cc: razor, idosch, davem, edumazet, kuba, pabeni, willemb, horms,
vladimir.oltean, ioana.ciornei, andrew, bridge, netdev
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · Low: 1
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).
- [Medium] In net/bridge/br_switchdev.c, nbp_switchdev_add() sets
`p->ppid = ppid; p->offload_count = 1;` (lines 304-305) before it…
- [Medium] The new `WRITE_ONCE(p->hwdom, 0)` in nbp_switchdev_del()
(net/bridge/br_switchdev.c:329-332) runs from dsa_port_pre_lag_leave()…
- [Medium] net: bridge: switchdev: unreset offload_count on hwdom_set()
error
- [Low] The patch adds a lockless writer of p->hwdom:
`WRITE_ONCE(p->hwdom, 0)` in nbp_switchdev_del()…
--
net: bridge: switchdev: clear the hwdom of a port that stops the offload
When the last offload of a bridge port stops while the port stays in the
bridge, this patch clears p->hwdom in nbp_switchdev_del() so the hwdom is
no longer leaked. It also makes nbp_switchdev_hwdom_set() skip ports that
have no hwdom. It replaces nbp_switchdev_can_offload_tx_fwd() with
nbp_switchdev_tx_fwd_hwdom(), which reads the hwdom once with READ_ONCE().
> diff --git a/net/bridge/br_switchdev.c b/net/bridge/br_switchdev.c
> index 10f3aea6a1f82..7b368fa82f762 100644
> --- a/net/bridge/br_switchdev.c
> +++ b/net/bridge/br_switchdev.c
[ ... ]
> @@ -53,8 +69,10 @@ 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,
[Severity: Low]
This isn't a functional bug, but nbp_switchdev_del() now does a lockless
WRITE_ONCE(p->hwdom, 0) on a port that RCU readers can still reach. Should
the other readers of p->hwdom get an annotation too?
nbp_switchdev_frame_mark() and nbp_switchdev_allowed_egress() each read
the field twice with plain loads:
if (p->hwdom)
BR_INPUT_SKB_CB(skb)->src_hwdom = p->hwdom;
return !test_bit(p->hwdom, &cb->fwd_hwdoms) &&
(!skb->offload_fwd_mark || cb->src_hwdom != p->hwdom);
The notes below the changelog say that any mix of the old and new values
gives the result of one of the two states. That seems to hold for the
current expressions.
KCSAN will still report these loads as data races against the new writer.
That can happen when nbp_switchdev_del() runs under RTNL during a LAG
leave or a conduit change, while br_handle_frame() or should_deliver()
runs in softirq on another CPU. The plain loads are also inconsistent with
the READ_ONCE() in nbp_switchdev_tx_fwd_hwdom().
Would it be better to read p->hwdom once into a local with READ_ONCE() in
these helpers? If the race is intended, data_race() would also work.
[ ... ]
> @@ -221,10 +239,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;
> }
> }
[Severity: Medium]
Can this skip make nbp_switchdev_hwdom_set() fail for a port that stays
in the bridge? Would that leave the port with a stale offload_count?
Before this patch, a port already in br->port_list always matched
itself in this loop, because nbp_switchdev_add() has just copied the
ppid into joining->ppid. The -EBUSY return could only be reached from
br_add_if(), where a failure destroys the new port anyway.
With the new p->hwdom check, a port that is already a member and has no
hwdom falls through to find_next_zero_bit(). It gets -EBUSY once all
BR_HWDOM_MAX - 1 hwdoms are busy. At that point nbp_switchdev_add() has
already set the count and the ppid, and it does not undo them:
p->ppid = ppid;
p->offload_count = 1;
err = nbp_switchdev_hwdom_set(p);
if (err)
return err;
br_switchdev_port_offload() then returns the error without calling
nbp_switchdev_del(). Several paths offload a port that is already in
the bridge and can hit this:
- a LAG whose first switch port joins it, via dsa_port_lag_join() or
lan966x_lag_port_join()
- a VXLAN bridge port in mlxsw_sp_bridge_vxlan_join()
- the rejoin in dsa_port_change_conduit(), where the port now has no
hwdom because nbp_switchdev_del() cleared it
The port is then left with offload_count == 1, the switch's ppid and
hwdom == 0.
A later offload from the same switch, including a retry of the same
command, takes the if (p->offload_count) branch in nbp_switchdev_add().
That branch increments the count and returns 0. The port is then
offloaded but never gets a hwdom, and BR_TX_FWD_OFFLOAD is never set.
This is the hwdom 0 state that the commit message says the skip fixes.
A frame from such a LAG with offload_fwd_mark would again be refused
toward every port without a hwdom.
An offload from a different switch is refused with "Same bridge port
cannot be offloaded by two physical switches", because offload_count
is non-zero.
The count also stays one above the real number of offloads. So
nbp_switchdev_del() never reaches its cleanup until the port leaves
the bridge.
Should nbp_switchdev_add() reset offload_count, and probably ppid, when
nbp_switchdev_hwdom_set() fails?
[Severity: Medium]
If the hwdom allocation fails, can this skip leave a LAG bridge port with a
stale offload_count?
Before this patch, a LAG that is already a bridge port always matched
itself here, because nbp_switchdev_add() has just set joining->ppid. So
nbp_switchdev_hwdom_set() could not return -EBUSY for it.
With the new p->hwdom check, the LAG falls through to
find_next_zero_bit(). That returns -EBUSY once all BR_HWDOM_MAX - 1
hwdoms are busy.
nbp_switchdev_add() sets the count and ppid before this call, and does not
undo them on failure:
net/bridge/br_switchdev.c:nbp_switchdev_add() {
...
p->ppid = ppid;
p->offload_count = 1;
err = nbp_switchdev_hwdom_set(p);
if (err)
return err;
...
}
br_switchdev_port_offload() returns that error without calling
nbp_switchdev_del(). dsa_port_bridge_join() then jumps to
out_rollback_unbridge, which comes after the unoffload rollback.
The LAG then stays in the bridge with offload_count == 1, the switch's
ppid and hwdom == 0. A later join of a port from the same switch,
including a retry of the same command, takes this branch in
nbp_switchdev_add():
if (p->offload_count) {
...
p->offload_count++;
return 0;
}
After that, no hwdom is ever assigned. The commit message says the skip
fixes a LAG with hwdom 0, where nbp_switchdev_allowed_egress() refuses
frames from the LAG with offload_fwd_mark toward every port without a
hwdom. Doesn't this path bring that state back?
offload_count also stays one above the real number of offloads, so
nbp_switchdev_del() never reaches its cleanup. Until the LAG leaves the
bridge, ports of any other switch are refused with "Same bridge port
cannot be offloaded by two physical switches".
Would it help to reset offload_count and ppid in nbp_switchdev_add() when
nbp_switchdev_hwdom_set() fails?
[ ... ]
> @@ -304,8 +326,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);
> + }
[Severity: Medium]
The commit message describes the transmit-side effect of the LAG leave
window. Does clearing the hwdom here also affect the receive side during
that window?
This runs from:
dsa_port_pre_lag_leave()
dsa_port_pre_bridge_leave()
switchdev_bridge_port_unoffload()
...
nbp_switchdev_del()
The switch leaves the hardware bridge only later, in dsa_port_lag_leave()
-> dsa_port_bridge_leave(). The hardware trunk and the dsa_lag mapping are
removed after that. In between, the hardware still forwards between the
trunk and the other ports of the same hwdom.
With tag_dsa/edsa (mv88e6xxx), dsa_rcv_ll() injects trunk frames directly
onto the LAG device and always marks them:
net/dsa/tag_dsa.c:dsa_rcv_ll() {
...
lag = dsa_lag_by_id(cpu_dp->dst, source_port + 1);
skb->dev = lag ? lag->dev : NULL;
...
if (trunk)
skb->offload_fwd_mark = true;
...
}
These frames reach br_handle_frame() on the LAG's bridge port. Because
p->hwdom is now 0, nbp_switchdev_frame_mark() leaves src_hwdom at 0. In
nbp_switchdev_allowed_egress():
return !test_bit(p->hwdom, &cb->fwd_hwdoms) &&
(!skb->offload_fwd_mark || cb->src_hwdom != p->hwdom);
the second condition is false for every software port with hwdom 0, so
these frames are dropped toward the non-switchdev ports. The other ports
of the switch pass the check and get a software copy of frames that the
hardware already flooded to them.
The commit message says the patch fixes a bridge that "refuses each port
without a hwdom for a frame from the LAG with offload_fwd_mark". Doesn't
the same thing now happen for the length of this window?
dsa_port_change_conduit() opens a similar window for a port that is not
in a LAG:
if (bridge_dev) {
dsa_port_pre_bridge_leave(dp, bridge_dev);
dsa_port_bridge_leave(dp, bridge_dev);
}
dsa_default_offload_fwd_mark() keeps setting offload_fwd_mark from
dp->bridge until dsa_port_bridge_leave() clears it. Frames received on
that port in between also get src_hwdom 0. Before this patch the port
kept its hwdom here, which matched the hardware.
Should the commit message also describe these cases?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124011639.990261-1-koralilgun%40gmail.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 1:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 1:16 [PATCH net] net: bridge: switchdev: clear the hwdom of a port that stops the offload Koral Ilgun
2026-11-25 1:38 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox