Commit 6a1ba875 authored by Yevhen Orlov's avatar Yevhen Orlov Committed by Jakub Kicinski

net: marvell: prestera: Refactor get/put VR functions

* Use refcount, instead of uint
* Increment/decrement recount inside get/put
* Fix error path in __prestera_vr_create. Remove unnecessary kfree.
* Make __prestera_vr_destroy symmetric to "create"

Fixes: bca5859b ("net: marvell: prestera: add hardware router objects accounting")
Signed-off-by: default avatarYevhen Orlov <yevhen.orlov@plvision.eu>
Link: https://lore.kernel.org/r/20220111011014.4418-1-yevhen.orlov@plvision.euSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 9c0c2c7a
...@@ -47,13 +47,8 @@ static struct prestera_vr *__prestera_vr_create(struct prestera_switch *sw, ...@@ -47,13 +47,8 @@ static struct prestera_vr *__prestera_vr_create(struct prestera_switch *sw,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
{ {
struct prestera_vr *vr; struct prestera_vr *vr;
u16 hw_vr_id;
int err; int err;
err = prestera_hw_vr_create(sw, &hw_vr_id);
if (err)
return ERR_PTR(-ENOMEM);
vr = kzalloc(sizeof(*vr), GFP_KERNEL); vr = kzalloc(sizeof(*vr), GFP_KERNEL);
if (!vr) { if (!vr) {
err = -ENOMEM; err = -ENOMEM;
...@@ -61,23 +56,26 @@ static struct prestera_vr *__prestera_vr_create(struct prestera_switch *sw, ...@@ -61,23 +56,26 @@ static struct prestera_vr *__prestera_vr_create(struct prestera_switch *sw,
} }
vr->tb_id = tb_id; vr->tb_id = tb_id;
vr->hw_vr_id = hw_vr_id;
err = prestera_hw_vr_create(sw, &vr->hw_vr_id);
if (err)
goto err_hw_create;
list_add(&vr->router_node, &sw->router->vr_list); list_add(&vr->router_node, &sw->router->vr_list);
return vr; return vr;
err_alloc_vr: err_hw_create:
prestera_hw_vr_delete(sw, hw_vr_id);
kfree(vr); kfree(vr);
err_alloc_vr:
return ERR_PTR(err); return ERR_PTR(err);
} }
static void __prestera_vr_destroy(struct prestera_switch *sw, static void __prestera_vr_destroy(struct prestera_switch *sw,
struct prestera_vr *vr) struct prestera_vr *vr)
{ {
prestera_hw_vr_delete(sw, vr->hw_vr_id);
list_del(&vr->router_node); list_del(&vr->router_node);
prestera_hw_vr_delete(sw, vr->hw_vr_id);
kfree(vr); kfree(vr);
} }
...@@ -87,17 +85,22 @@ static struct prestera_vr *prestera_vr_get(struct prestera_switch *sw, u32 tb_id ...@@ -87,17 +85,22 @@ static struct prestera_vr *prestera_vr_get(struct prestera_switch *sw, u32 tb_id
struct prestera_vr *vr; struct prestera_vr *vr;
vr = __prestera_vr_find(sw, tb_id); vr = __prestera_vr_find(sw, tb_id);
if (!vr) if (vr) {
refcount_inc(&vr->refcount);
} else {
vr = __prestera_vr_create(sw, tb_id, extack); vr = __prestera_vr_create(sw, tb_id, extack);
if (IS_ERR(vr)) if (IS_ERR(vr))
return ERR_CAST(vr); return ERR_CAST(vr);
refcount_set(&vr->refcount, 1);
}
return vr; return vr;
} }
static void prestera_vr_put(struct prestera_switch *sw, struct prestera_vr *vr) static void prestera_vr_put(struct prestera_switch *sw, struct prestera_vr *vr)
{ {
if (!vr->ref_cnt) if (refcount_dec_and_test(&vr->refcount))
__prestera_vr_destroy(sw, vr); __prestera_vr_destroy(sw, vr);
} }
...@@ -158,7 +161,6 @@ void prestera_rif_entry_destroy(struct prestera_switch *sw, ...@@ -158,7 +161,6 @@ void prestera_rif_entry_destroy(struct prestera_switch *sw,
iface.vr_id = e->vr->hw_vr_id; iface.vr_id = e->vr->hw_vr_id;
prestera_hw_rif_delete(sw, e->hw_id, &iface); prestera_hw_rif_delete(sw, e->hw_id, &iface);
e->vr->ref_cnt--;
prestera_vr_put(sw, e->vr); prestera_vr_put(sw, e->vr);
kfree(e); kfree(e);
} }
...@@ -183,7 +185,6 @@ prestera_rif_entry_create(struct prestera_switch *sw, ...@@ -183,7 +185,6 @@ prestera_rif_entry_create(struct prestera_switch *sw,
if (IS_ERR(e->vr)) if (IS_ERR(e->vr))
goto err_vr_get; goto err_vr_get;
e->vr->ref_cnt++;
memcpy(&e->addr, addr, sizeof(e->addr)); memcpy(&e->addr, addr, sizeof(e->addr));
/* HW */ /* HW */
...@@ -198,7 +199,6 @@ prestera_rif_entry_create(struct prestera_switch *sw, ...@@ -198,7 +199,6 @@ prestera_rif_entry_create(struct prestera_switch *sw,
return e; return e;
err_hw_create: err_hw_create:
e->vr->ref_cnt--;
prestera_vr_put(sw, e->vr); prestera_vr_put(sw, e->vr);
err_vr_get: err_vr_get:
err_key_copy: err_key_copy:
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
struct prestera_vr { struct prestera_vr {
struct list_head router_node; struct list_head router_node;
unsigned int ref_cnt; refcount_t refcount;
u32 tb_id; /* key (kernel fib table id) */ u32 tb_id; /* key (kernel fib table id) */
u16 hw_vr_id; /* virtual router ID */ u16 hw_vr_id; /* virtual router ID */
u8 __pad[2]; u8 __pad[2];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment