Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net] net: sparx5/lan969x: remove MDB entries on last bridge leave
@ 2026-11-23 14:55 Daniel Machon
  2026-11-24 14:56 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Machon @ 2026-11-23 14:55 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Steen Hegelund, UNGLinuxDriver, Casper Andersson
  Cc: netdev, linux-arm-kernel, Haoxiang Li

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 host groups, the MDB entries that copy a group to
the CPU. The bridge keeps them, and does not offload them again when a
port rejoins, as sparx5 does not ask it to replay its MDB. Host groups
added before the first port joins the bridge are not offloaded either,
so this matches the existing behaviour.

Found by an internal test. Reproduced on a lan9698 EV board, without
kernel modifications, 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

The fix was tested against that reproducer on the same board.

Fixes: 3bacfccdcb2d ("net: sparx5: Add mdb handlers")
Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
---
 .../ethernet/microchip/sparx5/sparx5_switchdev.c   | 41 +++++++++++++++++-----
 1 file changed, 33 insertions(+), 8 deletions(-)

diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c b/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c
index dac4dd833127a..65ce225baa023 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)
@@ -227,8 +251,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 +554,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 +568,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 +627,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))

---
base-commit: af6737b9066832a2017c281159950ba92385ce00
change-id: 20261123-sparx5-mdb-bridge-leave-db49b15ac354

Best regards,
-- 
Daniel Machon <daniel.machon@microchip.com>



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net] net: sparx5/lan969x: remove MDB entries on last bridge leave
  2026-11-23 14:55 [PATCH net] net: sparx5/lan969x: remove MDB entries on last bridge leave Daniel Machon
@ 2026-11-24 14:56 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 14:56 UTC (permalink / raw)
  To: daniel.machon
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, Steen.Hegelund,
	UNGLinuxDriver, casper.casan, netdev, linux-arm-kernel,
	haoxiang_li2024

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 (1) · 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] The patch makes host-joined MDB groups disappear from hardware
  when the last sparx5 port leaves a bridge that stays up, and nothing…
- [Low] sparx5_mdb_flush() ignores the return value of
  sparx5_mact_forget(), which can be -ETIMEDOUT from…

Pre-existing issues:
- [Medium] MDB entries leak when the driver is unbound while ports are
  bridged.

--

net: sparx5/lan969x: remove MDB entries on last bridge leave

This patch fixes a NULL pointer dereference in sparx5_handle_port_mdb_del()
that happens when a bridge with a permanent MDB entry on one of its ports is
deleted. When the last port leaves the bridge, all MDB entries are now
flushed before hw_bridge_dev is cleared. MDB adds and deletes are ignored
while no bridge is offloaded.

> diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c b/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c
> index dac4dd833127a..65ce225baa023 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,

[ ... ]

> +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);

[Severity: Medium]
Can flushing the host-only entries here leave the hardware out of sync
with the bridge? This would happen when the bridge stays up and a port
rejoins it later.

After the last port leaves, the bridge still holds the host group (an
entry with cpu_copy set and an empty port_mask).
br_multicast_del_port() only deletes the leaving port's groups from
port->mglist, so mp->host_joined stays true. br_multicast_host_join()
only notifies when host_joined goes from false to true.

When a port rejoins, sparx5_port_bridge_join() passes no notifier blocks:

	err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
					    false, extack);

so br_switchdev_mdb_replay() returns early:

	if (!nb)
		return 0;

Nothing adds the MAC/PGID entry with CPU copy back. This sequence looks
like enough to hit it:

  ip link add br0 type bridge
  ip link set p1 master br0
  ip link set br0 up
  bridge mdb add dev br0 port br0 grp 239.1.1.1 permanent
  ip link set p1 nomaster
  ip link set p1 master br0

Afterwards the bridge still lists 239.1.1.1 as host-joined, but the
switch no longer copies the group to the CPU. Dynamic host joins look
the same, because later host reports only refresh the timer.

Before this patch, sparx5_port_bridge_leave() did not touch the MDB list
or the hardware entries, so this leave/rejoin case kept working. The
commit message says:

  "Host groups added before the first port joins the bridge are not
  offloaded either, so this matches the existing behaviour."

Does that hold for the leave/rejoin case as well?

Could the flush keep the host-only entries? Another option is to pass
notifier blocks to switchdev_bridge_port_offload() so the bridge replays
its MDB. lan966x_port_bridge_join() does this with &lan966x_switchdev_nb
and &lan966x_switchdev_blocking_nb.

> +		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);

[Severity: Low]
sparx5_mact_forget() can return -ETIMEDOUT from
sparx5_mact_wait_for_completion(). Is it safe to ignore that and still
return the PGID to the pool?

If the UNLEARN did not complete, the MAC table could still have an entry
pointing at this PGID. sparx5_pgid_alloc_mcast() hands out the lowest
free index first, so the next group would probably get this PGID.

This needs a MAC table engine timeout. sparx5_handle_port_mdb_del()
already frees the PGID unconditionally after sparx5_mact_forget() in the
same way.

> +		kfree(entry);
> +	}
> +	mutex_unlock(&sparx5->mdb_lock);
> +}

[Severity: Medium]
This is a pre-existing issue, not one introduced by this patch, but are
the MDB entries still leaked when the driver is unbound while ports are
bridged?

mchp_sparx5_remove() unregisters the notifiers before the netdevs:

	sparx5_unregister_notifier_blocks(sparx5);
	sparx5_unregister_netdevs(sparx5);

The CHANGEUPPER sent when the bridge ports are removed therefore never
reaches sparx5_port_bridge_leave(), and sparx5_mdb_flush() never runs.
The deferred switchdev MDB deletes are not delivered either.

sparx5_mact_deinit() does not walk sparx5->mdb_entries:

	cancel_delayed_work_sync(&sparx5->mact_work);
	destroy_workqueue(sparx5->mact_queue);
	mutex_destroy(&sparx5->mact_lock);

So the sparx5_mdb_entry allocations are never freed, and mdb_lock is
never destroyed.

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123-sparx5-mdb-bridge-leave-v1-1-40b50d7b7a1c%40microchip.com


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24 15:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 14:55 [PATCH net] net: sparx5/lan969x: remove MDB entries on last bridge leave Daniel Machon
2026-11-24 14:56 ` 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