Commit 32836f56f88c280d39fc302bcc0e0e3ed6bb412e

Authored by Sven Eckelmann
Committed by Antonio Quartulli
1 parent f7157dd135

batman-adv: Convert batadv_tvlv_handler 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 19 additions and 7 deletions Side-by-side Diff

net/batman-adv/main.c
... ... @@ -625,15 +625,27 @@
625 625 }
626 626  
627 627 /**
628   - * batadv_tvlv_handler_free_ref - decrement the tvlv handler refcounter and
629   - * possibly free it
  628 + * batadv_tvlv_handler_release - release tvlv handler from lists and queue for
  629 + * free after rcu grace period
  630 + * @ref: kref pointer of the tvlv
  631 + */
  632 +static void batadv_tvlv_handler_release(struct kref *ref)
  633 +{
  634 + struct batadv_tvlv_handler *tvlv_handler;
  635 +
  636 + tvlv_handler = container_of(ref, struct batadv_tvlv_handler, refcount);
  637 + kfree_rcu(tvlv_handler, rcu);
  638 +}
  639 +
  640 +/**
  641 + * batadv_tvlv_handler_free_ref - decrement the tvlv container refcounter and
  642 + * possibly release it
630 643 * @tvlv_handler: the tvlv handler to free
631 644 */
632 645 static void
633 646 batadv_tvlv_handler_free_ref(struct batadv_tvlv_handler *tvlv_handler)
634 647 {
635   - if (atomic_dec_and_test(&tvlv_handler->refcount))
636   - kfree_rcu(tvlv_handler, rcu);
  648 + kref_put(&tvlv_handler->refcount, batadv_tvlv_handler_release);
637 649 }
638 650  
639 651 /**
... ... @@ -659,7 +671,7 @@
659 671 if (tvlv_handler_tmp->version != version)
660 672 continue;
661 673  
662   - if (!atomic_inc_not_zero(&tvlv_handler_tmp->refcount))
  674 + if (!kref_get_unless_zero(&tvlv_handler_tmp->refcount))
663 675 continue;
664 676  
665 677 tvlv_handler = tvlv_handler_tmp;
... ... @@ -1112,7 +1124,7 @@
1112 1124 tvlv_handler->type = type;
1113 1125 tvlv_handler->version = version;
1114 1126 tvlv_handler->flags = flags;
1115   - atomic_set(&tvlv_handler->refcount, 1);
  1127 + kref_init(&tvlv_handler->refcount);
1116 1128 INIT_HLIST_NODE(&tvlv_handler->list);
1117 1129  
1118 1130 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
net/batman-adv/types.h
... ... @@ -1293,7 +1293,7 @@
1293 1293 u8 type;
1294 1294 u8 version;
1295 1295 u8 flags;
1296   - atomic_t refcount;
  1296 + struct kref refcount;
1297 1297 struct rcu_head rcu;
1298 1298 };
1299 1299