Skip to content

Commit 73f7cbb

Browse files
ecsvgregkh
authored andcommitted
batman-adv: Check ptr for NULL before reducing its refcnt
commit 6340dcb upstream. The commit b37a466 ("netdevice: add the case if dev is NULL") changed the way how the NULL check for net_devices have to be handled when trying to reduce its reference counter. Before this commit, it was the responsibility of the caller to check whether the object is NULL or not. But it was changed to behave more like kfree. Now the callee has to handle the NULL-case. The batman-adv code was scanned via cocinelle for similar places. These were changed to use the paradigm @@ identifier E, T, R, C; identifier put; @@ void put(struct T *E) { + if (!E) + return; kref_put(&E->C, R); } Functions which were used in other sources files were moved to the header to allow the compiler to inline the NULL check and the kref_put call. Signed-off-by: Sven Eckelmann <sven@narfation.org> Signed-off-by: Simon Wunderlich <sw@simonwunderlich.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f6da750 commit 73f7cbb

14 files changed

Lines changed: 181 additions & 113 deletions

net/batman-adv/bridge_loop_avoidance.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ static void batadv_backbone_gw_release(struct kref *ref)
164164
*/
165165
static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
166166
{
167+
if (!backbone_gw)
168+
return;
169+
167170
kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
168171
}
169172

@@ -199,6 +202,9 @@ static void batadv_claim_release(struct kref *ref)
199202
*/
200203
static void batadv_claim_put(struct batadv_bla_claim *claim)
201204
{
205+
if (!claim)
206+
return;
207+
202208
kref_put(&claim->refcount, batadv_claim_release);
203209
}
204210

net/batman-adv/distributed-arp-table.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ static void batadv_dat_entry_release(struct kref *ref)
128128
*/
129129
static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
130130
{
131+
if (!dat_entry)
132+
return;
133+
131134
kref_put(&dat_entry->refcount, batadv_dat_entry_release);
132135
}
133136

net/batman-adv/gateway_client.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
* after rcu grace period
6161
* @ref: kref pointer of the gw_node
6262
*/
63-
static void batadv_gw_node_release(struct kref *ref)
63+
void batadv_gw_node_release(struct kref *ref)
6464
{
6565
struct batadv_gw_node *gw_node;
6666

@@ -70,16 +70,6 @@ static void batadv_gw_node_release(struct kref *ref)
7070
kfree_rcu(gw_node, rcu);
7171
}
7272

73-
/**
74-
* batadv_gw_node_put() - decrement the gw_node refcounter and possibly release
75-
* it
76-
* @gw_node: gateway node to free
77-
*/
78-
void batadv_gw_node_put(struct batadv_gw_node *gw_node)
79-
{
80-
kref_put(&gw_node->refcount, batadv_gw_node_release);
81-
}
82-
8373
/**
8474
* batadv_gw_get_selected_gw_node() - Get currently selected gateway
8575
* @bat_priv: the bat priv with all the soft interface information

net/batman-adv/gateway_client.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "main.h"
1111

12+
#include <linux/kref.h>
1213
#include <linux/netlink.h>
1314
#include <linux/seq_file.h>
1415
#include <linux/skbuff.h>
@@ -28,7 +29,7 @@ void batadv_gw_node_update(struct batadv_priv *bat_priv,
2829
void batadv_gw_node_delete(struct batadv_priv *bat_priv,
2930
struct batadv_orig_node *orig_node);
3031
void batadv_gw_node_free(struct batadv_priv *bat_priv);
31-
void batadv_gw_node_put(struct batadv_gw_node *gw_node);
32+
void batadv_gw_node_release(struct kref *ref);
3233
struct batadv_gw_node *
3334
batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv);
3435
int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset);
@@ -40,4 +41,17 @@ batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
4041
struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
4142
struct batadv_orig_node *orig_node);
4243

44+
/**
45+
* batadv_gw_node_put() - decrement the gw_node refcounter and possibly release
46+
* it
47+
* @gw_node: gateway node to free
48+
*/
49+
static inline void batadv_gw_node_put(struct batadv_gw_node *gw_node)
50+
{
51+
if (!gw_node)
52+
return;
53+
54+
kref_put(&gw_node->refcount, batadv_gw_node_release);
55+
}
56+
4357
#endif /* _NET_BATMAN_ADV_GATEWAY_CLIENT_H_ */

net/batman-adv/hard-interface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
113113
*/
114114
static inline void batadv_hardif_put(struct batadv_hard_iface *hard_iface)
115115
{
116+
if (!hard_iface)
117+
return;
118+
116119
kref_put(&hard_iface->refcount, batadv_hardif_release);
117120
}
118121

net/batman-adv/network-coding.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ static void batadv_nc_node_release(struct kref *ref)
222222
*/
223223
static void batadv_nc_node_put(struct batadv_nc_node *nc_node)
224224
{
225+
if (!nc_node)
226+
return;
227+
225228
kref_put(&nc_node->refcount, batadv_nc_node_release);
226229
}
227230

@@ -246,6 +249,9 @@ static void batadv_nc_path_release(struct kref *ref)
246249
*/
247250
static void batadv_nc_path_put(struct batadv_nc_path *nc_path)
248251
{
252+
if (!nc_path)
253+
return;
254+
249255
kref_put(&nc_path->refcount, batadv_nc_path_release);
250256
}
251257

net/batman-adv/originator.c

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
178178
* and queue for free after rcu grace period
179179
* @ref: kref pointer of the originator-vlan object
180180
*/
181-
static void batadv_orig_node_vlan_release(struct kref *ref)
181+
void batadv_orig_node_vlan_release(struct kref *ref)
182182
{
183183
struct batadv_orig_node_vlan *orig_vlan;
184184

@@ -187,16 +187,6 @@ static void batadv_orig_node_vlan_release(struct kref *ref)
187187
kfree_rcu(orig_vlan, rcu);
188188
}
189189

190-
/**
191-
* batadv_orig_node_vlan_put() - decrement the refcounter and possibly release
192-
* the originator-vlan object
193-
* @orig_vlan: the originator-vlan object to release
194-
*/
195-
void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
196-
{
197-
kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
198-
}
199-
200190
/**
201191
* batadv_originator_init() - Initialize all originator structures
202192
* @bat_priv: the bat priv with all the soft interface information
@@ -232,7 +222,7 @@ int batadv_originator_init(struct batadv_priv *bat_priv)
232222
* free after rcu grace period
233223
* @ref: kref pointer of the neigh_ifinfo
234224
*/
235-
static void batadv_neigh_ifinfo_release(struct kref *ref)
225+
void batadv_neigh_ifinfo_release(struct kref *ref)
236226
{
237227
struct batadv_neigh_ifinfo *neigh_ifinfo;
238228

@@ -244,22 +234,12 @@ static void batadv_neigh_ifinfo_release(struct kref *ref)
244234
kfree_rcu(neigh_ifinfo, rcu);
245235
}
246236

247-
/**
248-
* batadv_neigh_ifinfo_put() - decrement the refcounter and possibly release
249-
* the neigh_ifinfo
250-
* @neigh_ifinfo: the neigh_ifinfo object to release
251-
*/
252-
void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
253-
{
254-
kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
255-
}
256-
257237
/**
258238
* batadv_hardif_neigh_release() - release hardif neigh node from lists and
259239
* queue for free after rcu grace period
260240
* @ref: kref pointer of the neigh_node
261241
*/
262-
static void batadv_hardif_neigh_release(struct kref *ref)
242+
void batadv_hardif_neigh_release(struct kref *ref)
263243
{
264244
struct batadv_hardif_neigh_node *hardif_neigh;
265245

@@ -274,22 +254,12 @@ static void batadv_hardif_neigh_release(struct kref *ref)
274254
kfree_rcu(hardif_neigh, rcu);
275255
}
276256

277-
/**
278-
* batadv_hardif_neigh_put() - decrement the hardif neighbors refcounter
279-
* and possibly release it
280-
* @hardif_neigh: hardif neigh neighbor to free
281-
*/
282-
void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
283-
{
284-
kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
285-
}
286-
287257
/**
288258
* batadv_neigh_node_release() - release neigh_node from lists and queue for
289259
* free after rcu grace period
290260
* @ref: kref pointer of the neigh_node
291261
*/
292-
static void batadv_neigh_node_release(struct kref *ref)
262+
void batadv_neigh_node_release(struct kref *ref)
293263
{
294264
struct hlist_node *node_tmp;
295265
struct batadv_neigh_node *neigh_node;
@@ -309,16 +279,6 @@ static void batadv_neigh_node_release(struct kref *ref)
309279
kfree_rcu(neigh_node, rcu);
310280
}
311281

312-
/**
313-
* batadv_neigh_node_put() - decrement the neighbors refcounter and possibly
314-
* release it
315-
* @neigh_node: neigh neighbor to free
316-
*/
317-
void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
318-
{
319-
kref_put(&neigh_node->refcount, batadv_neigh_node_release);
320-
}
321-
322282
/**
323283
* batadv_orig_router_get() - router to the originator depending on iface
324284
* @orig_node: the orig node for the router
@@ -851,7 +811,7 @@ int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
851811
* free after rcu grace period
852812
* @ref: kref pointer of the orig_ifinfo
853813
*/
854-
static void batadv_orig_ifinfo_release(struct kref *ref)
814+
void batadv_orig_ifinfo_release(struct kref *ref)
855815
{
856816
struct batadv_orig_ifinfo *orig_ifinfo;
857817
struct batadv_neigh_node *router;
@@ -869,16 +829,6 @@ static void batadv_orig_ifinfo_release(struct kref *ref)
869829
kfree_rcu(orig_ifinfo, rcu);
870830
}
871831

872-
/**
873-
* batadv_orig_ifinfo_put() - decrement the refcounter and possibly release
874-
* the orig_ifinfo
875-
* @orig_ifinfo: the orig_ifinfo object to release
876-
*/
877-
void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
878-
{
879-
kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
880-
}
881-
882832
/**
883833
* batadv_orig_node_free_rcu() - free the orig_node
884834
* @rcu: rcu pointer of the orig_node
@@ -902,7 +852,7 @@ static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
902852
* free after rcu grace period
903853
* @ref: kref pointer of the orig_node
904854
*/
905-
static void batadv_orig_node_release(struct kref *ref)
855+
void batadv_orig_node_release(struct kref *ref)
906856
{
907857
struct hlist_node *node_tmp;
908858
struct batadv_neigh_node *neigh_node;
@@ -948,16 +898,6 @@ static void batadv_orig_node_release(struct kref *ref)
948898
call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
949899
}
950900

951-
/**
952-
* batadv_orig_node_put() - decrement the orig node refcounter and possibly
953-
* release it
954-
* @orig_node: the orig node to free
955-
*/
956-
void batadv_orig_node_put(struct batadv_orig_node *orig_node)
957-
{
958-
kref_put(&orig_node->refcount, batadv_orig_node_release);
959-
}
960-
961901
/**
962902
* batadv_originator_free() - Free all originator structures
963903
* @bat_priv: the bat priv with all the soft interface information

0 commit comments

Comments
 (0)