Commit f7157dd1 authored by Sven Eckelmann's avatar Sven Eckelmann Committed by Antonio Quartulli

batman-adv: Convert batadv_tvlv_container to kref

batman-adv uses a self-written reference implementation which is just based
on atomic_t. This is less obvious when reading the code than kref and
therefore increases the change that the reference counting will be missed.
Signed-off-by: default avatarSven Eckelmann <sven@narfation.org>
Signed-off-by: default avatarMarek Lindner <mareklindner@neomailbox.ch>
Signed-off-by: default avatarAntonio Quartulli <a@unstable.cc>
parent 68a6722c
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <linux/ip.h> #include <linux/ip.h>
#include <linux/ipv6.h> #include <linux/ipv6.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/kref.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/lockdep.h> #include <linux/lockdep.h>
#include <linux/module.h> #include <linux/module.h>
...@@ -669,15 +670,26 @@ static struct batadv_tvlv_handler ...@@ -669,15 +670,26 @@ static struct batadv_tvlv_handler
return tvlv_handler; return tvlv_handler;
} }
/**
* batadv_tvlv_container_release - release tvlv from lists and free
* @ref: kref pointer of the tvlv
*/
static void batadv_tvlv_container_release(struct kref *ref)
{
struct batadv_tvlv_container *tvlv;
tvlv = container_of(ref, struct batadv_tvlv_container, refcount);
kfree(tvlv);
}
/** /**
* batadv_tvlv_container_free_ref - decrement the tvlv container refcounter and * batadv_tvlv_container_free_ref - decrement the tvlv container refcounter and
* possibly free it * possibly release it
* @tvlv: the tvlv container to free * @tvlv: the tvlv container to free
*/ */
static void batadv_tvlv_container_free_ref(struct batadv_tvlv_container *tvlv) static void batadv_tvlv_container_free_ref(struct batadv_tvlv_container *tvlv)
{ {
if (atomic_dec_and_test(&tvlv->refcount)) kref_put(&tvlv->refcount, batadv_tvlv_container_release);
kfree(tvlv);
} }
/** /**
...@@ -706,7 +718,7 @@ static struct batadv_tvlv_container ...@@ -706,7 +718,7 @@ static struct batadv_tvlv_container
if (tvlv_tmp->tvlv_hdr.version != version) if (tvlv_tmp->tvlv_hdr.version != version)
continue; continue;
if (!atomic_inc_not_zero(&tvlv_tmp->refcount)) if (!kref_get_unless_zero(&tvlv_tmp->refcount))
continue; continue;
tvlv = tvlv_tmp; tvlv = tvlv_tmp;
...@@ -814,7 +826,7 @@ void batadv_tvlv_container_register(struct batadv_priv *bat_priv, ...@@ -814,7 +826,7 @@ void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len)); memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len));
INIT_HLIST_NODE(&tvlv_new->list); INIT_HLIST_NODE(&tvlv_new->list);
atomic_set(&tvlv_new->refcount, 1); kref_init(&tvlv_new->refcount);
spin_lock_bh(&bat_priv->tvlv.container_list_lock); spin_lock_bh(&bat_priv->tvlv.container_list_lock);
tvlv_old = batadv_tvlv_container_get(bat_priv, type, version); tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
......
...@@ -1266,7 +1266,7 @@ struct batadv_dat_candidate { ...@@ -1266,7 +1266,7 @@ struct batadv_dat_candidate {
struct batadv_tvlv_container { struct batadv_tvlv_container {
struct hlist_node list; struct hlist_node list;
struct batadv_tvlv_hdr tvlv_hdr; struct batadv_tvlv_hdr tvlv_hdr;
atomic_t refcount; struct kref refcount;
}; };
/** /**
......
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