Re: [PATCH net 6/6] net: dsa: keep a bridge number while another tree uses it
From: willemb
Date: Sat Dec 05 2026 - 22:57:09 EST
Julius Bairaktaris wrote:
Bridge numbers are unique across all DSA trees, and a bridge spanning
ports of two trees gets one number, which both trees use.
dsa_port_bridge_destroy() puts the number when the last port of its
own tree leaves the bridge, and dsa_bridge_num_put() clears the bit
unconditionally, so the number is free while the other tree still
offloads the bridge under it. The next bridge that gets a number then
gets the same one: on a tag_8021q switch both bridges share a VLAN, on
mv88e6xxx a cross-chip PVT row, and frames of one reach the ports of
the other.
Clear the bit only when no tree offloads the bridge any more.
Found by an LLM review (Claude), and reproduced in QEMU with dsa_loop.
Fixes: f5e165e72b29 ("net: dsa: track unique bridge numbers across all DSA switch trees")
Signed-off-by: Julius Bairaktaris <julius@xxxxxxxxxxxxxx>
net/dsa/dsa.c:130-143, 184 (dsa_bridge_num_find / dsa_bridge_num_put):
The new guard `if (dsa_bridge_num_find(bridge_dev) == bridge_num) return;`
fails to enforce the commit message's invariant ("clear the bit only when no
tree offloads the bridge any more") because dsa_bridge_num_find() returns
immediately on the first tree in dsa_tree_list with a struct dsa_bridge for
bridge_dev, even when that tree has bridge->num == 0 (any switch driver with
ds->max_num_bridges == 0, which is most DSA drivers).
Concrete failure with 3 trees in dsa_tree_list = [Tree 0 (max=0), Tree 1
(max=7), Tree 2 (max=7)]:
1. Tree 1 joins br0 -> bridge->num = 1 (bit 1 set).
2. Tree 2 joins br0 -> dsa_bridge_num_find(br0) finds Tree 1 -> bridge->num = 1.
3. Tree 0 joins br0 -> max == 0 -> bridge->num = 0.
4. Tree 1's last port leaves br0 -> dsa_port_bridge_destroy() calls
dsa_bridge_num_put(br0, 1) -> dsa_bridge_num_find(br0) hits Tree 0 first
and returns 0 without inspecting Tree 2 -> 0 == 1 is false -> clear_bit(1,
&dsa_fwd_offloading_bridges) releases bit 1 while Tree 2 still offloads br0
under number 1!
5. Next bridge br1 allocates bit 1, colliding with br0 on Tree 2 (shared
tag_8021q VLAN / mv88e6xxx PVT row / mxl862xx bridge slot).
Root cause: commit d3eed0e57d5d accidentally dropped `&& dp->bridge_num` when
rewriting dsa_bridge_num_find() to call dsa_tree_bridge_find(). Changing line
138 of net/dsa/dsa.c from `if (bridge)` to `if (bridge && bridge->num)` fixes
both dsa_bridge_num_put() and dsa_bridge_num_get().
net/dsa/dsa.c:177 (Fixes trailer):
Patch 6/6 cites `Fixes: f5e165e72b29 ("net: dsa: track unique bridge numbers
across all DSA switch trees")`, but f5e165e72b29 (and 3f9bb0301d50) guarded
clear_bit() with dsa_bridge_num_find(). It was commit d3eed0e57d5d ("net: dsa:
keep the bridge_dev and bridge_num as part of the same structure") that removed
the dsa_bridge_num_find() check in dsa_bridge_num_put() (and dropped
`&& bridge->num` in dsa_bridge_num_find()). Update to `Fixes: d3eed0e57d5d`.
net/dsa/dsa.c:184 (dsa_bridge_num_put -> dsa_bridge_num_find):
dsa_tree_list and dst->ports are mutated under dsa2_mutex without rtnl_lock()
(dsa_register_switch()/dsa_unregister_switch()), whereas dsa_bridge_num_get()
and now dsa_bridge_num_put() walk dsa_tree_list under rtnl_lock() without
dsa2_mutex (and taking dsa2_mutex under rtnl_lock() would invert lock order
versus dsa_switch_shutdown()). Pre-existing race, not introduced here.
pw-bot: cr