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