public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Subject: [B.A.T.M.A.N.] [PATCH v4 29/31] batman-adv: Convert batadv_orig_node to kref
Date: Tue,  5 Jan 2016 12:06:44 +0100	[thread overview]
Message-ID: <1451992006-13191-29-git-send-email-sven@narfation.org> (raw)
In-Reply-To: <1451992006-13191-1-git-send-email-sven@narfation.org>

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>
---
v4:
 - fix function names in commit messages
 - fix double whitespace in batadv_tt_orig_list_entry_release kerneldoc
 - add extra patch for batadv_claim_free_ref kerneldoc fix
 - change the phrase "free it" in all *_free_ref/*_put functions to "release it"
v3:
 - update copyright year
v2:
 - split patchset into fixes and kref migration to make it easier when the
   decision is made where each patch will be applied

 net/batman-adv/distributed-arp-table.c |  2 +-
 net/batman-adv/gateway_client.c        |  4 ++--
 net/batman-adv/multicast.c             |  7 ++++---
 net/batman-adv/network-coding.c        |  2 +-
 net/batman-adv/originator.c            | 14 +++++++++-----
 net/batman-adv/originator.h            |  4 ++--
 net/batman-adv/translation-table.c     |  4 ++--
 net/batman-adv/types.h                 |  2 +-
 8 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/net/batman-adv/distributed-arp-table.c b/net/batman-adv/distributed-arp-table.c
index 2ff969a..f9e7bd2 100644
--- a/net/batman-adv/distributed-arp-table.c
+++ b/net/batman-adv/distributed-arp-table.c
@@ -542,7 +542,7 @@ static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
 							  max_orig_node))
 				continue;
 
-			if (!atomic_inc_not_zero(&orig_node->refcount))
+			if (!kref_get_unless_zero(&orig_node->refcount))
 				continue;
 
 			max = tmp_max;
diff --git a/net/batman-adv/gateway_client.c b/net/batman-adv/gateway_client.c
index bf5a6ef..17858f2 100644
--- a/net/batman-adv/gateway_client.c
+++ b/net/batman-adv/gateway_client.c
@@ -118,7 +118,7 @@ batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
 	if (!orig_node)
 		goto unlock;
 
-	if (!atomic_inc_not_zero(&orig_node->refcount))
+	if (!kref_get_unless_zero(&orig_node->refcount))
 		orig_node = NULL;
 
 unlock:
@@ -441,7 +441,7 @@ static void batadv_gw_node_add(struct batadv_priv *bat_priv,
 	if (gateway->bandwidth_down == 0)
 		return;
 
-	if (!atomic_inc_not_zero(&orig_node->refcount))
+	if (!kref_get_unless_zero(&orig_node->refcount))
 		return;
 
 	gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
diff --git a/net/batman-adv/multicast.c b/net/batman-adv/multicast.c
index 8abf488..288844f 100644
--- a/net/batman-adv/multicast.c
+++ b/net/batman-adv/multicast.c
@@ -30,6 +30,7 @@
 #include <linux/in.h>
 #include <linux/ip.h>
 #include <linux/ipv6.h>
+#include <linux/kref.h>
 #include <linux/list.h>
 #include <linux/lockdep.h>
 #include <linux/netdevice.h>
@@ -447,7 +448,7 @@ batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
 	hlist_for_each_entry_rcu(tmp_orig_node,
 				 &bat_priv->mcast.want_all_ipv4_list,
 				 mcast_want_all_ipv4_node) {
-		if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
+		if (!kref_get_unless_zero(&tmp_orig_node->refcount))
 			continue;
 
 		orig_node = tmp_orig_node;
@@ -474,7 +475,7 @@ batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
 	hlist_for_each_entry_rcu(tmp_orig_node,
 				 &bat_priv->mcast.want_all_ipv6_list,
 				 mcast_want_all_ipv6_node) {
-		if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
+		if (!kref_get_unless_zero(&tmp_orig_node->refcount))
 			continue;
 
 		orig_node = tmp_orig_node;
@@ -525,7 +526,7 @@ batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
 	hlist_for_each_entry_rcu(tmp_orig_node,
 				 &bat_priv->mcast.want_all_unsnoopables_list,
 				 mcast_want_all_unsnoopables_node) {
-		if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
+		if (!kref_get_unless_zero(&tmp_orig_node->refcount))
 			continue;
 
 		orig_node = tmp_orig_node;
diff --git a/net/batman-adv/network-coding.c b/net/batman-adv/network-coding.c
index 0ad2cc1..42206c4 100644
--- a/net/batman-adv/network-coding.c
+++ b/net/batman-adv/network-coding.c
@@ -858,7 +858,7 @@ static struct batadv_nc_node
 	if (!nc_node)
 		return NULL;
 
-	if (!atomic_inc_not_zero(&orig_neigh_node->refcount))
+	if (!kref_get_unless_zero(&orig_neigh_node->refcount))
 		goto free;
 
 	/* Initialize nc_node */
diff --git a/net/batman-adv/originator.c b/net/batman-adv/originator.c
index 4e7a090..217399f 100644
--- a/net/batman-adv/originator.c
+++ b/net/batman-adv/originator.c
@@ -18,6 +18,7 @@
 #include "originator.h"
 #include "main.h"
 
+#include <linux/atomic.h>
 #include <linux/errno.h>
 #include <linux/etherdevice.h>
 #include <linux/fs.h>
@@ -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
  *  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 batadv_neigh_node *neigh_node;
+	struct batadv_orig_node *orig_node;
 	struct batadv_orig_ifinfo *orig_ifinfo;
 
+	orig_node = container_of(ref, struct batadv_orig_node, refcount);
+
 	spin_lock_bh(&orig_node->neigh_list_lock);
 
 	/* for all neighbors towards this originator ... */
@@ -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)
 {
-	if (atomic_dec_and_test(&orig_node->refcount))
-		batadv_orig_node_release(orig_node);
+	kref_put(&orig_node->refcount, batadv_orig_node_release);
 }
 
 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,
 	batadv_nc_init_orig(orig_node);
 
 	/* 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;
 	ether_addr_copy(orig_node->orig, addr);
diff --git a/net/batman-adv/originator.h b/net/batman-adv/originator.h
index cf07304..6ad7bf3 100644
--- a/net/batman-adv/originator.h
+++ b/net/batman-adv/originator.h
@@ -20,10 +20,10 @@
 
 #include "main.h"
 
-#include <linux/atomic.h>
 #include <linux/compiler.h>
 #include <linux/if_ether.h>
 #include <linux/jhash.h>
+#include <linux/kref.h>
 #include <linux/rculist.h>
 #include <linux/rcupdate.h>
 #include <linux/stddef.h>
@@ -115,7 +115,7 @@ batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data)
 		if (!batadv_compare_eth(orig_node, data))
 			continue;
 
-		if (!atomic_inc_not_zero(&orig_node->refcount))
+		if (!kref_get_unless_zero(&orig_node->refcount))
 			continue;
 
 		orig_node_tmp = orig_node;
diff --git a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c
index 0f61b03..19c1d66 100644
--- a/net/batman-adv/translation-table.c
+++ b/net/batman-adv/translation-table.c
@@ -1334,7 +1334,7 @@ batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
 		goto out;
 
 	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);
 	orig_entry->orig_node = orig_node;
 	orig_entry->ttvn = ttvn;
@@ -2097,7 +2097,7 @@ struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
 	/* found anything? */
 	if (best_entry)
 		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;
 	rcu_read_unlock();
 
diff --git a/net/batman-adv/types.h b/net/batman-adv/types.h
index 441ebd9..c8c7ab8 100644
--- a/net/batman-adv/types.h
+++ b/net/batman-adv/types.h
@@ -299,7 +299,7 @@ struct batadv_orig_node {
 	struct batadv_priv *bat_priv;
 	/* bcast_seqno_lock protects: bcast_bits & last_bcast_seqno */
 	spinlock_t bcast_seqno_lock;
-	atomic_t refcount;
+	struct kref refcount;
 	struct rcu_head rcu;
 #ifdef CONFIG_BATMAN_ADV_NC
 	struct list_head in_coding_list;
-- 
2.6.4


  parent reply	other threads:[~2016-01-05 11:06 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-05 11:06 [B.A.T.M.A.N.] [PATCH v4 01/31] batman-adv: Fix kernel-doc for batadv_claim_free_ref Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 02/31] batman-adv: Fix list removal of batadv_hardif_neigh_node Sven Eckelmann
2016-01-06 16:04   ` Marek Lindner
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 03/31] batman-adv: Avoid recursive call_rcu for batadv_bla_claim Sven Eckelmann
2016-01-06 16:12   ` Marek Lindner
2016-01-14 14:24   ` Simon Wunderlich
2016-01-14 14:28   ` [B.A.T.M.A.N.] [PATCH v5 " Sven Eckelmann
2016-01-16  4:49     ` Marek Lindner
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 04/31] batman-adv: Avoid recursive call_rcu for batadv_nc_node Sven Eckelmann
2016-01-16  4:54   ` Marek Lindner
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 05/31] batman-adv: Drop immediate orig_node free function Sven Eckelmann
2016-01-11 15:50   ` Antonio Quartulli
2016-01-16  4:58   ` Marek Lindner
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 06/31] batman-adv: Drop immediate batadv_orig_ifinfo " Sven Eckelmann
2016-01-16  5:11   ` Marek Lindner
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 07/31] batman-adv: Drop immediate batadv_neigh_node " Sven Eckelmann
2016-01-16  5:17   ` Marek Lindner
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 08/31] batman-adv: Drop immediate batadv_hardif_neigh_node " Sven Eckelmann
2016-01-16  5:23   ` Marek Lindner
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 09/31] batman-adv: Drop immediate neigh_ifinfo " Sven Eckelmann
2016-01-16  5:25   ` Marek Lindner
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 10/31] batman-adv: Drop immediate batadv_hard_iface " Sven Eckelmann
2016-01-16  5:30   ` Marek Lindner
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 11/31] batman-adv: Drop reference to netdevice on last reference Sven Eckelmann
2016-01-11 15:58   ` Antonio Quartulli
2016-01-11 16:05     ` Sven Eckelmann
2016-01-11 16:04       ` Antonio Quartulli
2016-01-16  5:33   ` Marek Lindner
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 12/31] batman-adv: Add compatibility code for kref_get_unless_zero Sven Eckelmann
2016-01-07  3:39   ` Linus Lüssing
2016-01-07  8:32     ` Sven Eckelmann
2016-01-07  8:53   ` [B.A.T.M.A.N.] [PATCH v5 " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 13/31] batman-adv: Convert batadv_hardif_neigh_node to kref Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 14/31] batman-adv: Convert batadv_gw_node " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 15/31] batman-adv: Convert batadv_softif_vlan " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 16/31] batman-adv: Convert batadv_bla_backbone_gw " Sven Eckelmann
2016-01-14 14:26   ` Simon Wunderlich
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 17/31] batman-adv: Convert batadv_bla_claim " Sven Eckelmann
2016-01-14 14:25   ` Simon Wunderlich
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 18/31] batman-adv: Convert batadv_nc_node " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 19/31] batman-adv: Convert batadv_nc_path " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 20/31] batman-adv: Convert batadv_dat_entry " Sven Eckelmann
2016-01-11 16:07   ` Antonio Quartulli
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 21/31] batman-adv: Convert batadv_tvlv_container " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 22/31] batman-adv: Convert batadv_tvlv_handler " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 23/31] batman-adv: Convert batadv_tt_orig_list_entry " Sven Eckelmann
2016-01-11 16:12   ` Antonio Quartulli
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 24/31] batman-adv: Convert batadv_neigh_ifinfo " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 25/31] batman-adv: Convert batadv_orig_ifinfo " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 26/31] batman-adv: Convert batadv_neigh_node " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 27/31] batman-adv: Convert batadv_hard_iface " Sven Eckelmann
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 28/31] batman-adv: Convert batadv_orig_node_vlan " Sven Eckelmann
2016-01-05 11:06 ` Sven Eckelmann [this message]
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 30/31] batman-adv: Convert batadv_tt_common_entry " Sven Eckelmann
2016-01-11 16:16   ` Antonio Quartulli
2016-01-05 11:06 ` [B.A.T.M.A.N.] [PATCH v4 31/31] batman-adv: Rename *_free_ref function to *_put Sven Eckelmann
2016-01-06 15:53 ` [B.A.T.M.A.N.] [PATCH v4 01/31] batman-adv: Fix kernel-doc for batadv_claim_free_ref Marek Lindner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1451992006-13191-29-git-send-email-sven@narfation.org \
    --to=sven@narfation.org \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox