Commit 7c124391 authored by Sven Eckelmann's avatar Sven Eckelmann Committed by Antonio Quartulli

batman-adv: Convert batadv_orig_node 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 161a3be9
...@@ -542,7 +542,7 @@ static void batadv_choose_next_candidate(struct batadv_priv *bat_priv, ...@@ -542,7 +542,7 @@ static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
max_orig_node)) max_orig_node))
continue; continue;
if (!atomic_inc_not_zero(&orig_node->refcount)) if (!kref_get_unless_zero(&orig_node->refcount))
continue; continue;
max = tmp_max; max = tmp_max;
......
...@@ -118,7 +118,7 @@ batadv_gw_get_selected_orig(struct batadv_priv *bat_priv) ...@@ -118,7 +118,7 @@ batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
if (!orig_node) if (!orig_node)
goto unlock; goto unlock;
if (!atomic_inc_not_zero(&orig_node->refcount)) if (!kref_get_unless_zero(&orig_node->refcount))
orig_node = NULL; orig_node = NULL;
unlock: unlock:
...@@ -441,7 +441,7 @@ static void batadv_gw_node_add(struct batadv_priv *bat_priv, ...@@ -441,7 +441,7 @@ static void batadv_gw_node_add(struct batadv_priv *bat_priv,
if (gateway->bandwidth_down == 0) if (gateway->bandwidth_down == 0)
return; return;
if (!atomic_inc_not_zero(&orig_node->refcount)) if (!kref_get_unless_zero(&orig_node->refcount))
return; return;
gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <linux/in.h> #include <linux/in.h>
#include <linux/ip.h> #include <linux/ip.h>
#include <linux/ipv6.h> #include <linux/ipv6.h>
#include <linux/kref.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/lockdep.h> #include <linux/lockdep.h>
#include <linux/netdevice.h> #include <linux/netdevice.h>
...@@ -447,7 +448,7 @@ batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv) ...@@ -447,7 +448,7 @@ batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
hlist_for_each_entry_rcu(tmp_orig_node, hlist_for_each_entry_rcu(tmp_orig_node,
&bat_priv->mcast.want_all_ipv4_list, &bat_priv->mcast.want_all_ipv4_list,
mcast_want_all_ipv4_node) { mcast_want_all_ipv4_node) {
if (!atomic_inc_not_zero(&tmp_orig_node->refcount)) if (!kref_get_unless_zero(&tmp_orig_node->refcount))
continue; continue;
orig_node = tmp_orig_node; orig_node = tmp_orig_node;
...@@ -474,7 +475,7 @@ batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv) ...@@ -474,7 +475,7 @@ batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
hlist_for_each_entry_rcu(tmp_orig_node, hlist_for_each_entry_rcu(tmp_orig_node,
&bat_priv->mcast.want_all_ipv6_list, &bat_priv->mcast.want_all_ipv6_list,
mcast_want_all_ipv6_node) { mcast_want_all_ipv6_node) {
if (!atomic_inc_not_zero(&tmp_orig_node->refcount)) if (!kref_get_unless_zero(&tmp_orig_node->refcount))
continue; continue;
orig_node = tmp_orig_node; orig_node = tmp_orig_node;
...@@ -525,7 +526,7 @@ batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv) ...@@ -525,7 +526,7 @@ batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
hlist_for_each_entry_rcu(tmp_orig_node, hlist_for_each_entry_rcu(tmp_orig_node,
&bat_priv->mcast.want_all_unsnoopables_list, &bat_priv->mcast.want_all_unsnoopables_list,
mcast_want_all_unsnoopables_node) { mcast_want_all_unsnoopables_node) {
if (!atomic_inc_not_zero(&tmp_orig_node->refcount)) if (!kref_get_unless_zero(&tmp_orig_node->refcount))
continue; continue;
orig_node = tmp_orig_node; orig_node = tmp_orig_node;
......
...@@ -858,7 +858,7 @@ static struct batadv_nc_node ...@@ -858,7 +858,7 @@ static struct batadv_nc_node
if (!nc_node) if (!nc_node)
return NULL; return NULL;
if (!atomic_inc_not_zero(&orig_neigh_node->refcount)) if (!kref_get_unless_zero(&orig_neigh_node->refcount))
goto free; goto free;
/* Initialize nc_node */ /* Initialize nc_node */
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "originator.h" #include "originator.h"
#include "main.h" #include "main.h"
#include <linux/atomic.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/etherdevice.h> #include <linux/etherdevice.h>
#include <linux/fs.h> #include <linux/fs.h>
...@@ -775,14 +776,17 @@ static void batadv_orig_node_free_rcu(struct rcu_head *rcu) ...@@ -775,14 +776,17 @@ static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
/** /**
* batadv_orig_node_release - release orig_node from lists and queue for * batadv_orig_node_release - release orig_node from lists and queue for
* free after rcu grace period * free after rcu grace period
* @orig_node: the orig node to free * @ref: kref pointer of the orig_node
*/ */
static void batadv_orig_node_release(struct batadv_orig_node *orig_node) static void batadv_orig_node_release(struct kref *ref)
{ {
struct hlist_node *node_tmp; struct hlist_node *node_tmp;
struct batadv_neigh_node *neigh_node; struct batadv_neigh_node *neigh_node;
struct batadv_orig_node *orig_node;
struct batadv_orig_ifinfo *orig_ifinfo; struct batadv_orig_ifinfo *orig_ifinfo;
orig_node = container_of(ref, struct batadv_orig_node, refcount);
spin_lock_bh(&orig_node->neigh_list_lock); spin_lock_bh(&orig_node->neigh_list_lock);
/* for all neighbors towards this originator ... */ /* for all neighbors towards this originator ... */
...@@ -812,8 +816,7 @@ static void batadv_orig_node_release(struct batadv_orig_node *orig_node) ...@@ -812,8 +816,7 @@ static void batadv_orig_node_release(struct batadv_orig_node *orig_node)
*/ */
void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node) void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
{ {
if (atomic_dec_and_test(&orig_node->refcount)) kref_put(&orig_node->refcount, batadv_orig_node_release);
batadv_orig_node_release(orig_node);
} }
void batadv_originator_free(struct batadv_priv *bat_priv) void batadv_originator_free(struct batadv_priv *bat_priv)
...@@ -885,7 +888,8 @@ struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv, ...@@ -885,7 +888,8 @@ struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
batadv_nc_init_orig(orig_node); batadv_nc_init_orig(orig_node);
/* extra reference for return */ /* extra reference for return */
atomic_set(&orig_node->refcount, 2); kref_init(&orig_node->refcount);
kref_get(&orig_node->refcount);
orig_node->bat_priv = bat_priv; orig_node->bat_priv = bat_priv;
ether_addr_copy(orig_node->orig, addr); ether_addr_copy(orig_node->orig, addr);
......
...@@ -20,10 +20,10 @@ ...@@ -20,10 +20,10 @@
#include "main.h" #include "main.h"
#include <linux/atomic.h>
#include <linux/compiler.h> #include <linux/compiler.h>
#include <linux/if_ether.h> #include <linux/if_ether.h>
#include <linux/jhash.h> #include <linux/jhash.h>
#include <linux/kref.h>
#include <linux/rculist.h> #include <linux/rculist.h>
#include <linux/rcupdate.h> #include <linux/rcupdate.h>
#include <linux/stddef.h> #include <linux/stddef.h>
...@@ -115,7 +115,7 @@ batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data) ...@@ -115,7 +115,7 @@ batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data)
if (!batadv_compare_eth(orig_node, data)) if (!batadv_compare_eth(orig_node, data))
continue; continue;
if (!atomic_inc_not_zero(&orig_node->refcount)) if (!kref_get_unless_zero(&orig_node->refcount))
continue; continue;
orig_node_tmp = orig_node; orig_node_tmp = orig_node;
......
...@@ -1334,7 +1334,7 @@ batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global, ...@@ -1334,7 +1334,7 @@ batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
goto out; goto out;
INIT_HLIST_NODE(&orig_entry->list); INIT_HLIST_NODE(&orig_entry->list);
atomic_inc(&orig_node->refcount); kref_get(&orig_node->refcount);
batadv_tt_global_size_inc(orig_node, tt_global->common.vid); batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
orig_entry->orig_node = orig_node; orig_entry->orig_node = orig_node;
orig_entry->ttvn = ttvn; orig_entry->ttvn = ttvn;
...@@ -2097,7 +2097,7 @@ struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv, ...@@ -2097,7 +2097,7 @@ struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
/* found anything? */ /* found anything? */
if (best_entry) if (best_entry)
orig_node = best_entry->orig_node; orig_node = best_entry->orig_node;
if (orig_node && !atomic_inc_not_zero(&orig_node->refcount)) if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
orig_node = NULL; orig_node = NULL;
rcu_read_unlock(); rcu_read_unlock();
......
...@@ -299,7 +299,7 @@ struct batadv_orig_node { ...@@ -299,7 +299,7 @@ struct batadv_orig_node {
struct batadv_priv *bat_priv; struct batadv_priv *bat_priv;
/* bcast_seqno_lock protects: bcast_bits & last_bcast_seqno */ /* bcast_seqno_lock protects: bcast_bits & last_bcast_seqno */
spinlock_t bcast_seqno_lock; spinlock_t bcast_seqno_lock;
atomic_t refcount; struct kref refcount;
struct rcu_head rcu; struct rcu_head rcu;
#ifdef CONFIG_BATMAN_ADV_NC #ifdef CONFIG_BATMAN_ADV_NC
struct list_head in_coding_list; struct list_head in_coding_list;
......
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