Skip to content

Commit

Permalink
net: dsa: allocate ports on touch
Browse files Browse the repository at this point in the history
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 <vivien.didelot@gmail.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
  • Loading branch information
Vivien Didelot authored and Jakub Kicinski committed Oct 22, 2019
1 parent d5a619b commit 05f294a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 0 additions & 2 deletions include/net/dsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ struct dsa_switch {
*/
bool vlan_filtering;

/* Dynamically allocated ports, keep last */
size_t num_ports;
struct dsa_port ports[];
};

static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
Expand Down
16 changes: 14 additions & 2 deletions net/dsa/dsa2.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,13 @@ static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
struct dsa_switch_tree *dst = ds->dst;
struct dsa_port *dp;

dp = &ds->ports[index];
list_for_each_entry(dp, &dst->ports, list)
if (dp->ds == ds && dp->index == index)
return dp;

dp = kzalloc(sizeof(*dp), GFP_KERNEL);
if (!dp)
return NULL;

dp->ds = ds;
dp->index = index;
Expand Down Expand Up @@ -857,7 +863,7 @@ struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
{
struct dsa_switch *ds;

ds = devm_kzalloc(dev, struct_size(ds, ports, n), GFP_KERNEL);
ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
if (!ds)
return NULL;

Expand Down Expand Up @@ -885,6 +891,12 @@ static void dsa_switch_remove(struct dsa_switch *ds)
{
struct dsa_switch_tree *dst = ds->dst;
unsigned int index = ds->index;
struct dsa_port *dp, *next;

list_for_each_entry_safe(dp, next, &dst->ports, list) {
list_del(&dp->list);
kfree(dp);
}

dsa_tree_remove_switch(dst, index);
}
Expand Down

0 comments on commit 05f294a

Please sign in to comment.