Skip to content

Commit e78783d

Browse files
ecsvsimonwunderlich
authored andcommitted
batman-adv: Check ptr for NULL before reducing its refcnt
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>
1 parent 5520722 commit e78783d

14 files changed

+181
-113
lines changed

net/batman-adv/bridge_loop_avoidance.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ static void batadv_backbone_gw_release(struct kref *ref)
162162
*/
163163
static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
164164
{
165+
if (!backbone_gw)
166+
return;
167+
165168
kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
166169
}
167170

@@ -197,6 +200,9 @@ static void batadv_claim_release(struct kref *ref)
197200
*/
198201
static void batadv_claim_put(struct batadv_bla_claim *claim)
199202
{
203+
if (!claim)
204+
return;
205+
200206
kref_put(&claim->refcount, batadv_claim_release);
201207
}
202208

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

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

net/batman-adv/gateway_client.c

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

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

72-
/**
73-
* batadv_gw_node_put() - decrement the gw_node refcounter and possibly release
74-
* it
75-
* @gw_node: gateway node to free
76-
*/
77-
void batadv_gw_node_put(struct batadv_gw_node *gw_node)
78-
{
79-
kref_put(&gw_node->refcount, batadv_gw_node_release);
80-
}
81-
8272
/**
8373
* batadv_gw_get_selected_gw_node() - Get currently selected gateway
8474
* @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/skbuff.h>
1415
#include <linux/types.h>
@@ -27,7 +28,7 @@ void batadv_gw_node_update(struct batadv_priv *bat_priv,
2728
void batadv_gw_node_delete(struct batadv_priv *bat_priv,
2829
struct batadv_orig_node *orig_node);
2930
void batadv_gw_node_free(struct batadv_priv *bat_priv);
30-
void batadv_gw_node_put(struct batadv_gw_node *gw_node);
31+
void batadv_gw_node_release(struct kref *ref);
3132
struct batadv_gw_node *
3233
batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv);
3334
int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb);
@@ -38,4 +39,17 @@ batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
3839
struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
3940
struct batadv_orig_node *orig_node);
4041

42+
/**
43+
* batadv_gw_node_put() - decrement the gw_node refcounter and possibly release
44+
* it
45+
* @gw_node: gateway node to free
46+
*/
47+
static inline void batadv_gw_node_put(struct batadv_gw_node *gw_node)
48+
{
49+
if (!gw_node)
50+
return;
51+
52+
kref_put(&gw_node->refcount, batadv_gw_node_release);
53+
}
54+
4155
#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
@@ -89,6 +89,9 @@ int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
8989
*/
9090
static inline void batadv_hardif_put(struct batadv_hard_iface *hard_iface)
9191
{
92+
if (!hard_iface)
93+
return;
94+
9295
kref_put(&hard_iface->refcount, batadv_hardif_release);
9396
}
9497

net/batman-adv/network-coding.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ static void batadv_nc_node_release(struct kref *ref)
217217
*/
218218
static void batadv_nc_node_put(struct batadv_nc_node *nc_node)
219219
{
220+
if (!nc_node)
221+
return;
222+
220223
kref_put(&nc_node->refcount, batadv_nc_node_release);
221224
}
222225

@@ -241,6 +244,9 @@ static void batadv_nc_path_release(struct kref *ref)
241244
*/
242245
static void batadv_nc_path_put(struct batadv_nc_path *nc_path)
243246
{
247+
if (!nc_path)
248+
return;
249+
244250
kref_put(&nc_path->refcount, batadv_nc_path_release);
245251
}
246252

net/batman-adv/originator.c

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

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

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

@@ -243,22 +233,12 @@ static void batadv_neigh_ifinfo_release(struct kref *ref)
243233
kfree_rcu(neigh_ifinfo, rcu);
244234
}
245235

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

@@ -273,22 +253,12 @@ static void batadv_hardif_neigh_release(struct kref *ref)
273253
kfree_rcu(hardif_neigh, rcu);
274254
}
275255

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

311-
/**
312-
* batadv_neigh_node_put() - decrement the neighbors refcounter and possibly
313-
* release it
314-
* @neigh_node: neigh neighbor to free
315-
*/
316-
void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
317-
{
318-
kref_put(&neigh_node->refcount, batadv_neigh_node_release);
319-
}
320-
321281
/**
322282
* batadv_orig_router_get() - router to the originator depending on iface
323283
* @orig_node: the orig node for the router
@@ -812,7 +772,7 @@ int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
812772
* free after rcu grace period
813773
* @ref: kref pointer of the orig_ifinfo
814774
*/
815-
static void batadv_orig_ifinfo_release(struct kref *ref)
775+
void batadv_orig_ifinfo_release(struct kref *ref)
816776
{
817777
struct batadv_orig_ifinfo *orig_ifinfo;
818778
struct batadv_neigh_node *router;
@@ -830,16 +790,6 @@ static void batadv_orig_ifinfo_release(struct kref *ref)
830790
kfree_rcu(orig_ifinfo, rcu);
831791
}
832792

833-
/**
834-
* batadv_orig_ifinfo_put() - decrement the refcounter and possibly release
835-
* the orig_ifinfo
836-
* @orig_ifinfo: the orig_ifinfo object to release
837-
*/
838-
void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
839-
{
840-
kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
841-
}
842-
843793
/**
844794
* batadv_orig_node_free_rcu() - free the orig_node
845795
* @rcu: rcu pointer of the orig_node
@@ -863,7 +813,7 @@ static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
863813
* free after rcu grace period
864814
* @ref: kref pointer of the orig_node
865815
*/
866-
static void batadv_orig_node_release(struct kref *ref)
816+
void batadv_orig_node_release(struct kref *ref)
867817
{
868818
struct hlist_node *node_tmp;
869819
struct batadv_neigh_node *neigh_node;
@@ -909,16 +859,6 @@ static void batadv_orig_node_release(struct kref *ref)
909859
call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
910860
}
911861

912-
/**
913-
* batadv_orig_node_put() - decrement the orig node refcounter and possibly
914-
* release it
915-
* @orig_node: the orig node to free
916-
*/
917-
void batadv_orig_node_put(struct batadv_orig_node *orig_node)
918-
{
919-
kref_put(&orig_node->refcount, batadv_orig_node_release);
920-
}
921-
922862
/**
923863
* batadv_originator_free() - Free all originator structures
924864
* @bat_priv: the bat priv with all the soft interface information

0 commit comments

Comments
 (0)