Skip to content

Commit 1605d0d

Browse files
author
Marek Lindner
committed
batman-adv: increase refcount in create_neighbor to be consistent
Signed-off-by: Marek Lindner <lindner_marek@yahoo.de>
1 parent 7b36e8e commit 1605d0d

2 files changed

Lines changed: 30 additions & 37 deletions

File tree

‎net/batman-adv/originator.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node,
9494
memcpy(neigh_node->addr, neigh, ETH_ALEN);
9595
neigh_node->orig_node = orig_neigh_node;
9696
neigh_node->if_incoming = if_incoming;
97-
atomic_set(&neigh_node->refcount, 1);
97+
98+
/* extra reference for return */
99+
atomic_set(&neigh_node->refcount, 2);
98100

99101
spin_lock_bh(&orig_node->neigh_list_lock);
100102
hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);

‎net/batman-adv/routing.c‎

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static int is_bidirectional_neigh(struct orig_node *orig_node,
150150
struct batman_if *if_incoming)
151151
{
152152
struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
153-
struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
153+
struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
154154
struct hlist_node *node;
155155
unsigned char total_count;
156156
uint8_t orig_eq_count, neigh_rq_count, tq_own;
@@ -161,27 +161,27 @@ static int is_bidirectional_neigh(struct orig_node *orig_node,
161161
hlist_for_each_entry_rcu(tmp_neigh_node, node,
162162
&orig_node->neigh_list, list) {
163163

164-
if (compare_eth(tmp_neigh_node->addr,
165-
orig_neigh_node->orig) &&
166-
(tmp_neigh_node->if_incoming == if_incoming))
167-
neigh_node = tmp_neigh_node;
164+
if (!compare_eth(tmp_neigh_node->addr,
165+
orig_neigh_node->orig))
166+
continue;
167+
168+
if (tmp_neigh_node->if_incoming != if_incoming)
169+
continue;
170+
171+
if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
172+
continue;
173+
174+
neigh_node = tmp_neigh_node;
168175
}
176+
rcu_read_unlock();
169177

170178
if (!neigh_node)
171179
neigh_node = create_neighbor(orig_node,
172180
orig_neigh_node,
173181
orig_neigh_node->orig,
174182
if_incoming);
175-
/* create_neighbor failed, return 0 */
176183
if (!neigh_node)
177-
goto unlock;
178-
179-
if (!atomic_inc_not_zero(&neigh_node->refcount)) {
180-
neigh_node = NULL;
181-
goto unlock;
182-
}
183-
184-
rcu_read_unlock();
184+
goto out;
185185

186186
neigh_node->last_valid = jiffies;
187187
} else {
@@ -190,27 +190,27 @@ static int is_bidirectional_neigh(struct orig_node *orig_node,
190190
hlist_for_each_entry_rcu(tmp_neigh_node, node,
191191
&orig_neigh_node->neigh_list, list) {
192192

193-
if (compare_eth(tmp_neigh_node->addr,
194-
orig_neigh_node->orig) &&
195-
(tmp_neigh_node->if_incoming == if_incoming))
196-
neigh_node = tmp_neigh_node;
193+
if (!compare_eth(tmp_neigh_node->addr,
194+
orig_neigh_node->orig))
195+
continue;
196+
197+
if (tmp_neigh_node->if_incoming != if_incoming)
198+
continue;
199+
200+
if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
201+
continue;
202+
203+
neigh_node = tmp_neigh_node;
197204
}
205+
rcu_read_unlock();
198206

199207
if (!neigh_node)
200208
neigh_node = create_neighbor(orig_neigh_node,
201209
orig_neigh_node,
202210
orig_neigh_node->orig,
203211
if_incoming);
204-
/* create_neighbor failed, return 0 */
205212
if (!neigh_node)
206-
goto unlock;
207-
208-
if (!atomic_inc_not_zero(&neigh_node->refcount)) {
209-
neigh_node = NULL;
210-
goto unlock;
211-
}
212-
213-
rcu_read_unlock();
213+
goto out;
214214
}
215215

216216
orig_node->last_valid = jiffies;
@@ -265,10 +265,6 @@ static int is_bidirectional_neigh(struct orig_node *orig_node,
265265
if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
266266
ret = 1;
267267

268-
goto out;
269-
270-
unlock:
271-
rcu_read_unlock();
272268
out:
273269
if (neigh_node)
274270
neigh_node_free_ref(neigh_node);
@@ -423,11 +419,6 @@ static void update_orig(struct bat_priv *bat_priv,
423419
orig_node_free_ref(orig_tmp);
424420
if (!neigh_node)
425421
goto unlock;
426-
427-
if (!atomic_inc_not_zero(&neigh_node->refcount)) {
428-
neigh_node = NULL;
429-
goto unlock;
430-
}
431422
} else
432423
bat_dbg(DBG_BATMAN, bat_priv,
433424
"Updating existing last-hop neighbor of originator\n");

0 commit comments

Comments
 (0)