Bridge numbers are unique across all DSA trees, and a bridge with ports in several trees is meant to have one number, which the trees share: a tag_8021q switch derives the VLAN of a VLAN-unaware bridge from it. dsa_bridge_num_find() returns the number of the first tree that has a port in the bridge, and that can be 0: a tree whose switch does not number bridges (max_num_bridges 0, as an mv88e6xxx without a PVT) keeps the bridge at number 0. When such a tree comes first, dsa_bridge_num_get() for a switch of a later tree finds 0 and takes a new number for a bridge that another tree already offloads under a different one, and the two trees forward the bridge in different VLANs. The lookup skipped ports without a bridge number before d3eed0e57d5d; skip trees that offload the bridge without one again. Fixes: d3eed0e57d5d ("net: dsa: keep the bridge_dev and bridge_num as part of the same structure") Signed-off-by: Julius Bairaktaris --- v2: new. Sashiko and Willem de Bruijn found on v1 4/6 and 6/6 that the lookup can return 0 from a tree that offloads the bridge without a number; 4/7 relies on this patch. net/dsa/dsa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c index 2771c34e8400..3c11d22ca776 100644 --- a/net/dsa/dsa.c +++ b/net/dsa/dsa.c @@ -135,7 +135,7 @@ static int dsa_bridge_num_find(const struct net_device *bridge_dev) struct dsa_bridge *bridge; bridge = dsa_tree_bridge_find(dst, bridge_dev); - if (bridge) + if (bridge && bridge->num) return bridge->num; } -- 2.53.0 Bridge numbers are unique across all DSA trees, and each switch tells dsa_bridge_num_get() the largest number it can use (max_num_bridges). The limit is checked only when the bridge gets its number. A switch that joins a bridge which another switch, with a larger limit, has numbered already gets that number unchecked. With an mv88e6xxx tree (limit up to 31) or an mxl862xx tree (48) beside a sja1105 or vsc73xx tree (DSA_TAG_8021Q_MAX_NUM_BRIDGES, 7) and eight or more offloaded bridges, the tag_8021q switch encodes number 9 in the three VBID bits of its bridge VLAN (dsa_tag_8021q_bridge_vid()), so bridge 9 gets the VLAN of bridge 1 and the two share a broadcast domain in the switch. An mxl862xx tree beside an mv88e6xxx one with 32 or more bridges overflows the device field of the DSA header in the same way. Return 0 for a number beyond the limit in that case too. Refuse the same in dsa_port_bridge_create() when the bridge is found in the tree, where another switch of the tree may have numbered it beyond this switch's limit, or not at all: an mv88e6xxx chip without a PVT has max_num_bridges 0 and leaves the bridge at number 0, which a chip with a PVT would take. The port is then bridged in software, with its own extack message for the unnumbered case. Which ports are offloaded then depends on the order in which they join. The bridge is not renumbered for the later port: the ports that joined earlier were set up with number 0, and a new number would have to reach all of them. Fixes: f5e165e72b29 ("net: dsa: track unique bridge numbers across all DSA switch trees") Signed-off-by: Julius Bairaktaris --- v2 (v1 4/6): own extack message for a bridge that another switch of the tree offloads without a number, and a sentence on join order (Sashiko); applies to net without v1 1/6. net/dsa/dsa.c | 5 +++++ net/dsa/port.c | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c index 3c11d22ca776..9c2cb9ed2966 100644 --- a/net/dsa/dsa.c +++ b/net/dsa/dsa.c @@ -163,6 +163,11 @@ unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max) return 0; set_bit(bridge_num, &dsa_fwd_offloading_bridges); + } else if (bridge_num > max) { + /* A switch with a larger limit, maybe in another tree, numbered + * this bridge already, beyond what this switch can encode + */ + return 0; } return bridge_num; diff --git a/net/dsa/port.c b/net/dsa/port.c index 1f5536c0dffc..f71dfd78bf7c 100644 --- a/net/dsa/port.c +++ b/net/dsa/port.c @@ -426,6 +426,21 @@ static int dsa_port_bridge_create(struct dsa_port *dp, bridge = dsa_tree_bridge_find(ds->dst, br); if (bridge) { + /* Another switch of the tree offloads the bridge without a + * number, or under one beyond this switch's limit + */ + if (ds->max_num_bridges && !bridge->num) { + NL_SET_ERR_MSG_MOD(extack, + "Bridge offloaded without a number by another switch of the tree"); + return -EOPNOTSUPP; + } + + if (ds->max_num_bridges && bridge->num > ds->max_num_bridges) { + NL_SET_ERR_MSG_MOD(extack, + "Range of offloadable bridges exceeded"); + return -EOPNOTSUPP; + } + refcount_inc(&bridge->refcount); dp->bridge = bridge; return 0; -- 2.53.0 The bridge numbers in use are bits of dsa_fwd_offloading_bridges, one unsigned long, and dsa_bridge_num_get() searches it with find_next_zero_bit() over DSA_MAX_NUM_OFFLOADING_BRIDGES, which is BITS_PER_LONG. When every bit is taken, find_next_zero_bit() returns the size of the bitmap, and only "bridge_num > max" stands between that and set_bit(). mxl862xx sets max_num_bridges to 48 (MXL862XX_MAX_BRIDGES), so on a 32-bit kernel the 32nd offloaded bridge gets number 32 and set_bit() writes past dsa_fwd_offloading_bridges. Refuse a number at or past the size of the bitmap; the port is then bridged in software, as one beyond the switch's limit is. Fixes: f5e165e72b29 ("net: dsa: track unique bridge numbers across all DSA switch trees") Signed-off-by: Julius Bairaktaris --- v2 (v1 5/6): unchanged. net/dsa/dsa.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c index 9c2cb9ed2966..4e697b8926a7 100644 --- a/net/dsa/dsa.c +++ b/net/dsa/dsa.c @@ -159,7 +159,8 @@ unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max) bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges, DSA_MAX_NUM_OFFLOADING_BRIDGES, 1); - if (bridge_num > max) + if (bridge_num >= DSA_MAX_NUM_OFFLOADING_BRIDGES || + bridge_num > max) return 0; set_bit(bridge_num, &dsa_fwd_offloading_bridges); -- 2.53.0 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 under that number any more, as the put did before d3eed0e57d5d. Fixes: d3eed0e57d5d ("net: dsa: keep the bridge_dev and bridge_num as part of the same structure") Signed-off-by: Julius Bairaktaris --- v2 (v1 6/6): Fixes d3eed0e57d5d instead of f5e165e72b29 (Sashiko, Willem de Bruijn); correct with 1/7, which keeps the lookup from returning 0 from an unnumbered tree. net/dsa/dsa.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c index 4e697b8926a7..c11d983d9721 100644 --- a/net/dsa/dsa.c +++ b/net/dsa/dsa.c @@ -177,10 +177,13 @@ unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max) void dsa_bridge_num_put(const struct net_device *bridge_dev, unsigned int bridge_num) { - /* Since we refcount bridges, we know that when we call this function - * it is no longer in use, so we can just go ahead and remove it from - * the bit mask. + /* The bridge is no longer offloaded by the tree that calls this, but + * the number is global: another tree may still offload the bridge + * under it. */ + if (dsa_bridge_num_find(bridge_dev) == bridge_num) + return; + clear_bit(bridge_num, &dsa_fwd_offloading_bridges); } -- 2.53.0 struct dsa_bridge is shared by the ports of all switches of a tree that are in the bridge, and dsa_port_bridge_join() overwrites its tx_fwd_offload with the answer of the switch whose port joins. mv88e6xxx offloads TX forwarding only on a chip with a PVT, where it maps the bridge as a virtual device, and on leave remaps that device only if bridge.tx_fwd_offload is set. In a tree with one chip that has a PVT and one that does not, a port of the second joining the bridge after a port of the first clears the flag; when the port of the first chip then leaves, mv88e6xxx_port_bridge_leave() skips the remap, and the PVT row of the bridge's virtual device still lets FORWARD frames of the bridge out of the port that left. Only ever set the shared flag on join, and offload TX forwarding to the bridge port by the port's own answer, as before 857fdd74fb38. On a chip without a PVT the remap on leave does nothing (mv88e6xxx_pvt_map() returns early). Fixes: 857fdd74fb38 ("net: dsa: eliminate dsa_switch_ops :: port_bridge_tx_fwd_{,un}offload") Signed-off-by: Julius Bairaktaris --- v2: new, from the Sashiko review of v1 4/6. net/dsa/port.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/net/dsa/port.c b/net/dsa/port.c index f71dfd78bf7c..7ebdc9ce6b92 100644 --- a/net/dsa/port.c +++ b/net/dsa/port.c @@ -521,13 +521,18 @@ int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br, if (err) goto out_rollback; - /* Drivers which support bridge TX forwarding should set this */ - dp->bridge->tx_fwd_offload = info.tx_fwd_offload; + /* Drivers which support bridge TX forwarding should set this. The + * bridge is shared by the ports of all switches of the tree: one + * switch whose port cannot offload TX forwarding must not clear what + * another switch's port set up and has to undo when it leaves. + */ + if (info.tx_fwd_offload) + dp->bridge->tx_fwd_offload = true; err = switchdev_bridge_port_offload(brport_dev, dev, dp, &dsa_user_switchdev_notifier, &dsa_user_switchdev_blocking_notifier, - dp->bridge->tx_fwd_offload, extack); + info.tx_fwd_offload, extack); if (err) goto out_rollback_unbridge; -- 2.53.0 When switchdev_bridge_port_offload() or dsa_port_switchdev_sync_attrs() fails after the BRIDGE_JOIN broadcast, dsa_port_bridge_join() sends BRIDGE_LEAVE while dp->bridge is still set, and clears it afterwards. dsa_port_bridge_leave() does it the other way round, and drivers that recompute membership from dp->bridge in their leave callback rely on that: mv88e6xxx_port_bridge_leave() rebuilds the port VLAN maps through mv88e6xxx_port_vlan(), which still counts the failed port as a member of the bridge, so the port and the other members of the bridge keep forwarding to each other in the switch although the port is now standalone. The rollback also sends the copy of the bridge taken before the join, without the tx_fwd_offload the joining switch set, so mv88e6xxx skips the remap of the bridge's virtual device in the PVT. And unlike a leave, the rollback does not undo what dsa_port_switchdev_sync_attrs() set before it failed, so the now standalone port keeps the bridge port flags (learning), STP state and VLAN filtering of the bridge. Undo the join as a leave does: take the bridge as it is now, clear dp->bridge, send BRIDGE_LEAVE, then reset the port attributes with dsa_port_switchdev_unsync_attrs(). When switchdev_bridge_port_offload() failed and nothing was synced, the reset leaves the port as dsa_port_bridge_leave() leaves any port: standalone flooding without learning, the forwarding state, and VLAN filtering changed only if the bridge's setting differs from what the switch needs standalone. Fixes: 5961d6a12c13 ("net: dsa: inherit the actual bridge port flags at join time") Signed-off-by: Julius Bairaktaris --- v2: new, found while reviewing 5/7; also resets the port attributes that dsa_port_switchdev_sync_attrs() set before it failed. net/dsa/port.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/net/dsa/port.c b/net/dsa/port.c index 7ebdc9ce6b92..f07064384f34 100644 --- a/net/dsa/port.c +++ b/net/dsa/port.c @@ -548,7 +548,14 @@ int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br, &dsa_user_switchdev_blocking_notifier); dsa_flush_workqueue(); out_rollback_unbridge: + /* Undo the join as dsa_port_bridge_leave() does: drivers see the port + * out of the bridge, and the bridge as it is now + */ + info.bridge = *dp->bridge; + dsa_port_bridge_destroy(dp, br); dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); + dsa_port_switchdev_unsync_attrs(dp, info.bridge); + return err; out_rollback: dsa_port_bridge_destroy(dp, br); return err; -- 2.53.0 dsa_broadcast(), dsa_bridge_num_find() and dsa_bridge_mtu_normalization() walk dsa_tree_list and the port lists of all trees under rtnl_lock() only, on bridge join and leave and on an MTU change of a bridged port, and dsa_broadcast() calls into the notifier chain of every tree. Trees, ports and switch notifiers are added and removed under dsa2_mutex only: when the switch of one tree is unbound, dsa_switch_teardown() takes its notifier off the chain, dsa_switch_release_ports() frees its ports and dsa_tree_free() its tree, while a port of another tree joining or leaving a bridge walks them. A broadcast that reached the switch's notifier just before it was taken off can then still run in dsa_switch_event() when the driver frees the switch. Add and remove trees, ports and switch notifiers with rtnl_lock() held as well, inside dsa2_mutex, which dsa_tree_setup_conduit(), dsa_tree_teardown_conduit() and dsa_switch_shutdown() already take before rtnl_lock(). No path takes dsa2_mutex with rtnl_lock() held. Fixes: f66a6a69f97a ("net: dsa: permit cross-chip bridging between all trees in the system") Signed-off-by: Julius Bairaktaris --- v2: new. Sashiko and Willem de Bruijn pointed out on v1 6/6 that the lists are walked under rtnl_lock() and changed under dsa2_mutex only; the switch notifiers that dsa_broadcast() calls are covered as well. net/dsa/dsa.c | 13 +++++++++++++ net/dsa/switch.c | 11 ++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c index c11d983d9721..330799667f15 100644 --- a/net/dsa/dsa.c +++ b/net/dsa/dsa.c @@ -33,6 +33,11 @@ #define DSA_MAX_NUM_OFFLOADING_BRIDGES BITS_PER_LONG static DEFINE_MUTEX(dsa2_mutex); + +/* Trees are added to and removed from dsa_tree_list, ports to and from + * dst->ports and switch notifiers to and from dst->nh, with both dsa2_mutex + * and rtnl_lock() held, so either lock is enough to walk them. + */ LIST_HEAD(dsa_tree_list); static struct workqueue_struct *dsa_owq; @@ -234,7 +239,9 @@ static struct dsa_switch_tree *dsa_tree_alloc(int index) INIT_LIST_HEAD(&dst->ports); INIT_LIST_HEAD(&dst->list); + rtnl_lock(); list_add_tail(&dst->list, &dsa_tree_list); + rtnl_unlock(); kref_init(&dst->refcount); @@ -245,7 +252,9 @@ static void dsa_tree_free(struct dsa_switch_tree *dst) { if (dst->tag_ops) dsa_tag_driver_put(dst->tag_ops); + rtnl_lock(); list_del(&dst->list); + rtnl_unlock(); kfree(dst); } @@ -1115,7 +1124,9 @@ static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index) INIT_LIST_HEAD(&dp->mdbs); INIT_LIST_HEAD(&dp->vlans); /* also initializes &dp->user_vlans */ INIT_LIST_HEAD(&dp->list); + rtnl_lock(); list_add_tail(&dp->list, &dst->ports); + rtnl_unlock(); return dp; } @@ -1535,7 +1546,9 @@ static void dsa_switch_release_ports(struct dsa_switch *ds) kfree(v); } + rtnl_lock(); list_del(&dp->list); + rtnl_unlock(); kfree(dp); } } diff --git a/net/dsa/switch.c b/net/dsa/switch.c index a670a04edb72..d1230354dd08 100644 --- a/net/dsa/switch.c +++ b/net/dsa/switch.c @@ -1119,16 +1119,25 @@ int dsa_broadcast(unsigned long e, void *v) int dsa_switch_register_notifier(struct dsa_switch *ds) { + int err; + ds->nb.notifier_call = dsa_switch_event; - return raw_notifier_chain_register(&ds->dst->nh, &ds->nb); + /* dsa_broadcast() walks the chains of all trees under rtnl_lock() */ + rtnl_lock(); + err = raw_notifier_chain_register(&ds->dst->nh, &ds->nb); + rtnl_unlock(); + + return err; } void dsa_switch_unregister_notifier(struct dsa_switch *ds) { int err; + rtnl_lock(); err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb); + rtnl_unlock(); if (err) dev_err(ds->dev, "failed to unregister notifier (%d)\n", err); } -- 2.53.0