Commit f7157dd135012d9e9b4d74a9f8b2426ea92131f8

Authored by Sven Eckelmann
Committed by Antonio Quartulli
1 parent 68a6722cc4

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: Sven Eckelmann <sven@narfation.org>
Signed-off-by: Marek Lindner <mareklindner@neomailbox.ch>
Signed-off-by: Antonio Quartulli <a@unstable.cc>

Showing 2 changed files with 18 additions and 6 deletions Side-by-side Diff

net/batman-adv/main.c
... ... @@ -29,6 +29,7 @@
29 29 #include <linux/ip.h>
30 30 #include <linux/ipv6.h>
31 31 #include <linux/kernel.h>
  32 +#include <linux/kref.h>
32 33 #include <linux/list.h>
33 34 #include <linux/lockdep.h>
34 35 #include <linux/module.h>
35 36  
36 37  
... ... @@ -670,14 +671,25 @@
670 671 }
671 672  
672 673 /**
  674 + * batadv_tvlv_container_release - release tvlv from lists and free
  675 + * @ref: kref pointer of the tvlv
  676 + */
  677 +static void batadv_tvlv_container_release(struct kref *ref)
  678 +{
  679 + struct batadv_tvlv_container *tvlv;
  680 +
  681 + tvlv = container_of(ref, struct batadv_tvlv_container, refcount);
  682 + kfree(tvlv);
  683 +}
  684 +
  685 +/**
673 686 * batadv_tvlv_container_free_ref - decrement the tvlv container refcounter and
674   - * possibly free it
  687 + * possibly release it
675 688 * @tvlv: the tvlv container to free
676 689 */
677 690 static void batadv_tvlv_container_free_ref(struct batadv_tvlv_container *tvlv)
678 691 {
679   - if (atomic_dec_and_test(&tvlv->refcount))
680   - kfree(tvlv);
  692 + kref_put(&tvlv->refcount, batadv_tvlv_container_release);
681 693 }
682 694  
683 695 /**
... ... @@ -706,7 +718,7 @@
706 718 if (tvlv_tmp->tvlv_hdr.version != version)
707 719 continue;
708 720  
709   - if (!atomic_inc_not_zero(&tvlv_tmp->refcount))
  721 + if (!kref_get_unless_zero(&tvlv_tmp->refcount))
710 722 continue;
711 723  
712 724 tvlv = tvlv_tmp;
... ... @@ -814,7 +826,7 @@
814 826  
815 827 memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len));
816 828 INIT_HLIST_NODE(&tvlv_new->list);
817   - atomic_set(&tvlv_new->refcount, 1);
  829 + kref_init(&tvlv_new->refcount);
818 830  
819 831 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
820 832 tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
net/batman-adv/types.h
... ... @@ -1266,7 +1266,7 @@
1266 1266 struct batadv_tvlv_container {
1267 1267 struct hlist_node list;
1268 1268 struct batadv_tvlv_hdr tvlv_hdr;
1269   - atomic_t refcount;
  1269 + struct kref refcount;
1270 1270 };
1271 1271  
1272 1272 /**