[v2,17/18] batman-adv: Place kref_get for tvlv_container near use

Message ID 1468597173-25378-17-git-send-email-sven@narfation.org (mailing list archive)
State Accepted, archived
Commit aebb6e7f02e480a6bbae9833ecaf7e11e316f7c6
Delegated to: Marek Lindner
Headers

Commit Message

Sven Eckelmann July 15, 2016, 3:39 p.m. UTC
  It is hard to understand why the refcnt is increased when it isn't done
near the actual place the new reference is used. So using kref_get right
before the place which requires the reference and in the same function
helps to avoid accidental problems causedy incorrect reference counting.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
v2:
 - split patch based on type

 net/batman-adv/tvlv.c | 5 +++++
 1 file changed, 5 insertions(+)
  

Comments

Marek Lindner July 19, 2016, 8:45 a.m. UTC | #1
On Friday, July 15, 2016 17:39:32 Sven Eckelmann wrote:
> It is hard to understand why the refcnt is increased when it isn't done
> near the actual place the new reference is used. So using kref_get right
> before the place which requires the reference and in the same function
> helps to avoid accidental problems causedy incorrect reference counting.
> 
> Signed-off-by: Sven Eckelmann <sven@narfation.org>
> ---
> v2:
>  - split patch based on type
> 
>  net/batman-adv/tvlv.c | 5 +++++
>  1 file changed, 5 insertions(+)

Applied in revision aebb6e7.

Thanks,
Marek
  

Patch

diff --git a/net/batman-adv/tvlv.c b/net/batman-adv/tvlv.c
index 3d1cf0f..3533867 100644
--- a/net/batman-adv/tvlv.c
+++ b/net/batman-adv/tvlv.c
@@ -257,8 +257,13 @@  void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
 	spin_lock_bh(&bat_priv->tvlv.container_list_lock);
 	tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
 	batadv_tvlv_container_remove(bat_priv, tvlv_old);
+
+	kref_get(&tvlv_new->refcount);
 	hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list);
 	spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
+
+	/* don't return reference to new tvlv_container */
+	batadv_tvlv_container_put(tvlv_new);
 }
 
 /**