Skip to content

Commit 1ca28ec

Browse files
viviendavem330
authored andcommitted
net: dsa: provide a find or new tree helper
Rename dsa_get_dst to dsa_tree_find since it doesn't increment the reference counter, rename dsa_add_dst to dsa_tree_alloc for symmetry with dsa_tree_free, and provide a convenient dsa_tree_touch function to find or allocate a new tree. Signed-off-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6525410 commit 1ca28ec

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

net/dsa/dsa2.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,35 @@
2121

2222
#include "dsa_priv.h"
2323

24-
static LIST_HEAD(dsa_switch_trees);
24+
static LIST_HEAD(dsa_tree_list);
2525
static DEFINE_MUTEX(dsa2_mutex);
2626

2727
static const struct devlink_ops dsa_devlink_ops = {
2828
};
2929

30-
static struct dsa_switch_tree *dsa_get_dst(unsigned int index)
30+
static struct dsa_switch_tree *dsa_tree_find(int index)
3131
{
3232
struct dsa_switch_tree *dst;
3333

34-
list_for_each_entry(dst, &dsa_switch_trees, list)
34+
list_for_each_entry(dst, &dsa_tree_list, list)
3535
if (dst->index == index)
3636
return dst;
3737

3838
return NULL;
3939
}
4040

41-
static struct dsa_switch_tree *dsa_add_dst(unsigned int index)
41+
static struct dsa_switch_tree *dsa_tree_alloc(int index)
4242
{
4343
struct dsa_switch_tree *dst;
4444

4545
dst = kzalloc(sizeof(*dst), GFP_KERNEL);
4646
if (!dst)
4747
return NULL;
48+
4849
dst->index = index;
50+
4951
INIT_LIST_HEAD(&dst->list);
50-
list_add_tail(&dsa_switch_trees, &dst->list);
52+
list_add_tail(&dsa_tree_list, &dst->list);
5153

5254
/* Initialize the reference counter to the number of switches, not 1 */
5355
kref_init(&dst->refcount);
@@ -62,6 +64,17 @@ static void dsa_tree_free(struct dsa_switch_tree *dst)
6264
kfree(dst);
6365
}
6466

67+
static struct dsa_switch_tree *dsa_tree_touch(int index)
68+
{
69+
struct dsa_switch_tree *dst;
70+
71+
dst = dsa_tree_find(index);
72+
if (!dst)
73+
dst = dsa_tree_alloc(index);
74+
75+
return dst;
76+
}
77+
6578
static void dsa_tree_get(struct dsa_switch_tree *dst)
6679
{
6780
kref_get(&dst->refcount);
@@ -745,12 +758,9 @@ static int _dsa_register_switch(struct dsa_switch *ds)
745758
return err;
746759
}
747760

748-
dst = dsa_get_dst(tree);
749-
if (!dst) {
750-
dst = dsa_add_dst(tree);
751-
if (!dst)
752-
return -ENOMEM;
753-
}
761+
dst = dsa_tree_touch(tree);
762+
if (!dst)
763+
return -ENOMEM;
754764

755765
if (dst->ds[index])
756766
return -EBUSY;

0 commit comments

Comments
 (0)