Skip to content

Commit

Permalink
batman-adv: Check ptr for NULL before reducing its refcnt
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ecsv authored and gregkh committed Apr 8, 2022
1 parent f6da750 commit 73f7cbb
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 113 deletions.
6 changes: 6 additions & 0 deletions net/batman-adv/bridge_loop_avoidance.c
Expand Up @@ -164,6 +164,9 @@ static void batadv_backbone_gw_release(struct kref *ref)
*/
static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
{
if (!backbone_gw)
return;

kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
}

Expand Down Expand Up @@ -199,6 +202,9 @@ static void batadv_claim_release(struct kref *ref)
*/
static void batadv_claim_put(struct batadv_bla_claim *claim)
{
if (!claim)
return;

kref_put(&claim->refcount, batadv_claim_release);
}

Expand Down
3 changes: 3 additions & 0 deletions net/batman-adv/distributed-arp-table.c
Expand Up @@ -128,6 +128,9 @@ static void batadv_dat_entry_release(struct kref *ref)
*/
static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
{
if (!dat_entry)
return;

kref_put(&dat_entry->refcount, batadv_dat_entry_release);
}

Expand Down
12 changes: 1 addition & 11 deletions net/batman-adv/gateway_client.c
Expand Up @@ -60,7 +60,7 @@
* after rcu grace period
* @ref: kref pointer of the gw_node
*/
static void batadv_gw_node_release(struct kref *ref)
void batadv_gw_node_release(struct kref *ref)
{
struct batadv_gw_node *gw_node;

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

/**
* batadv_gw_node_put() - decrement the gw_node refcounter and possibly release
* it
* @gw_node: gateway node to free
*/
void batadv_gw_node_put(struct batadv_gw_node *gw_node)
{
kref_put(&gw_node->refcount, batadv_gw_node_release);
}

/**
* batadv_gw_get_selected_gw_node() - Get currently selected gateway
* @bat_priv: the bat priv with all the soft interface information
Expand Down
16 changes: 15 additions & 1 deletion net/batman-adv/gateway_client.h
Expand Up @@ -9,6 +9,7 @@

#include "main.h"

#include <linux/kref.h>
#include <linux/netlink.h>
#include <linux/seq_file.h>
#include <linux/skbuff.h>
Expand All @@ -28,7 +29,7 @@ void batadv_gw_node_update(struct batadv_priv *bat_priv,
void batadv_gw_node_delete(struct batadv_priv *bat_priv,
struct batadv_orig_node *orig_node);
void batadv_gw_node_free(struct batadv_priv *bat_priv);
void batadv_gw_node_put(struct batadv_gw_node *gw_node);
void batadv_gw_node_release(struct kref *ref);
struct batadv_gw_node *
batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv);
int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset);
Expand All @@ -40,4 +41,17 @@ batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
struct batadv_orig_node *orig_node);

/**
* batadv_gw_node_put() - decrement the gw_node refcounter and possibly release
* it
* @gw_node: gateway node to free
*/
static inline void batadv_gw_node_put(struct batadv_gw_node *gw_node)
{
if (!gw_node)
return;

kref_put(&gw_node->refcount, batadv_gw_node_release);
}

#endif /* _NET_BATMAN_ADV_GATEWAY_CLIENT_H_ */
3 changes: 3 additions & 0 deletions net/batman-adv/hard-interface.h
Expand Up @@ -113,6 +113,9 @@ int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
*/
static inline void batadv_hardif_put(struct batadv_hard_iface *hard_iface)
{
if (!hard_iface)
return;

kref_put(&hard_iface->refcount, batadv_hardif_release);
}

Expand Down
6 changes: 6 additions & 0 deletions net/batman-adv/network-coding.c
Expand Up @@ -222,6 +222,9 @@ static void batadv_nc_node_release(struct kref *ref)
*/
static void batadv_nc_node_put(struct batadv_nc_node *nc_node)
{
if (!nc_node)
return;

kref_put(&nc_node->refcount, batadv_nc_node_release);
}

Expand All @@ -246,6 +249,9 @@ static void batadv_nc_path_release(struct kref *ref)
*/
static void batadv_nc_path_put(struct batadv_nc_path *nc_path)
{
if (!nc_path)
return;

kref_put(&nc_path->refcount, batadv_nc_path_release);
}

Expand Down
72 changes: 6 additions & 66 deletions net/batman-adv/originator.c
Expand Up @@ -178,7 +178,7 @@ batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
* and queue for free after rcu grace period
* @ref: kref pointer of the originator-vlan object
*/
static void batadv_orig_node_vlan_release(struct kref *ref)
void batadv_orig_node_vlan_release(struct kref *ref)
{
struct batadv_orig_node_vlan *orig_vlan;

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

/**
* batadv_orig_node_vlan_put() - decrement the refcounter and possibly release
* the originator-vlan object
* @orig_vlan: the originator-vlan object to release
*/
void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
{
kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
}

/**
* batadv_originator_init() - Initialize all originator structures
* @bat_priv: the bat priv with all the soft interface information
Expand Down Expand Up @@ -232,7 +222,7 @@ int batadv_originator_init(struct batadv_priv *bat_priv)
* free after rcu grace period
* @ref: kref pointer of the neigh_ifinfo
*/
static void batadv_neigh_ifinfo_release(struct kref *ref)
void batadv_neigh_ifinfo_release(struct kref *ref)
{
struct batadv_neigh_ifinfo *neigh_ifinfo;

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

/**
* batadv_neigh_ifinfo_put() - decrement the refcounter and possibly release
* the neigh_ifinfo
* @neigh_ifinfo: the neigh_ifinfo object to release
*/
void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
{
kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
}

/**
* batadv_hardif_neigh_release() - release hardif neigh node from lists and
* queue for free after rcu grace period
* @ref: kref pointer of the neigh_node
*/
static void batadv_hardif_neigh_release(struct kref *ref)
void batadv_hardif_neigh_release(struct kref *ref)
{
struct batadv_hardif_neigh_node *hardif_neigh;

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

/**
* batadv_hardif_neigh_put() - decrement the hardif neighbors refcounter
* and possibly release it
* @hardif_neigh: hardif neigh neighbor to free
*/
void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
{
kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
}

/**
* batadv_neigh_node_release() - release neigh_node from lists and queue for
* free after rcu grace period
* @ref: kref pointer of the neigh_node
*/
static void batadv_neigh_node_release(struct kref *ref)
void batadv_neigh_node_release(struct kref *ref)
{
struct hlist_node *node_tmp;
struct batadv_neigh_node *neigh_node;
Expand All @@ -309,16 +279,6 @@ static void batadv_neigh_node_release(struct kref *ref)
kfree_rcu(neigh_node, rcu);
}

/**
* batadv_neigh_node_put() - decrement the neighbors refcounter and possibly
* release it
* @neigh_node: neigh neighbor to free
*/
void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
{
kref_put(&neigh_node->refcount, batadv_neigh_node_release);
}

/**
* batadv_orig_router_get() - router to the originator depending on iface
* @orig_node: the orig node for the router
Expand Down Expand Up @@ -851,7 +811,7 @@ int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
* free after rcu grace period
* @ref: kref pointer of the orig_ifinfo
*/
static void batadv_orig_ifinfo_release(struct kref *ref)
void batadv_orig_ifinfo_release(struct kref *ref)
{
struct batadv_orig_ifinfo *orig_ifinfo;
struct batadv_neigh_node *router;
Expand All @@ -869,16 +829,6 @@ static void batadv_orig_ifinfo_release(struct kref *ref)
kfree_rcu(orig_ifinfo, rcu);
}

/**
* batadv_orig_ifinfo_put() - decrement the refcounter and possibly release
* the orig_ifinfo
* @orig_ifinfo: the orig_ifinfo object to release
*/
void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
{
kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
}

/**
* batadv_orig_node_free_rcu() - free the orig_node
* @rcu: rcu pointer of the orig_node
Expand All @@ -902,7 +852,7 @@ static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
* free after rcu grace period
* @ref: kref pointer of the orig_node
*/
static void batadv_orig_node_release(struct kref *ref)
void batadv_orig_node_release(struct kref *ref)
{
struct hlist_node *node_tmp;
struct batadv_neigh_node *neigh_node;
Expand Down Expand Up @@ -948,16 +898,6 @@ static void batadv_orig_node_release(struct kref *ref)
call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
}

/**
* batadv_orig_node_put() - decrement the orig node refcounter and possibly
* release it
* @orig_node: the orig node to free
*/
void batadv_orig_node_put(struct batadv_orig_node *orig_node)
{
kref_put(&orig_node->refcount, batadv_orig_node_release);
}

/**
* batadv_originator_free() - Free all originator structures
* @bat_priv: the bat priv with all the soft interface information
Expand Down

0 comments on commit 73f7cbb

Please sign in to comment.