Commit 7a659d56 authored by Sven Eckelmann's avatar Sven Eckelmann Committed by Antonio Quartulli

batman-adv: Convert batadv_hard_iface to kref

batman-adv uses a self-written reference implementation which is just based
on atomic_t. This is less obvious when reading the code than kref and
therefore increases the change that the reference counting will be missed.
Signed-off-by: default avatarSven Eckelmann <sven@narfation.org>
Signed-off-by: default avatarMarek Lindner <mareklindner@neomailbox.ch>
Signed-off-by: default avatarAntonio Quartulli <a@unstable.cc>
parent 77ae32e8
...@@ -644,10 +644,10 @@ static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff, ...@@ -644,10 +644,10 @@ static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
unsigned char *skb_buff; unsigned char *skb_buff;
unsigned int skb_size; unsigned int skb_size;
if (!atomic_inc_not_zero(&if_incoming->refcount)) if (!kref_get_unless_zero(&if_incoming->refcount))
return; return;
if (!atomic_inc_not_zero(&if_outgoing->refcount)) if (!kref_get_unless_zero(&if_outgoing->refcount))
goto out_free_incoming; goto out_free_incoming;
/* own packet should always be scheduled */ /* own packet should always be scheduled */
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "hard-interface.h" #include "hard-interface.h"
#include "main.h" #include "main.h"
#include <linux/atomic.h>
#include <linux/bug.h> #include <linux/bug.h>
#include <linux/byteorder/generic.h> #include <linux/byteorder/generic.h>
#include <linux/errno.h> #include <linux/errno.h>
...@@ -26,6 +27,7 @@ ...@@ -26,6 +27,7 @@
#include <linux/if_ether.h> #include <linux/if_ether.h>
#include <linux/if.h> #include <linux/if.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/kref.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <linux/printk.h> #include <linux/printk.h>
...@@ -50,10 +52,13 @@ ...@@ -50,10 +52,13 @@
/** /**
* batadv_hardif_release - release hard interface from lists and queue for * batadv_hardif_release - release hard interface from lists and queue for
* free after rcu grace period * free after rcu grace period
* @hard_iface: the hard interface to free * @ref: kref pointer of the hard interface
*/ */
void batadv_hardif_release(struct batadv_hard_iface *hard_iface) void batadv_hardif_release(struct kref *ref)
{ {
struct batadv_hard_iface *hard_iface;
hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
dev_put(hard_iface->net_dev); dev_put(hard_iface->net_dev);
kfree_rcu(hard_iface, rcu); kfree_rcu(hard_iface, rcu);
...@@ -67,7 +72,7 @@ batadv_hardif_get_by_netdev(const struct net_device *net_dev) ...@@ -67,7 +72,7 @@ batadv_hardif_get_by_netdev(const struct net_device *net_dev)
rcu_read_lock(); rcu_read_lock();
list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
if (hard_iface->net_dev == net_dev && if (hard_iface->net_dev == net_dev &&
atomic_inc_not_zero(&hard_iface->refcount)) kref_get_unless_zero(&hard_iface->refcount))
goto out; goto out;
} }
...@@ -172,7 +177,7 @@ batadv_hardif_get_active(const struct net_device *soft_iface) ...@@ -172,7 +177,7 @@ batadv_hardif_get_active(const struct net_device *soft_iface)
continue; continue;
if (hard_iface->if_status == BATADV_IF_ACTIVE && if (hard_iface->if_status == BATADV_IF_ACTIVE &&
atomic_inc_not_zero(&hard_iface->refcount)) kref_get_unless_zero(&hard_iface->refcount))
goto out; goto out;
} }
...@@ -206,7 +211,7 @@ static void batadv_primary_if_select(struct batadv_priv *bat_priv, ...@@ -206,7 +211,7 @@ static void batadv_primary_if_select(struct batadv_priv *bat_priv,
ASSERT_RTNL(); ASSERT_RTNL();
if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount)) if (new_hard_iface && !kref_get_unless_zero(&new_hard_iface->refcount))
new_hard_iface = NULL; new_hard_iface = NULL;
curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1); curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
...@@ -434,7 +439,7 @@ int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface, ...@@ -434,7 +439,7 @@ int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
goto out; goto out;
if (!atomic_inc_not_zero(&hard_iface->refcount)) if (!kref_get_unless_zero(&hard_iface->refcount))
goto out; goto out;
soft_iface = dev_get_by_name(&init_net, iface_name); soft_iface = dev_get_by_name(&init_net, iface_name);
...@@ -655,7 +660,8 @@ batadv_hardif_add_interface(struct net_device *net_dev) ...@@ -655,7 +660,8 @@ batadv_hardif_add_interface(struct net_device *net_dev)
hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS; hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
/* extra reference for return */ /* extra reference for return */
atomic_set(&hard_iface->refcount, 2); kref_init(&hard_iface->refcount);
kref_get(&hard_iface->refcount);
batadv_check_known_mac_addr(hard_iface->net_dev); batadv_check_known_mac_addr(hard_iface->net_dev);
list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list); list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
......
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
#include "main.h" #include "main.h"
#include <linux/atomic.h>
#include <linux/compiler.h> #include <linux/compiler.h>
#include <linux/kref.h>
#include <linux/notifier.h> #include <linux/notifier.h>
#include <linux/rcupdate.h> #include <linux/rcupdate.h>
#include <linux/stddef.h> #include <linux/stddef.h>
...@@ -61,18 +61,16 @@ void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface, ...@@ -61,18 +61,16 @@ void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
void batadv_hardif_remove_interfaces(void); void batadv_hardif_remove_interfaces(void);
int batadv_hardif_min_mtu(struct net_device *soft_iface); int batadv_hardif_min_mtu(struct net_device *soft_iface);
void batadv_update_min_mtu(struct net_device *soft_iface); void batadv_update_min_mtu(struct net_device *soft_iface);
void batadv_hardif_release(struct batadv_hard_iface *hard_iface); void batadv_hardif_release(struct kref *ref);
/** /**
* batadv_hardif_free_ref - decrement the hard interface refcounter and * batadv_hardif_free_ref - decrement the hard interface refcounter and possibly
* possibly release it * release it
* @hard_iface: the hard interface to free * @hard_iface: the hard interface to free
*/ */
static inline void static inline void batadv_hardif_free_ref(struct batadv_hard_iface *hard_iface)
batadv_hardif_free_ref(struct batadv_hard_iface *hard_iface)
{ {
if (atomic_dec_and_test(&hard_iface->refcount)) kref_put(&hard_iface->refcount, batadv_hardif_release);
batadv_hardif_release(hard_iface);
} }
static inline struct batadv_hard_iface * static inline struct batadv_hard_iface *
...@@ -85,7 +83,7 @@ batadv_primary_if_get_selected(struct batadv_priv *bat_priv) ...@@ -85,7 +83,7 @@ batadv_primary_if_get_selected(struct batadv_priv *bat_priv)
if (!hard_iface) if (!hard_iface)
goto out; goto out;
if (!atomic_inc_not_zero(&hard_iface->refcount)) if (!kref_get_unless_zero(&hard_iface->refcount))
hard_iface = NULL; hard_iface = NULL;
out: out:
......
...@@ -367,7 +367,7 @@ batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node, ...@@ -367,7 +367,7 @@ batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
goto out; goto out;
if (if_outgoing != BATADV_IF_DEFAULT && if (if_outgoing != BATADV_IF_DEFAULT &&
!atomic_inc_not_zero(&if_outgoing->refcount)) { !kref_get_unless_zero(&if_outgoing->refcount)) {
kfree(orig_ifinfo); kfree(orig_ifinfo);
orig_ifinfo = NULL; orig_ifinfo = NULL;
goto out; goto out;
...@@ -447,7 +447,7 @@ batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh, ...@@ -447,7 +447,7 @@ batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
if (!neigh_ifinfo) if (!neigh_ifinfo)
goto out; goto out;
if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) { if (if_outgoing && !kref_get_unless_zero(&if_outgoing->refcount)) {
kfree(neigh_ifinfo); kfree(neigh_ifinfo);
neigh_ifinfo = NULL; neigh_ifinfo = NULL;
goto out; goto out;
...@@ -524,7 +524,7 @@ batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface, ...@@ -524,7 +524,7 @@ batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
if (hardif_neigh) if (hardif_neigh)
goto out; goto out;
if (!atomic_inc_not_zero(&hard_iface->refcount)) if (!kref_get_unless_zero(&hard_iface->refcount))
goto out; goto out;
hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC); hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
...@@ -635,7 +635,7 @@ batadv_neigh_node_new(struct batadv_orig_node *orig_node, ...@@ -635,7 +635,7 @@ batadv_neigh_node_new(struct batadv_orig_node *orig_node,
if (!neigh_node) if (!neigh_node)
goto out; goto out;
if (!atomic_inc_not_zero(&hard_iface->refcount)) { if (!kref_get_unless_zero(&hard_iface->refcount)) {
kfree(neigh_node); kfree(neigh_node);
neigh_node = NULL; neigh_node = NULL;
goto out; goto out;
......
...@@ -111,7 +111,7 @@ struct batadv_hard_iface { ...@@ -111,7 +111,7 @@ struct batadv_hard_iface {
struct net_device *net_dev; struct net_device *net_dev;
u8 num_bcasts; u8 num_bcasts;
struct kobject *hardif_obj; struct kobject *hardif_obj;
atomic_t refcount; struct kref refcount;
struct packet_type batman_adv_ptype; struct packet_type batman_adv_ptype;
struct net_device *soft_iface; struct net_device *soft_iface;
struct rcu_head rcu; struct rcu_head rcu;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment