Commit 92dcdf09 authored by Sven Eckelmann's avatar Sven Eckelmann Committed by Antonio Quartulli

batman-adv: Convert batadv_tt_common_entry 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 7c124391
...@@ -142,7 +142,7 @@ batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr, ...@@ -142,7 +142,7 @@ batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
if (tt->vid != vid) if (tt->vid != vid)
continue; continue;
if (!atomic_inc_not_zero(&tt->refcount)) if (!kref_get_unless_zero(&tt->refcount))
continue; continue;
tt_tmp = tt; tt_tmp = tt;
...@@ -203,25 +203,59 @@ batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr, ...@@ -203,25 +203,59 @@ batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
return tt_global_entry; return tt_global_entry;
} }
/**
* batadv_tt_local_entry_release - release tt_local_entry from lists and queue
* for free after rcu grace period
* @ref: kref pointer of the nc_node
*/
static void batadv_tt_local_entry_release(struct kref *ref)
{
struct batadv_tt_local_entry *tt_local_entry;
tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
common.refcount);
kfree_rcu(tt_local_entry, common.rcu);
}
/**
* batadv_tt_local_entry_free_ref - decrement the tt_local_entry refcounter and
* possibly release it
* @tt_local_entry: tt_local_entry to be free'd
*/
static void static void
batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry) batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
{ {
if (atomic_dec_and_test(&tt_local_entry->common.refcount)) kref_put(&tt_local_entry->common.refcount,
kfree_rcu(tt_local_entry, common.rcu); batadv_tt_local_entry_release);
} }
/** /**
* batadv_tt_global_entry_free_ref - decrement the refcounter for a * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
* tt_global_entry and possibly free it * for free after rcu grace period
* @tt_global_entry: the object to free * @ref: kref pointer of the nc_node
*/
static void batadv_tt_global_entry_release(struct kref *ref)
{
struct batadv_tt_global_entry *tt_global_entry;
tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
common.refcount);
batadv_tt_global_del_orig_list(tt_global_entry);
kfree_rcu(tt_global_entry, common.rcu);
}
/**
* batadv_tt_global_entry_free_ref - decrement the tt_global_entry refcounter
* and possibly release it
* @tt_global_entry: tt_global_entry to be free'd
*/ */
static void static void
batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry) batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
{ {
if (atomic_dec_and_test(&tt_global_entry->common.refcount)) { kref_put(&tt_global_entry->common.refcount,
batadv_tt_global_del_orig_list(tt_global_entry); batadv_tt_global_entry_release);
kfree_rcu(tt_global_entry, common.rcu);
}
} }
/** /**
...@@ -633,7 +667,8 @@ bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr, ...@@ -633,7 +667,8 @@ bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
tt_local->common.vid = vid; tt_local->common.vid = vid;
if (batadv_is_wifi_netdev(in_dev)) if (batadv_is_wifi_netdev(in_dev))
tt_local->common.flags |= BATADV_TT_CLIENT_WIFI; tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
atomic_set(&tt_local->common.refcount, 2); kref_init(&tt_local->common.refcount);
kref_get(&tt_local->common.refcount);
tt_local->last_seen = jiffies; tt_local->last_seen = jiffies;
tt_local->common.added_at = tt_local->last_seen; tt_local->common.added_at = tt_local->last_seen;
...@@ -1415,7 +1450,8 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv, ...@@ -1415,7 +1450,8 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
*/ */
if (flags & BATADV_TT_CLIENT_ROAM) if (flags & BATADV_TT_CLIENT_ROAM)
tt_global_entry->roam_at = jiffies; tt_global_entry->roam_at = jiffies;
atomic_set(&common->refcount, 2); kref_init(&common->refcount);
kref_get(&common->refcount);
common->added_at = jiffies; common->added_at = jiffies;
INIT_HLIST_HEAD(&tt_global_entry->orig_list); INIT_HLIST_HEAD(&tt_global_entry->orig_list);
......
...@@ -972,7 +972,7 @@ struct batadv_tt_common_entry { ...@@ -972,7 +972,7 @@ struct batadv_tt_common_entry {
struct hlist_node hash_entry; struct hlist_node hash_entry;
u16 flags; u16 flags;
unsigned long added_at; unsigned long added_at;
atomic_t refcount; struct kref refcount;
struct rcu_head rcu; struct rcu_head rcu;
}; };
......
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