diff options
author | Vladimir Oltean <vladimir.oltean@nxp.com> | 2021-12-06 17:57:47 +0100 |
---|---|---|
committer | Jakub Kicinski <kuba@kernel.org> | 2021-12-08 23:31:14 +0100 |
commit | 3f9bb0301d50ce27421eff4b710c2bbe58111a83 (patch) | |
tree | 91271bc43b14b643d9b82b08df871ee3d2e8da22 /net/dsa/dsa2.c | |
parent | Merge branch 's390-net-updates-2021-12-06' (diff) | |
download | linux-3f9bb0301d50ce27421eff4b710c2bbe58111a83.tar.xz linux-3f9bb0301d50ce27421eff4b710c2bbe58111a83.zip |
net: dsa: make dp->bridge_num one-based
I have seen too many bugs already due to the fact that we must encode an
invalid dp->bridge_num as a negative value, because the natural tendency
is to check that invalid value using (!dp->bridge_num). Latest example
can be seen in commit 1bec0f05062c ("net: dsa: fix bridge_num not
getting cleared after ports leaving the bridge").
Convert the existing users to assume that dp->bridge_num == 0 is the
encoding for invalid, and valid bridge numbers start from 1.
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Diffstat (limited to 'net/dsa/dsa2.c')
-rw-r--r-- | net/dsa/dsa2.c | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/net/dsa/dsa2.c b/net/dsa/dsa2.c index 826957b6442b..9606e56710a5 100644 --- a/net/dsa/dsa2.c +++ b/net/dsa/dsa2.c @@ -141,23 +141,23 @@ static int dsa_bridge_num_find(const struct net_device *bridge_dev) */ list_for_each_entry(dst, &dsa_tree_list, list) list_for_each_entry(dp, &dst->ports, list) - if (dp->bridge_dev == bridge_dev && - dp->bridge_num != -1) + if (dp->bridge_dev == bridge_dev && dp->bridge_num) return dp->bridge_num; - return -1; + return 0; } -int dsa_bridge_num_get(const struct net_device *bridge_dev, int max) +unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max) { - int bridge_num = dsa_bridge_num_find(bridge_dev); + unsigned int bridge_num = dsa_bridge_num_find(bridge_dev); - if (bridge_num < 0) { + if (!bridge_num) { /* First port that offloads TX forwarding for this bridge */ - bridge_num = find_first_zero_bit(&dsa_fwd_offloading_bridges, - DSA_MAX_NUM_OFFLOADING_BRIDGES); + bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges, + DSA_MAX_NUM_OFFLOADING_BRIDGES, + 1); if (bridge_num >= max) - return -1; + return 0; set_bit(bridge_num, &dsa_fwd_offloading_bridges); } @@ -165,12 +165,13 @@ int dsa_bridge_num_get(const struct net_device *bridge_dev, int max) return bridge_num; } -void dsa_bridge_num_put(const struct net_device *bridge_dev, int bridge_num) +void dsa_bridge_num_put(const struct net_device *bridge_dev, + unsigned int bridge_num) { /* Check if the bridge is still in use, otherwise it is time * to clean it up so we can reuse this bridge_num later. */ - if (dsa_bridge_num_find(bridge_dev) < 0) + if (!dsa_bridge_num_find(bridge_dev)) clear_bit(bridge_num, &dsa_fwd_offloading_bridges); } @@ -1184,7 +1185,6 @@ static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index) dp->ds = ds; dp->index = index; - dp->bridge_num = -1; INIT_LIST_HEAD(&dp->list); list_add_tail(&dp->list, &dst->ports); |