From patchwork Sat Jan 16 09:29:57 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 5005 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=79.140.41.39; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver=b.a.t.m.a.n@lists.open-mesh.org Authentication-Results: open-mesh.org; dmarc=pass header.from=narfation.org Authentication-Results: open-mesh.org; dkim=pass reason="1024-bit key; unprotected key" header.d=narfation.org header.i=@narfation.org header.b=gJFVr1/2; dkim-adsp=pass; dkim-atps=neutral Received: from v3-1039.vlinux.de (narfation.org [79.140.41.39]) by open-mesh.org (Postfix) with ESMTPS id 55DD281B81 for ; Sat, 16 Jan 2016 10:30:16 +0100 (CET) Received: from sven-desktop.home.narfation.org (p200300C593C167FD0000000000002E16.dip0.t-ipconnect.de [IPv6:2003:c5:93c1:67fd::2e16]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id AAFBA110115; Sat, 16 Jan 2016 10:30:15 +0100 (CET) Authentication-Results: v3-1039.vlinux.de; dmarc=none header.from=narfation.org DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=narfation.org; s=20121; t=1452936615; bh=M8uO42k62gsnPrnM8iCCPTkBh1n8g6hbMAolmlbWoBA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gJFVr1/2SaF1D2mI+O06G9VLc7UobjyAeZN+Nu59n1HRpbNN3EicmxJ8yheN30ze+ 75kFlay9AtnJr/0PEVabHKc/IW2I5bKlqk3KhmWSKCZRXdo8ukbaSbAi4Q3g5c6y7e ttMK6hZIQDjusABpVpX1ly/YlrHajS7kEsH48lK4= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Sat, 16 Jan 2016 10:29:57 +0100 Message-Id: <1452936598-28619-19-git-send-email-sven@narfation.org> X-Mailer: git-send-email 2.7.0.rc3 In-Reply-To: <1452936598-28619-1-git-send-email-sven@narfation.org> References: <1452936598-28619-1-git-send-email-sven@narfation.org> Subject: [B.A.T.M.A.N.] [PATCH v6 19/20] batman-adv: Convert batadv_tt_common_entry to kref X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Jan 2016 09:30:16 -0000 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 --- v6: - removed patches which are now applied in the branch next - rebased remaining patches on the patch "batman-adv: Avoid recursive call_rcu for batadv_nc_node" which was modified by Marek while he applied the patches (this unfortunately made some of the remaining patches "hard" to apply) v5: - add hack which allows to compile against stable kernel like 3.2.44 which also added the kref_get_unless_zero function 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/translation-table.c | 60 ++++++++++++++++++++++++++++++-------- net/batman-adv/types.h | 2 +- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c index 96b1571..1188279 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; } +/** + * 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) +{ + 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_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) { - if (atomic_dec_and_test(&tt_local_entry->common.refcount)) - kfree_rcu(tt_local_entry, common.rcu); + kref_put(&tt_local_entry->common.refcount, + batadv_tt_local_entry_release); } /** - * 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_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 aea82a1..612de23 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; };