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 21/21] batman-adv: Convert batadv_tt_common_entry to kref
Date: Wed, 10 Feb 2016 23:57:27 +0800	[thread overview]
Message-ID: <1455119847-5862-22-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/translation-table.c | 64 +++++++++++++++++++++++++++++---------
 net/batman-adv/types.h             |  2 +-
 2 files changed, 51 insertions(+), 15 deletions(-)

diff --git a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c
index 96b1571213bd..11882793f0a4 100644
--- a/net/batman-adv/translation-table.c
+++ b/net/batman-adv/translation-table.c
@@ -142,7 +142,7 @@ batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
 		if (tt->vid != vid)
 			continue;
 
-		if (!atomic_inc_not_zero(&tt->refcount))
+		if (!kref_get_unless_zero(&tt->refcount))
 			continue;
 
 		tt_tmp = tt;
@@ -203,25 +203,59 @@ batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
 	return tt_global_entry;
 }
 
-static void
-batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_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)
 {
-	if (atomic_dec_and_test(&tt_local_entry->common.refcount))
-		kfree_rcu(tt_local_entry, common.rcu);
+	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_global_entry_free_ref - decrement the refcounter for a
- *  tt_global_entry and possibly free it
- * @tt_global_entry: the object to free
+ * 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
+batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
+{
+	kref_put(&tt_local_entry->common.refcount,
+		 batadv_tt_local_entry_release);
+}
+
+/**
+ * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
+ *  for free after rcu grace period
+ * @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
 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
 {
-	if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
-		batadv_tt_global_del_orig_list(tt_global_entry);
-		kfree_rcu(tt_global_entry, common.rcu);
-	}
+	kref_put(&tt_global_entry->common.refcount,
+		 batadv_tt_global_entry_release);
 }
 
 /**
@@ -633,7 +667,8 @@ bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
 	tt_local->common.vid = vid;
 	if (batadv_is_wifi_netdev(in_dev))
 		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->common.added_at = tt_local->last_seen;
 
@@ -1415,7 +1450,8 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
 		 */
 		if (flags & BATADV_TT_CLIENT_ROAM)
 			tt_global_entry->roam_at = jiffies;
-		atomic_set(&common->refcount, 2);
+		kref_init(&common->refcount);
+		kref_get(&common->refcount);
 		common->added_at = jiffies;
 
 		INIT_HLIST_HEAD(&tt_global_entry->orig_list);
diff --git a/net/batman-adv/types.h b/net/batman-adv/types.h
index aea82a121c02..612de23178e6 100644
--- a/net/batman-adv/types.h
+++ b/net/batman-adv/types.h
@@ -972,7 +972,7 @@ struct batadv_tt_common_entry {
 	struct hlist_node hash_entry;
 	u16 flags;
 	unsigned long added_at;
-	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 ` [B.A.T.M.A.N.] [PATCH 05/21] batman-adv: Convert batadv_gw_node " Antonio Quartulli
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 ` Antonio Quartulli [this message]
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-22-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).