Skip to content

Commit 55db2d5

Browse files
ecsvsimonwunderlich
authored andcommitted
batman-adv: Place kref_get for orig_node near use
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 caused by incorrect reference counting. Signed-off-by: Sven Eckelmann <sven@narfation.org> Signed-off-by: Marek Lindner <mareklindner@neomailbox.ch> Signed-off-by: Simon Wunderlich <sw@simonwunderlich.de>
1 parent 8427445 commit 55db2d5

5 files changed

Lines changed: 10 additions & 12 deletions

File tree

‎net/batman-adv/bat_iv_ogm.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,18 @@ batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
324324
if (!orig_node->bat_iv.bcast_own_sum)
325325
goto free_orig_node;
326326

327+
kref_get(&orig_node->refcount);
327328
hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
328329
batadv_choose_orig, orig_node,
329330
&orig_node->hash_entry);
330331
if (hash_added != 0)
331-
goto free_orig_node;
332+
goto free_orig_node_hash;
332333

333334
return orig_node;
334335

335-
free_orig_node:
336-
/* free twice, as batadv_orig_node_new sets refcount to 2 */
336+
free_orig_node_hash:
337337
batadv_orig_node_put(orig_node);
338+
free_orig_node:
338339
batadv_orig_node_put(orig_node);
339340

340341
return NULL;

‎net/batman-adv/bat_v_ogm.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,12 @@ struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
7373
if (!orig_node)
7474
return NULL;
7575

76+
kref_get(&orig_node->refcount);
7677
hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
7778
batadv_choose_orig, orig_node,
7879
&orig_node->hash_entry);
7980
if (hash_added != 0) {
80-
/* orig_node->refcounter is initialised to 2 by
81-
* batadv_orig_node_new()
82-
*/
81+
/* remove refcnt for newly created orig_node and hash entry */
8382
batadv_orig_node_put(orig_node);
8483
batadv_orig_node_put(orig_node);
8584
orig_node = NULL;

‎net/batman-adv/gateway_client.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ static void batadv_gw_node_add(struct batadv_priv *bat_priv,
339339
if (!gw_node)
340340
return;
341341

342-
kref_get(&orig_node->refcount);
343342
INIT_HLIST_NODE(&gw_node->list);
343+
kref_get(&orig_node->refcount);
344344
gw_node->orig_node = orig_node;
345345
gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
346346
gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);

‎net/batman-adv/network-coding.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,14 +856,13 @@ batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
856856
if (!nc_node)
857857
return NULL;
858858

859-
kref_get(&orig_neigh_node->refcount);
860-
861859
/* Initialize nc_node */
862860
INIT_LIST_HEAD(&nc_node->list);
863-
ether_addr_copy(nc_node->addr, orig_node->orig);
864-
nc_node->orig_node = orig_neigh_node;
865861
kref_init(&nc_node->refcount);
866862
kref_get(&nc_node->refcount);
863+
ether_addr_copy(nc_node->addr, orig_node->orig);
864+
kref_get(&orig_neigh_node->refcount);
865+
nc_node->orig_node = orig_neigh_node;
867866

868867
/* Select ingoing or outgoing coding node */
869868
if (in_coding) {

‎net/batman-adv/originator.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,6 @@ struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
989989

990990
/* extra reference for return */
991991
kref_init(&orig_node->refcount);
992-
kref_get(&orig_node->refcount);
993992

994993
orig_node->bat_priv = bat_priv;
995994
ether_addr_copy(orig_node->orig, addr);

0 commit comments

Comments
 (0)