Skip to content

Commit 05f294a

Browse files
vivienJakub Kicinski
authored andcommitted
net: dsa: allocate ports on touch
Allocate the struct dsa_port the first time it is accessed with dsa_port_touch, and remove the static dsa_port array from the dsa_switch structure. Signed-off-by: Vivien Didelot <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent d5a619b commit 05f294a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

include/net/dsa.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ struct dsa_switch {
277277
*/
278278
bool vlan_filtering;
279279

280-
/* Dynamically allocated ports, keep last */
281280
size_t num_ports;
282-
struct dsa_port ports[];
283281
};
284282

285283
static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)

net/dsa/dsa2.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,13 @@ static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
588588
struct dsa_switch_tree *dst = ds->dst;
589589
struct dsa_port *dp;
590590

591-
dp = &ds->ports[index];
591+
list_for_each_entry(dp, &dst->ports, list)
592+
if (dp->ds == ds && dp->index == index)
593+
return dp;
594+
595+
dp = kzalloc(sizeof(*dp), GFP_KERNEL);
596+
if (!dp)
597+
return NULL;
592598

593599
dp->ds = ds;
594600
dp->index = index;
@@ -857,7 +863,7 @@ struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
857863
{
858864
struct dsa_switch *ds;
859865

860-
ds = devm_kzalloc(dev, struct_size(ds, ports, n), GFP_KERNEL);
866+
ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
861867
if (!ds)
862868
return NULL;
863869

@@ -885,6 +891,12 @@ static void dsa_switch_remove(struct dsa_switch *ds)
885891
{
886892
struct dsa_switch_tree *dst = ds->dst;
887893
unsigned int index = ds->index;
894+
struct dsa_port *dp, *next;
895+
896+
list_for_each_entry_safe(dp, next, &dst->ports, list) {
897+
list_del(&dp->list);
898+
kfree(dp);
899+
}
888900

889901
dsa_tree_remove_switch(dst, index);
890902
}

0 commit comments

Comments
 (0)