Skip to content

Commit 7f29731

Browse files
vladimirolteandavem330
authored andcommitted
net: dsa: make tagging protocols connect to individual switches from a tree
On the NXP Bluebox 3 board which uses a multi-switch setup with sja1105, the mechanism through which the tagger connects to the switch tree is broken, due to improper DSA code design. At the time when tag_ops->connect() is called in dsa_port_parse_cpu(), DSA hasn't finished "touching" all the ports, so it doesn't know how large the tree is and how many ports it has. It has just seen the first CPU port by this time. As a result, this function will call the tagger's ->connect method too early, and the tagger will connect only to the first switch from the tree. This could be perhaps addressed a bit more simply by just moving the tag_ops->connect(dst) call a bit later (for example in dsa_tree_setup), but there is already a design inconsistency at present: on the switch side, the notification is on a per-switch basis, but on the tagger side, it is on a per-tree basis. Furthermore, the persistent storage itself is per switch (ds->tagger_data). And the tagger connect and disconnect procedures (at least the ones that exist currently) could see a fair bit of simplification if they didn't have to iterate through the switches of a tree. To fix the issue, this change transforms tag_ops->connect(dst) into tag_ops->connect(ds) and moves it somewhere where we already iterate over all switches of a tree. That is in dsa_switch_setup_tag_protocol(), which is a good placement because we already have there the connection call to the switch side of things. As for the dsa_tree_bind_tag_proto() method (called from the code path that changes the tag protocol), things are a bit more complicated because we receive the tree as argument, yet when we unwind on errors, it would be nice to not call tag_ops->disconnect(ds) where we didn't previously call tag_ops->connect(ds). We didn't have this problem before because the tag_ops connection operations passed the entire dst before, and this is more fine grained now. To solve the error rewind case using the new API, we have to create yet one more cross-chip notifier for disconnection, and stay connected with the old tag protocol to all the switches in the tree until we've succeeded to connect with the new one as well. So if something fails half way, the whole tree is still connected to the old tagger. But there may still be leaks if the tagger fails to connect to the 2nd out of 3 switches in a tree: somebody needs to tell the tagger to disconnect from the first switch. Nothing comes for free, and this was previously handled privately by the tagging protocol driver before, but now we need to emit a disconnect cross-chip notifier for that, because DSA has to take care of the unwind path. We assume that the tagging protocol has connected to a switch if it has set ds->tagger_data to something, otherwise we avoid calling its disconnection method in the error rewind path. The rest of the changes are in the tagging protocol drivers, and have to do with the replacement of dst with ds. The iteration is removed and the error unwind path is simplified, as mentioned above. Signed-off-by: Vladimir Oltean <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent c8a2a01 commit 7f29731

File tree

6 files changed

+109
-113
lines changed

6 files changed

+109
-113
lines changed

include/net/dsa.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ enum dsa_tag_protocol {
8282
};
8383

8484
struct dsa_switch;
85-
struct dsa_switch_tree;
8685

8786
struct dsa_device_ops {
8887
struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
8988
struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
9089
void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
9190
int *offset);
92-
int (*connect)(struct dsa_switch_tree *dst);
93-
void (*disconnect)(struct dsa_switch_tree *dst);
91+
int (*connect)(struct dsa_switch *ds);
92+
void (*disconnect)(struct dsa_switch *ds);
9493
unsigned int needed_headroom;
9594
unsigned int needed_tailroom;
9695
const char *name;

net/dsa/dsa2.c

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,8 @@ static struct dsa_switch_tree *dsa_tree_alloc(int index)
248248

249249
static void dsa_tree_free(struct dsa_switch_tree *dst)
250250
{
251-
if (dst->tag_ops) {
252-
if (dst->tag_ops->disconnect)
253-
dst->tag_ops->disconnect(dst);
254-
251+
if (dst->tag_ops)
255252
dsa_tag_driver_put(dst->tag_ops);
256-
}
257253
list_del(&dst->list);
258254
kfree(dst);
259255
}
@@ -841,17 +837,29 @@ static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
841837
}
842838

843839
connect:
840+
if (tag_ops->connect) {
841+
err = tag_ops->connect(ds);
842+
if (err)
843+
return err;
844+
}
845+
844846
if (ds->ops->connect_tag_protocol) {
845847
err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
846848
if (err) {
847849
dev_err(ds->dev,
848850
"Unable to connect to tag protocol \"%s\": %pe\n",
849851
tag_ops->name, ERR_PTR(err));
850-
return err;
852+
goto disconnect;
851853
}
852854
}
853855

854856
return 0;
857+
858+
disconnect:
859+
if (tag_ops->disconnect)
860+
tag_ops->disconnect(ds);
861+
862+
return err;
855863
}
856864

857865
static int dsa_switch_setup(struct dsa_switch *ds)
@@ -1160,13 +1168,6 @@ static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
11601168

11611169
dst->tag_ops = tag_ops;
11621170

1163-
/* Notify the new tagger about the connection to this tree */
1164-
if (tag_ops->connect) {
1165-
err = tag_ops->connect(dst);
1166-
if (err)
1167-
goto out_revert;
1168-
}
1169-
11701171
/* Notify the switches from this tree about the connection
11711172
* to the new tagger
11721173
*/
@@ -1176,16 +1177,14 @@ static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
11761177
goto out_disconnect;
11771178

11781179
/* Notify the old tagger about the disconnection from this tree */
1179-
if (old_tag_ops->disconnect)
1180-
old_tag_ops->disconnect(dst);
1180+
info.tag_ops = old_tag_ops;
1181+
dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
11811182

11821183
return 0;
11831184

11841185
out_disconnect:
1185-
/* Revert the new tagger's connection to this tree */
1186-
if (tag_ops->disconnect)
1187-
tag_ops->disconnect(dst);
1188-
out_revert:
1186+
info.tag_ops = tag_ops;
1187+
dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
11891188
dst->tag_ops = old_tag_ops;
11901189

11911190
return err;
@@ -1318,7 +1317,6 @@ static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
13181317
struct dsa_switch_tree *dst = ds->dst;
13191318
const struct dsa_device_ops *tag_ops;
13201319
enum dsa_tag_protocol default_proto;
1321-
int err;
13221320

13231321
/* Find out which protocol the switch would prefer. */
13241322
default_proto = dsa_get_tag_protocol(dp, master);
@@ -1366,12 +1364,6 @@ static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
13661364
*/
13671365
dsa_tag_driver_put(tag_ops);
13681366
} else {
1369-
if (tag_ops->connect) {
1370-
err = tag_ops->connect(dst);
1371-
if (err)
1372-
return err;
1373-
}
1374-
13751367
dst->tag_ops = tag_ops;
13761368
}
13771369

net/dsa/dsa_priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum {
3838
DSA_NOTIFIER_MTU,
3939
DSA_NOTIFIER_TAG_PROTO,
4040
DSA_NOTIFIER_TAG_PROTO_CONNECT,
41+
DSA_NOTIFIER_TAG_PROTO_DISCONNECT,
4142
DSA_NOTIFIER_MRP_ADD,
4243
DSA_NOTIFIER_MRP_DEL,
4344
DSA_NOTIFIER_MRP_ADD_RING_ROLE,

net/dsa/switch.c

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,15 +647,58 @@ static int dsa_switch_change_tag_proto(struct dsa_switch *ds,
647647
return 0;
648648
}
649649

650-
static int dsa_switch_connect_tag_proto(struct dsa_switch *ds,
651-
struct dsa_notifier_tag_proto_info *info)
650+
/* We use the same cross-chip notifiers to inform both the tagger side, as well
651+
* as the switch side, of connection and disconnection events.
652+
* Since ds->tagger_data is owned by the tagger, it isn't a hard error if the
653+
* switch side doesn't support connecting to this tagger, and therefore, the
654+
* fact that we don't disconnect the tagger side doesn't constitute a memory
655+
* leak: the tagger will still operate with persistent per-switch memory, just
656+
* with the switch side unconnected to it. What does constitute a hard error is
657+
* when the switch side supports connecting but fails.
658+
*/
659+
static int
660+
dsa_switch_connect_tag_proto(struct dsa_switch *ds,
661+
struct dsa_notifier_tag_proto_info *info)
652662
{
653663
const struct dsa_device_ops *tag_ops = info->tag_ops;
664+
int err;
665+
666+
/* Notify the new tagger about the connection to this switch */
667+
if (tag_ops->connect) {
668+
err = tag_ops->connect(ds);
669+
if (err)
670+
return err;
671+
}
654672

655673
if (!ds->ops->connect_tag_protocol)
656674
return -EOPNOTSUPP;
657675

658-
return ds->ops->connect_tag_protocol(ds, tag_ops->proto);
676+
/* Notify the switch about the connection to the new tagger */
677+
err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
678+
if (err) {
679+
/* Revert the new tagger's connection to this tree */
680+
if (tag_ops->disconnect)
681+
tag_ops->disconnect(ds);
682+
return err;
683+
}
684+
685+
return 0;
686+
}
687+
688+
static int
689+
dsa_switch_disconnect_tag_proto(struct dsa_switch *ds,
690+
struct dsa_notifier_tag_proto_info *info)
691+
{
692+
const struct dsa_device_ops *tag_ops = info->tag_ops;
693+
694+
/* Notify the tagger about the disconnection from this switch */
695+
if (tag_ops->disconnect && ds->tagger_data)
696+
tag_ops->disconnect(ds);
697+
698+
/* No need to notify the switch, since it shouldn't have any
699+
* resources to tear down
700+
*/
701+
return 0;
659702
}
660703

661704
static int dsa_switch_mrp_add(struct dsa_switch *ds,
@@ -780,6 +823,9 @@ static int dsa_switch_event(struct notifier_block *nb,
780823
case DSA_NOTIFIER_TAG_PROTO_CONNECT:
781824
err = dsa_switch_connect_tag_proto(ds, info);
782825
break;
826+
case DSA_NOTIFIER_TAG_PROTO_DISCONNECT:
827+
err = dsa_switch_disconnect_tag_proto(ds, info);
828+
break;
783829
case DSA_NOTIFIER_MRP_ADD:
784830
err = dsa_switch_mrp_add(ds, info);
785831
break;

net/dsa/tag_ocelot_8021q.c

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -81,55 +81,34 @@ static struct sk_buff *ocelot_rcv(struct sk_buff *skb,
8181
return skb;
8282
}
8383

84-
static void ocelot_disconnect(struct dsa_switch_tree *dst)
84+
static void ocelot_disconnect(struct dsa_switch *ds)
8585
{
86-
struct ocelot_8021q_tagger_private *priv;
87-
struct dsa_port *dp;
88-
89-
list_for_each_entry(dp, &dst->ports, list) {
90-
priv = dp->ds->tagger_data;
91-
92-
if (!priv)
93-
continue;
86+
struct ocelot_8021q_tagger_private *priv = ds->tagger_data;
9487

95-
if (priv->xmit_worker)
96-
kthread_destroy_worker(priv->xmit_worker);
97-
98-
kfree(priv);
99-
dp->ds->tagger_data = NULL;
100-
}
88+
kthread_destroy_worker(priv->xmit_worker);
89+
kfree(priv);
90+
ds->tagger_data = NULL;
10191
}
10292

103-
static int ocelot_connect(struct dsa_switch_tree *dst)
93+
static int ocelot_connect(struct dsa_switch *ds)
10494
{
10595
struct ocelot_8021q_tagger_private *priv;
106-
struct dsa_port *dp;
10796
int err;
10897

109-
list_for_each_entry(dp, &dst->ports, list) {
110-
if (dp->ds->tagger_data)
111-
continue;
98+
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
99+
if (!priv)
100+
return -ENOMEM;
112101

113-
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
114-
if (!priv) {
115-
err = -ENOMEM;
116-
goto out;
117-
}
118-
119-
priv->xmit_worker = kthread_create_worker(0, "felix_xmit");
120-
if (IS_ERR(priv->xmit_worker)) {
121-
err = PTR_ERR(priv->xmit_worker);
122-
goto out;
123-
}
124-
125-
dp->ds->tagger_data = priv;
102+
priv->xmit_worker = kthread_create_worker(0, "felix_xmit");
103+
if (IS_ERR(priv->xmit_worker)) {
104+
err = PTR_ERR(priv->xmit_worker);
105+
kfree(priv);
106+
return err;
126107
}
127108

128-
return 0;
109+
ds->tagger_data = priv;
129110

130-
out:
131-
ocelot_disconnect(dst);
132-
return err;
111+
return 0;
133112
}
134113

135114
static const struct dsa_device_ops ocelot_8021q_netdev_ops = {

net/dsa/tag_sja1105.c

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -741,65 +741,44 @@ static void sja1110_flow_dissect(const struct sk_buff *skb, __be16 *proto,
741741
*proto = ((__be16 *)skb->data)[(VLAN_HLEN / 2) - 1];
742742
}
743743

744-
static void sja1105_disconnect(struct dsa_switch_tree *dst)
744+
static void sja1105_disconnect(struct dsa_switch *ds)
745745
{
746-
struct sja1105_tagger_private *priv;
747-
struct dsa_port *dp;
748-
749-
list_for_each_entry(dp, &dst->ports, list) {
750-
priv = dp->ds->tagger_data;
751-
752-
if (!priv)
753-
continue;
746+
struct sja1105_tagger_private *priv = ds->tagger_data;
754747

755-
if (priv->xmit_worker)
756-
kthread_destroy_worker(priv->xmit_worker);
757-
758-
kfree(priv);
759-
dp->ds->tagger_data = NULL;
760-
}
748+
kthread_destroy_worker(priv->xmit_worker);
749+
kfree(priv);
750+
ds->tagger_data = NULL;
761751
}
762752

763-
static int sja1105_connect(struct dsa_switch_tree *dst)
753+
static int sja1105_connect(struct dsa_switch *ds)
764754
{
765755
struct sja1105_tagger_data *tagger_data;
766756
struct sja1105_tagger_private *priv;
767757
struct kthread_worker *xmit_worker;
768-
struct dsa_port *dp;
769758
int err;
770759

771-
list_for_each_entry(dp, &dst->ports, list) {
772-
if (dp->ds->tagger_data)
773-
continue;
760+
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
761+
if (!priv)
762+
return -ENOMEM;
774763

775-
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
776-
if (!priv) {
777-
err = -ENOMEM;
778-
goto out;
779-
}
780-
781-
spin_lock_init(&priv->meta_lock);
782-
783-
xmit_worker = kthread_create_worker(0, "dsa%d:%d_xmit",
784-
dst->index, dp->ds->index);
785-
if (IS_ERR(xmit_worker)) {
786-
err = PTR_ERR(xmit_worker);
787-
goto out;
788-
}
764+
spin_lock_init(&priv->meta_lock);
789765

790-
priv->xmit_worker = xmit_worker;
791-
/* Export functions for switch driver use */
792-
tagger_data = &priv->data;
793-
tagger_data->rxtstamp_get_state = sja1105_rxtstamp_get_state;
794-
tagger_data->rxtstamp_set_state = sja1105_rxtstamp_set_state;
795-
dp->ds->tagger_data = priv;
766+
xmit_worker = kthread_create_worker(0, "dsa%d:%d_xmit",
767+
ds->dst->index, ds->index);
768+
if (IS_ERR(xmit_worker)) {
769+
err = PTR_ERR(xmit_worker);
770+
kfree(priv);
771+
return err;
796772
}
797773

798-
return 0;
774+
priv->xmit_worker = xmit_worker;
775+
/* Export functions for switch driver use */
776+
tagger_data = &priv->data;
777+
tagger_data->rxtstamp_get_state = sja1105_rxtstamp_get_state;
778+
tagger_data->rxtstamp_set_state = sja1105_rxtstamp_set_state;
779+
ds->tagger_data = priv;
799780

800-
out:
801-
sja1105_disconnect(dst);
802-
return err;
781+
return 0;
803782
}
804783

805784
static const struct dsa_device_ops sja1105_netdev_ops = {

0 commit comments

Comments
 (0)