A tagger that offloads TX forwarding reads dp->bridge from its xmit function, in an RCU read-side critical section with BH disabled, to find the bridge number of a frame that the bridge marked with offload_fwd_mark; the receive path reads it as well (dsa_tag_8021q_find_port_by_vbid()). dsa_port_bridge_destroy() clears dp->bridge under rtnl_lock and frees the structure with kfree() once the last port has left. A frame that was marked before the leave can still be on its way through the tagger at that point, and the tagger then dereferences freed memory. dsa_port_bridge_dev_get() and dsa_port_bridge_num_get() read the pointer twice, so a concurrent leave also turns their NULL test into a NULL dereference. Free the structure with kfree_rcu(), publish dp->bridge with smp_store_release() and clear it with WRITE_ONCE(), and read it with READ_ONCE() in the datapath: in both helpers, once each, and in dsa_find_designated_bridge_port_by_vid(), dsa_default_offload_fwd_mark() and dsa_tag_8021q_find_port_by_vbid(), which uses the bridge it read for the number as well. Found by an LLM review (Claude). Fixes: d3eed0e57d5d ("net: dsa: keep the bridge_dev and bridge_num as part of the same structure") Signed-off-by: Julius Bairaktaris --- include/net/dsa.h | 14 ++++++++++++-- net/dsa/port.c | 11 +++++++---- net/dsa/tag.h | 4 ++-- net/dsa/tag_8021q.c | 6 ++++-- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/include/net/dsa.h b/include/net/dsa.h index 5d12191b6f6..1076439e838 100644 --- a/include/net/dsa.h +++ b/include/net/dsa.h @@ -242,6 +242,7 @@ struct dsa_bridge { unsigned int num; bool tx_fwd_offload; refcount_t refcount; + struct rcu_head rcu; }; struct dsa_port { @@ -302,6 +303,11 @@ struct dsa_port { struct device_node *dn; unsigned int ageing_time; + /* Written under rtnl_lock. The datapath reads it once, with + * READ_ONCE(), in an RCU read-side critical section: the port can + * leave its bridge while a frame is on its way through the tagger, + * and the structure is freed only after a grace period. + */ struct dsa_bridge *bridge; struct devlink_port devlink_port; struct phylink *pl; @@ -772,12 +778,16 @@ struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp) static inline struct net_device * dsa_port_bridge_dev_get(const struct dsa_port *dp) { - return dp->bridge ? dp->bridge->dev : NULL; + const struct dsa_bridge *bridge = READ_ONCE(dp->bridge); + + return bridge ? bridge->dev : NULL; } static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp) { - return dp->bridge ? dp->bridge->num : 0; + const struct dsa_bridge *bridge = READ_ONCE(dp->bridge); + + return bridge ? bridge->num : 0; } static inline bool dsa_port_bridge_same(const struct dsa_port *a, diff --git a/net/dsa/port.c b/net/dsa/port.c index 1f5536c0dff..997e68cd215 100644 --- a/net/dsa/port.c +++ b/net/dsa/port.c @@ -427,7 +427,8 @@ static int dsa_port_bridge_create(struct dsa_port *dp, bridge = dsa_tree_bridge_find(ds->dst, br); if (bridge) { refcount_inc(&bridge->refcount); - dp->bridge = bridge; + /* Pairs with the READ_ONCE() of the datapath */ + smp_store_release(&dp->bridge, bridge); return 0; } @@ -447,7 +448,8 @@ static int dsa_port_bridge_create(struct dsa_port *dp, return -EOPNOTSUPP; } - dp->bridge = bridge; + /* Pairs with the READ_ONCE() of the datapath */ + smp_store_release(&dp->bridge, bridge); return 0; } @@ -457,7 +459,7 @@ static void dsa_port_bridge_destroy(struct dsa_port *dp, { struct dsa_bridge *bridge = dp->bridge; - dp->bridge = NULL; + WRITE_ONCE(dp->bridge, NULL); if (!refcount_dec_and_test(&bridge->refcount)) return; @@ -465,7 +467,8 @@ static void dsa_port_bridge_destroy(struct dsa_port *dp, if (bridge->num) dsa_bridge_num_put(br, bridge->num); - kfree(bridge); + /* The datapath may still hold the pointer it read from dp->bridge */ + kfree_rcu(bridge, rcu); } static bool dsa_port_supports_mst(struct dsa_port *dp) diff --git a/net/dsa/tag.h b/net/dsa/tag.h index cf52283fe9d..0e1be1da453 100644 --- a/net/dsa/tag.h +++ b/net/dsa/tag.h @@ -201,7 +201,7 @@ dsa_find_designated_bridge_port_by_vid(struct net_device *conduit, u16 vid) if (dp->type != DSA_PORT_TYPE_USER) continue; - if (!dp->bridge) + if (!READ_ONCE(dp->bridge)) continue; if (dp->stp_state != BR_STATE_LEARNING && @@ -238,7 +238,7 @@ static inline void dsa_default_offload_fwd_mark(struct sk_buff *skb) { struct dsa_port *dp = dsa_user_to_port(skb->dev); - skb->offload_fwd_mark = !!(dp->bridge); + skb->offload_fwd_mark = !!READ_ONCE(dp->bridge); } /* Helper for removing DSA header tags from packets in the RX path. diff --git a/net/dsa/tag_8021q.c b/net/dsa/tag_8021q.c index f91646e6024..ac9ba1ee04f 100644 --- a/net/dsa/tag_8021q.c +++ b/net/dsa/tag_8021q.c @@ -482,7 +482,9 @@ dsa_tag_8021q_find_port_by_vbid(struct net_device *conduit, int vbid) return NULL; dsa_tree_for_each_user_port(dp, dst) { - if (!dp->bridge) + const struct dsa_bridge *bridge = READ_ONCE(dp->bridge); + + if (!bridge) continue; if (dp->stp_state != BR_STATE_LEARNING && @@ -492,7 +494,7 @@ dsa_tag_8021q_find_port_by_vbid(struct net_device *conduit, int vbid) if (dp->cpu_dp != cpu_dp) continue; - if (dsa_port_bridge_num_get(dp) == vbid) + if (bridge->num == vbid) return dp->user; } -- 2.53.0 A frame received on a port that offloads a bridge carries offload_fwd_mark (dsa_default_offload_fwd_mark()). A tc mirred redirect or mirror, or an nft netdev fwd or dup, from that port to a standalone port of the same switch keeps the mark, and vsc73xx_xmit() and sja1105_imprecise_xmit() then look up the bridge of the standalone port: dsa_port_bridge_dev_get() returns NULL and br_vlan_enabled() dereferences it, in softirq, for every such frame. The same happens when a port leaves its bridge while a frame that the bridge marked is still queued for it. Both taggers also read dp->bridge twice, once for the device and once for the number. Read dp->bridge once. When the port is not in a bridge, it is a standalone port, so send the frame in its standalone VLAN, to the port alone, with the PCP of its TX queue, as for a frame without offload_fwd_mark. Both drivers are fixed in one commit because they share the mechanism. Found by an LLM review (Claude), and reproduced in QEMU with dsa_loop. Fixes: b6ad86e6ad6c ("net: dsa: sja1105: add bridge TX data plane offload based on tag_8021q") Fixes: 6c87e1a47928 ("net: dsa: vsc73xx: introduce tag 8021q for vsc73xx") Signed-off-by: Julius Bairaktaris --- net/dsa/tag_sja1105.c | 21 ++++++++++++++------- net/dsa/tag_vsc73xx_8021q.c | 14 +++++++++----- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/net/dsa/tag_sja1105.c b/net/dsa/tag_sja1105.c index bfe1f746f55..6c267a2a232 100644 --- a/net/dsa/tag_sja1105.c +++ b/net/dsa/tag_sja1105.c @@ -209,27 +209,34 @@ static u16 sja1105_xmit_tpid(struct dsa_port *dp) } static struct sk_buff *sja1105_imprecise_xmit(struct sk_buff *skb, - struct net_device *netdev) + struct net_device *netdev, u8 pcp) { struct dsa_port *dp = dsa_user_to_port(netdev); - unsigned int bridge_num = dsa_port_bridge_num_get(dp); - struct net_device *br = dsa_port_bridge_dev_get(dp); + const struct dsa_bridge *bridge = READ_ONCE(dp->bridge); u16 tx_vid; + /* The bridge marked the frame, then the port left it: the port is + * standalone now, send the frame to it alone. + */ + if (unlikely(!bridge)) + return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp), + (pcp << VLAN_PRIO_SHIFT) | + dsa_tag_8021q_standalone_vid(dp)); + /* If the port is under a VLAN-aware bridge, just slide the * VLAN-tagged packet into the FDB and hope for the best. * This works because we support a single VLAN-aware bridge * across the entire dst, and its VLANs cannot be shared with * any standalone port. */ - if (br_vlan_enabled(br)) + if (br_vlan_enabled(bridge->dev)) return skb; /* If the port is under a VLAN-unaware bridge, use an imprecise * TX VLAN that targets the bridge's entire broadcast domain, * instead of just the specific port. */ - tx_vid = dsa_tag_8021q_bridge_vid(bridge_num); + tx_vid = dsa_tag_8021q_bridge_vid(bridge->num); return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp), tx_vid); } @@ -273,7 +280,7 @@ static struct sk_buff *sja1105_xmit(struct sk_buff *skb, u16 tx_vid = dsa_tag_8021q_standalone_vid(dp); if (skb->offload_fwd_mark) - return sja1105_imprecise_xmit(skb, netdev); + return sja1105_imprecise_xmit(skb, netdev, pcp); /* Transmitting management traffic does not rely upon switch tagging, * but instead SPI-installed management routes. Part 2 of this @@ -304,7 +311,7 @@ static struct sk_buff *sja1110_xmit(struct sk_buff *skb, int trailer_pos; if (skb->offload_fwd_mark) - return sja1105_imprecise_xmit(skb, netdev); + return sja1105_imprecise_xmit(skb, netdev, pcp); /* Transmitting control packets is done using in-band control * extensions, while data packets are transmitted using diff --git a/net/dsa/tag_vsc73xx_8021q.c b/net/dsa/tag_vsc73xx_8021q.c index f4736a1a7a0..d65e5ddaa8c 100644 --- a/net/dsa/tag_vsc73xx_8021q.c +++ b/net/dsa/tag_vsc73xx_8021q.c @@ -17,13 +17,17 @@ vsc73xx_xmit(struct sk_buff *skb, struct net_device *netdev) u8 pcp; if (skb->offload_fwd_mark) { - unsigned int bridge_num = dsa_port_bridge_num_get(dp); - struct net_device *br = dsa_port_bridge_dev_get(dp); + const struct dsa_bridge *bridge = READ_ONCE(dp->bridge); - if (br_vlan_enabled(br)) - return skb; + /* The port may have left the bridge since the bridge marked + * the frame: then it goes to the port alone. + */ + if (bridge) { + if (br_vlan_enabled(bridge->dev)) + return skb; - tx_vid = dsa_tag_8021q_bridge_vid(bridge_num); + tx_vid = dsa_tag_8021q_bridge_vid(bridge->num); + } } pcp = netdev_txq_to_tc(netdev, queue_mapping); -- 2.53.0 dsa_xmit_ll() sends a frame with offload_fwd_mark as a FORWARD frame on behalf of the port's bridge, with the bridge number taken from dsa_port_bridge_num_get(). The port need not be in a bridge: a frame received on a bridged port keeps the mark through a tc mirred redirect or an nft netdev fwd to a standalone port of the same switch, and a frame that the bridge marked can still be queued for a port that has left the bridge. The bridge number is 0 then, and the frame goes out as FORWARD from device dst->last_switch, port 0, instead of FROM_CPU to the port. The function also reads dp->bridge twice, once for the number and once for the device. Read dp->bridge once, and send FORWARD only while the port is in a bridge; otherwise the frame goes FROM_CPU to the port, as a frame without the mark does. Found by an LLM review (Claude), and reproduced in QEMU with dsa_loop. Fixes: d82f8ab0d874 ("net: dsa: tag_dsa: offload the bridge forwarding process") Signed-off-by: Julius Bairaktaris --- net/dsa/tag_dsa.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/net/dsa/tag_dsa.c b/net/dsa/tag_dsa.c index d5ffee35fbb..9a89508e451 100644 --- a/net/dsa/tag_dsa.c +++ b/net/dsa/tag_dsa.c @@ -130,13 +130,17 @@ static struct sk_buff *dsa_xmit_ll(struct sk_buff *skb, struct net_device *dev, u8 extra) { struct dsa_port *dp = dsa_user_to_port(dev); - struct net_device *br_dev; + const struct dsa_bridge *bridge = READ_ONCE(dp->bridge); + struct net_device *br_dev = bridge ? bridge->dev : NULL; u8 tag_dev, tag_port; enum dsa_cmd cmd; u8 *dsa_header; - if (skb->offload_fwd_mark) { - unsigned int bridge_num = dsa_port_bridge_num_get(dp); + /* A frame keeps offload_fwd_mark through a redirect to a standalone + * port, and the port may have left its bridge since the bridge marked + * the frame: only a bridged port forwards for its bridge. + */ + if (skb->offload_fwd_mark && bridge) { struct dsa_switch_tree *dst = dp->ds->dst; cmd = DSA_CMD_FORWARD; @@ -145,7 +149,7 @@ static struct sk_buff *dsa_xmit_ll(struct sk_buff *skb, struct net_device *dev, * packets on behalf of a virtual switch device with an index * past the physical switches. */ - tag_dev = dst->last_switch + bridge_num; + tag_dev = dst->last_switch + bridge->num; tag_port = 0; } else { cmd = DSA_CMD_FROM_CPU; @@ -153,8 +157,6 @@ static struct sk_buff *dsa_xmit_ll(struct sk_buff *skb, struct net_device *dev, tag_port = dp->index; } - br_dev = dsa_port_bridge_dev_get(dp); - /* If frame is already 802.1Q tagged, we can convert it to a DSA * tag (avoiding a memmove), but only if the port is standalone * (in which case we always send FROM_CPU) or if the port's -- 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 ("Range of offloadable bridges exceeded"), as a port that joins a new bridge beyond the limit already is. 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 --- net/dsa/dsa.c | 5 +++++ net/dsa/port.c | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c index 2771c34e840..1c2a29678ac 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 997e68cd215..9cb441d16de 100644 --- a/net/dsa/port.c +++ b/net/dsa/port.c @@ -426,6 +426,16 @@ 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 numbered the bridge beyond this + * switch's limit, or left it without a number + */ + if (ds->max_num_bridges && + (!bridge->num || bridge->num > ds->max_num_bridges)) { + NL_SET_ERR_MSG_MOD(extack, + "Range of offloadable bridges exceeded"); + return -EOPNOTSUPP; + } + refcount_inc(&bridge->refcount); /* Pairs with the READ_ONCE() of the datapath */ smp_store_release(&dp->bridge, bridge); -- 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. Found by an LLM review (Claude). Fixes: f5e165e72b29 ("net: dsa: track unique bridge numbers across all DSA switch trees") Signed-off-by: Julius Bairaktaris --- 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 1c2a29678ac..67b5d2c42fb 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 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 --- 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 67b5d2c42fb..bcb398615fc 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