b.a.t.m.a.n.lists.open-mesh.org archive mirror
 help / color / mirror / Atom feed
From: Antonio Quartulli <a@unstable.cc>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, b.a.t.m.a.n@lists.open-mesh.org,
	Antonio Quartulli <a@unstable.cc>,
	Marek Lindner <mareklindner@neomailbox.ch>
Subject: [B.A.T.M.A.N.] [PATCH 05/21] batman-adv: Convert batadv_gw_node to kref
Date: Wed, 10 Feb 2016 23:57:11 +0800	[thread overview]
Message-ID: <1455119847-5862-6-git-send-email-a@unstable.cc> (raw)
In-Reply-To: <1455119847-5862-1-git-send-email-a@unstable.cc>

From: Sven Eckelmann <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>
Signed-off-by: Marek Lindner <mareklindner@neomailbox.ch>
Signed-off-by: Antonio Quartulli <a@unstable.cc>
---
 net/batman-adv/gateway_client.c | 40 +++++++++++++++++++++++++++++-----------
 net/batman-adv/types.h          |  2 +-
 2 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/net/batman-adv/gateway_client.c b/net/batman-adv/gateway_client.c
index 5950974de7b1..1f3847c61db8 100644
--- a/net/batman-adv/gateway_client.c
+++ b/net/batman-adv/gateway_client.c
@@ -28,6 +28,7 @@
 #include <linux/ip.h>
 #include <linux/ipv6.h>
 #include <linux/kernel.h>
+#include <linux/kref.h>
 #include <linux/list.h>
 #include <linux/netdevice.h>
 #include <linux/rculist.h>
@@ -59,12 +60,29 @@
  */
 #define BATADV_DHCP_CHADDR_OFFSET	28
 
+/**
+ * batadv_gw_node_release - release gw_node from lists and queue for free after
+ *  rcu grace period
+ * @ref: kref pointer of the gw_node
+ */
+static void batadv_gw_node_release(struct kref *ref)
+{
+	struct batadv_gw_node *gw_node;
+
+	gw_node = container_of(ref, struct batadv_gw_node, refcount);
+
+	batadv_orig_node_free_ref(gw_node->orig_node);
+	kfree_rcu(gw_node, rcu);
+}
+
+/**
+ * batadv_gw_node_free_ref - decrement the gw_node refcounter and possibly
+ *  release it
+ * @gw_node: gateway node to free
+ */
 static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node)
 {
-	if (atomic_dec_and_test(&gw_node->refcount)) {
-		batadv_orig_node_free_ref(gw_node->orig_node);
-		kfree_rcu(gw_node, rcu);
-	}
+	kref_put(&gw_node->refcount, batadv_gw_node_release);
 }
 
 static struct batadv_gw_node *
@@ -77,7 +95,7 @@ batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
 	if (!gw_node)
 		goto out;
 
-	if (!atomic_inc_not_zero(&gw_node->refcount))
+	if (!kref_get_unless_zero(&gw_node->refcount))
 		gw_node = NULL;
 
 out:
@@ -118,7 +136,7 @@ static void batadv_gw_select(struct batadv_priv *bat_priv,
 
 	spin_lock_bh(&bat_priv->gw.list_lock);
 
-	if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
+	if (new_gw_node && !kref_get_unless_zero(&new_gw_node->refcount))
 		new_gw_node = NULL;
 
 	curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
@@ -170,7 +188,7 @@ batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
 		if (!router_ifinfo)
 			goto next;
 
-		if (!atomic_inc_not_zero(&gw_node->refcount))
+		if (!kref_get_unless_zero(&gw_node->refcount))
 			goto next;
 
 		tq_avg = router_ifinfo->bat_iv.tq_avg;
@@ -188,7 +206,7 @@ batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
 				if (curr_gw)
 					batadv_gw_node_free_ref(curr_gw);
 				curr_gw = gw_node;
-				atomic_inc(&curr_gw->refcount);
+				kref_get(&curr_gw->refcount);
 			}
 			break;
 
@@ -203,7 +221,7 @@ batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
 				if (curr_gw)
 					batadv_gw_node_free_ref(curr_gw);
 				curr_gw = gw_node;
-				atomic_inc(&curr_gw->refcount);
+				kref_get(&curr_gw->refcount);
 			}
 			break;
 		}
@@ -436,7 +454,7 @@ static void batadv_gw_node_add(struct batadv_priv *bat_priv,
 	gw_node->orig_node = orig_node;
 	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
 	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
-	atomic_set(&gw_node->refcount, 1);
+	kref_init(&gw_node->refcount);
 
 	spin_lock_bh(&bat_priv->gw.list_lock);
 	hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
@@ -469,7 +487,7 @@ batadv_gw_node_get(struct batadv_priv *bat_priv,
 		if (gw_node_tmp->orig_node != orig_node)
 			continue;
 
-		if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
+		if (!kref_get_unless_zero(&gw_node_tmp->refcount))
 			continue;
 
 		gw_node = gw_node_tmp;
diff --git a/net/batman-adv/types.h b/net/batman-adv/types.h
index ace7900347d4..06c38881e382 100644
--- a/net/batman-adv/types.h
+++ b/net/batman-adv/types.h
@@ -342,7 +342,7 @@ struct batadv_gw_node {
 	struct batadv_orig_node *orig_node;
 	u32 bandwidth_down;
 	u32 bandwidth_up;
-	atomic_t refcount;
+	struct kref refcount;
 	struct rcu_head rcu;
 };
 
-- 
2.7.1


  parent reply	other threads:[~2016-02-10 15:57 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-10 15:57 [B.A.T.M.A.N.] pull request [net-next]: batman-adv 20160210 Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 01/21] batman-adv: Drop reference to netdevice on last reference Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 02/21] batman-adv: add seqno maximum age and protection start flag parameters Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 03/21] batman-adv: Add lockdep assert for container_list_lock Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 04/21] batman-adv: Convert batadv_hardif_neigh_node to kref Antonio Quartulli
2016-02-10 15:57 ` Antonio Quartulli [this message]
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 06/21] batman-adv: Convert batadv_softif_vlan " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 07/21] batman-adv: Convert batadv_bla_backbone_gw " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 08/21] batman-adv: Convert batadv_bla_claim " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 09/21] batman-adv: Convert batadv_nc_node " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 10/21] batman-adv: Convert batadv_nc_path " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 11/21] batman-adv: Convert batadv_dat_entry " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 12/21] batman-adv: Convert batadv_tvlv_container " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 13/21] batman-adv: Convert batadv_tvlv_handler " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 14/21] batman-adv: Convert batadv_tt_orig_list_entry " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 15/21] batman-adv: Convert batadv_neigh_ifinfo " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 16/21] batman-adv: Convert batadv_orig_ifinfo " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 17/21] batman-adv: Convert batadv_neigh_node " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 18/21] batman-adv: Convert batadv_hard_iface " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 19/21] batman-adv: Convert batadv_orig_node_vlan " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 20/21] batman-adv: Convert batadv_orig_node " Antonio Quartulli
2016-02-10 15:57 ` [B.A.T.M.A.N.] [PATCH 21/21] batman-adv: Convert batadv_tt_common_entry " Antonio Quartulli
2016-02-11  8:50 ` [B.A.T.M.A.N.] pull request [net-next]: batman-adv 20160210 David Miller

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=1455119847-5862-6-git-send-email-a@unstable.cc \
    --to=a@unstable.cc \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    --cc=davem@davemloft.net \
    --cc=mareklindner@neomailbox.ch \
    --cc=netdev@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).