[PATCH net v3] net: sparx5/lan969x: remove MDB entries on last bridge leave
Daniel Machon
daniel.machon at microchip.com
Thu Dec 3 06:11:38 PST 2026
sparx5_port_bridge_leave() clears hw_bridge_dev when the last port
leaves the bridge. The bridge deletes the permanent MDB entries of a
port only after the port has left it, in br_multicast_del_port(), and
the deletes reach the driver from the switchdev deferred work.
Before this patch, sparx5_handle_port_mdb_del() then calls
br_vlan_enabled() on the NULL hw_bridge_dev, so deleting a bridge with a
permanent MDB entry on one of its ports crashes the kernel:
Unable to handle kernel NULL pointer dereference at virtual address 0000000000000c10
Workqueue: events switchdev_deferred_process_work
pc : br_vlan_enabled+0x0/0xc
Remove all MDB entries when the last port leaves the bridge, before
hw_bridge_dev is cleared, and ignore MDB adds and deletes while no
bridge is offloaded. Disable the CPU copy of a host group before its
PGID is freed, as sparx5_pgid_alloc_mcast() does not reset it for the
next group.
The flush also removes the host groups, which the bridge keeps. Have the
bridge replay its MDB when a port joins, so they are offloaded again,
and ignore replayed objects of other ports. Flush again if the first
port fails to join, as the bridge does not undo a partial replay. As
the replay calls the blocking notifier block directly, set it up before
a port can join.
Found by an internal test. Reproduced on a lan9698 EV board by deleting
a bridge with a permanent group on a single port:
ip link add br0 type bridge
ip link set <port> master br0
ip link set br0 up
ip link set <port> up
bridge mdb add dev br0 port <port> grp 239.1.1.1 permanent
ip link del br0
Fixes: 3bacfccdcb2d ("net: sparx5: Add mdb handlers")
Signed-off-by: Daniel Machon <daniel.machon at microchip.com>
---
This is fine to take through net-next instead, if preferred.
It conflicts with the sparx5 router legs in net-next ("net: sparx5:
keep router legs in sync with the bridge"). The resolution in
sparx5_port_bridge_leave() keeps both flushes:
if (bitmap_empty(sparx5->bridge_mask, SPX5_PORTS)) {
/* L3 offload has no datapath left on this bridge. */
sparx5_rr_bridge_legs_flush(sparx5, bridge);
sparx5_mdb_flush(sparx5);
sparx5->hw_bridge_dev = NULL;
}
---
Changes in v3:
- Set up the blocking notifier block before the netdevice notifier is
registered, so a port that joins the bridge during probe does not
replay to a NULL notifier_call.
- Rebase on net.
- Note the conflict with the sparx5 router legs in net-next, and how to
resolve it.
- Link to v2: https://lore.kernel.org/r/20261201-sparx5-mdb-bridge-leave-v2-1-127cacfcba6d@microchip.com
Changes in v2:
- Replay the MDB when a port joins the bridge, so host groups are
offloaded again when a port rejoins the bridge.
- Link to v1: https://lore.kernel.org/r/20261123-sparx5-mdb-bridge-leave-v1-1-40b50d7b7a1c@microchip.com
---
.../ethernet/microchip/sparx5/sparx5_switchdev.c | 53 ++++++++++++++++++----
1 file changed, 43 insertions(+), 10 deletions(-)
diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c b/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c
index dac4dd833127a..5bd5d3474e54a 100644
--- a/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c
+++ b/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c
@@ -172,6 +172,30 @@ static int sparx5_port_attr_set(struct net_device *dev, const void *ctx,
return 0;
}
+static void sparx5_cpu_copy_ena(struct sparx5 *spx5, u16 pgid, bool enable)
+{
+ spx5_rmw(ANA_AC_PGID_MISC_CFG_PGID_CPU_COPY_ENA_SET(enable),
+ ANA_AC_PGID_MISC_CFG_PGID_CPU_COPY_ENA, spx5,
+ ANA_AC_PGID_MISC_CFG(pgid));
+}
+
+static void sparx5_mdb_flush(struct sparx5 *sparx5)
+{
+ struct sparx5_mdb_entry *entry, *tmp;
+
+ mutex_lock(&sparx5->mdb_lock);
+ list_for_each_entry_safe(entry, tmp, &sparx5->mdb_entries, list) {
+ if (entry->cpu_copy)
+ sparx5_cpu_copy_ena(sparx5, entry->pgid_idx, false);
+ sparx5_pgid_clear(sparx5, entry->pgid_idx);
+ sparx5_mact_forget(sparx5, entry->addr, entry->vid);
+ list_del(&entry->list);
+ sparx5_pgid_free(sparx5, entry->pgid_idx);
+ kfree(entry);
+ }
+ mutex_unlock(&sparx5->mdb_lock);
+}
+
static int sparx5_port_bridge_join(struct sparx5_port *port,
struct net_device *bridge,
struct netlink_ext_ack *extack)
@@ -193,7 +217,8 @@ static int sparx5_port_bridge_join(struct sparx5_port *port,
set_bit(port->portno, sparx5->bridge_mask);
- err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
+ err = switchdev_bridge_port_offload(ndev, ndev, port, NULL,
+ &sparx5->switchdev_blocking_nb,
false, extack);
if (err)
goto err_switchdev_offload;
@@ -215,6 +240,10 @@ static int sparx5_port_bridge_join(struct sparx5_port *port,
err_switchdev_offload:
clear_bit(port->portno, sparx5->bridge_mask);
+ if (bitmap_empty(sparx5->bridge_mask, SPX5_PORTS)) {
+ sparx5_mdb_flush(sparx5);
+ sparx5->hw_bridge_dev = NULL;
+ }
return err;
}
@@ -227,8 +256,10 @@ static void sparx5_port_bridge_leave(struct sparx5_port *port,
switchdev_bridge_port_unoffload(port->ndev, NULL, NULL, NULL);
clear_bit(port->portno, sparx5->bridge_mask);
- if (bitmap_empty(sparx5->bridge_mask, SPX5_PORTS))
+ if (bitmap_empty(sparx5->bridge_mask, SPX5_PORTS)) {
+ sparx5_mdb_flush(sparx5);
sparx5->hw_bridge_dev = NULL;
+ }
/* Clear bridge vlan settings before updating the port settings */
port->vlan_aware = 0;
@@ -528,13 +559,6 @@ static struct sparx5_mdb_entry *sparx5_mdb_get_entry(struct sparx5 *sparx5,
return found;
}
-static void sparx5_cpu_copy_ena(struct sparx5 *spx5, u16 pgid, bool enable)
-{
- spx5_rmw(ANA_AC_PGID_MISC_CFG_PGID_CPU_COPY_ENA_SET(enable),
- ANA_AC_PGID_MISC_CFG_PGID_CPU_COPY_ENA, spx5,
- ANA_AC_PGID_MISC_CFG(pgid));
-}
-
static int sparx5_handle_port_mdb_add(struct net_device *dev,
struct notifier_block *nb,
const struct switchdev_obj_port_mdb *v)
@@ -549,6 +573,9 @@ static int sparx5_handle_port_mdb_add(struct net_device *dev,
if (!sparx5_netdevice_check(dev))
return -EOPNOTSUPP;
+ if (!spx5->hw_bridge_dev)
+ return 0;
+
is_host = netif_is_bridge_master(v->obj.orig_dev);
/* When VLAN unaware the vlan value is not parsed and we receive vid 0.
@@ -605,6 +632,9 @@ static int sparx5_handle_port_mdb_del(struct net_device *dev,
if (!sparx5_netdevice_check(dev))
return -EOPNOTSUPP;
+ if (!spx5->hw_bridge_dev)
+ return 0;
+
is_host = netif_is_bridge_master(v->obj.orig_dev);
if (!br_vlan_enabled(spx5->hw_bridge_dev))
@@ -647,6 +677,9 @@ static int sparx5_handle_port_obj_add(struct net_device *dev,
const struct switchdev_obj *obj = info->obj;
int err;
+ if (info->info.ctx && info->info.ctx != netdev_priv(dev))
+ return 0;
+
switch (obj->id) {
case SWITCHDEV_OBJ_ID_PORT_VLAN:
err = sparx5_handle_port_vlan_add(dev, nb,
@@ -748,6 +781,7 @@ int sparx5_register_notifier_blocks(struct sparx5 *s5)
int err;
s5->netdevice_nb.notifier_call = sparx5_netdevice_event;
+ s5->switchdev_blocking_nb.notifier_call = sparx5_switchdev_blocking_event;
err = register_netdevice_notifier(&s5->netdevice_nb);
if (err)
return err;
@@ -757,7 +791,6 @@ int sparx5_register_notifier_blocks(struct sparx5 *s5)
if (err)
goto err_switchdev_nb;
- s5->switchdev_blocking_nb.notifier_call = sparx5_switchdev_blocking_event;
err = register_switchdev_blocking_notifier(&s5->switchdev_blocking_nb);
if (err)
goto err_switchdev_blocking_nb;
---
base-commit: 9d71a9bfd9cb92614d171af1b95b83db6a5d1d25
change-id: 20261123-sparx5-mdb-bridge-leave-db49b15ac354
Best regards,
--
Daniel Machon <daniel.machon at microchip.com>
More information about the linux-arm-kernel
mailing list