Index each port's multicast groups by VID in a fixed 64-bucket hash table. Maintain the index under multicast_lock alongside mglist, including groups retained without port VLAN membership, and remove entries on both port-group deletion paths. Use the index when counting groups for a newly created port VLAN context, avoiding a scan of all groups on the port for every added VLAN. Keep mglist for its existing RCU readers and teardown walks. The index also lets VLAN publication and unpublication update cached destinations without repeatedly walking the full group list. Operation VLANs Global ms before/after Change % Per-VLAN ms before/after Change % ------------------------------------------------------------------------------------------ add 1 0.0519 / 0.0900 +73.52 0.0481 / 0.0895 +86.16 add 64 2.2500 / 0.7190 -68.04 2.2726 / 0.7441 -67.26 add 1024 32.7464 / 5.7028 -82.58 32.9841 / 6.3116 -80.86 add 4094 149.6750 / 34.3223 -77.07 151.5808 / 35.4586 -76.61 Note that the struct net_bridge_port_group increase pushes the allocations above 256 bytes and into kmalloc-512, this can be kept compact by introducing a kmalloc cache for these. It can be done separately. Signed-off-by: Nikolay Aleksandrov --- CC: Ido Schimmel CC: David S. Miller CC: Eric Dumazet CC: Jakub Kicinski CC: Paolo Abeni CC: Simon Horman CC: bridge@lists.linux.dev net/bridge/br_multicast.c | 7 ++++++- net/bridge/br_private.h | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/net/bridge/br_multicast.c b/net/bridge/br_multicast.c index ae4fa050be97..7285229ca6d7 100644 --- a/net/bridge/br_multicast.c +++ b/net/bridge/br_multicast.c @@ -826,6 +826,7 @@ static void __br_multicast_del_pg(struct net_bridge_mdb_entry *mp, * and deleting other groups of the same port must not truncate it. */ hlist_del_init_rcu(&pg->mglist); + hash_del(&pg->vlan_node); br_multicast_eht_clean_sets(pg); hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node) br_multicast_del_group_src(ent, false); @@ -1472,6 +1473,7 @@ struct net_bridge_port_group *br_multicast_new_port_group( RCU_INIT_POINTER(p->next, next); hlist_add_head_rcu(&p->mglist, &port->mglist); + hash_add(port->mdb_vlan_hash, &p->vlan_node, group->vid); return p; @@ -1488,6 +1490,7 @@ void br_multicast_del_port_group(struct net_bridge_port_group *p) __u16 vid = p->key.addr.vid; hlist_del_init_rcu(&p->mglist); + hash_del(&p->vlan_node); if (!br_multicast_is_star_g(&p->key.addr)) rhashtable_remove_fast(&port->br->sg_port_tbl, &p->rhnode, br_sg_port_rht_params); @@ -2105,7 +2108,8 @@ void br_multicast_port_ctx_init(struct net_bridge_port *port, u32 n = 0; spin_lock_bh(&port->br->multicast_lock); - hlist_for_each_entry(pg, &port->mglist, mglist) + hash_for_each_possible(port->mdb_vlan_hash, pg, vlan_node, + vlan->vid) if (pg->key.addr.vid == vlan->vid) n++; WRITE_ONCE(pmctx->mdb_n_entries, n); @@ -2138,6 +2142,7 @@ int br_multicast_add_port(struct net_bridge_port *port) { int err; + hash_init(port->mdb_vlan_hash); port->multicast_eht_hosts_limit = BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT; br_multicast_port_ctx_init(port, NULL, &port->multicast_ctx); diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h index 20e423e22132..04430dc32d02 100644 --- a/net/bridge/br_private.h +++ b/net/bridge/br_private.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #define BR_HASH_BITS 8 @@ -29,6 +30,7 @@ #define BR_MAX_PORTS (1<