提交 25b6d3c1 编写于 作者: M Marek Lindner

batman-adv: Correct rcu refcounting for gw_node

It might be possible that 2 threads access the same data in the same
rcu grace period. The first thread calls call_rcu() to decrement the
refcount and free the data while the second thread increases the
refcount to use the data. To avoid this race condition all refcount
operations have to be atomic.
Reported-by: NSven Eckelmann <sven@narfation.org>
Signed-off-by: NMarek Lindner <lindner_marek@yahoo.de>
上级 44524fcd
...@@ -28,20 +28,18 @@ ...@@ -28,20 +28,18 @@
#include <linux/udp.h> #include <linux/udp.h>
#include <linux/if_vlan.h> #include <linux/if_vlan.h>
static void gw_node_free_ref(struct kref *refcount) static void gw_node_free_rcu(struct rcu_head *rcu)
{ {
struct gw_node *gw_node; struct gw_node *gw_node;
gw_node = container_of(refcount, struct gw_node, refcount); gw_node = container_of(rcu, struct gw_node, rcu);
kfree(gw_node); kfree(gw_node);
} }
static void gw_node_free_rcu(struct rcu_head *rcu) static void gw_node_free_ref(struct gw_node *gw_node)
{ {
struct gw_node *gw_node; if (atomic_dec_and_test(&gw_node->refcount))
call_rcu(&gw_node->rcu, gw_node_free_rcu);
gw_node = container_of(rcu, struct gw_node, rcu);
kref_put(&gw_node->refcount, gw_node_free_ref);
} }
void *gw_get_selected(struct bat_priv *bat_priv) void *gw_get_selected(struct bat_priv *bat_priv)
...@@ -61,25 +59,26 @@ void gw_deselect(struct bat_priv *bat_priv) ...@@ -61,25 +59,26 @@ void gw_deselect(struct bat_priv *bat_priv)
bat_priv->curr_gw = NULL; bat_priv->curr_gw = NULL;
if (gw_node) if (gw_node)
kref_put(&gw_node->refcount, gw_node_free_ref); gw_node_free_ref(gw_node);
} }
static struct gw_node *gw_select(struct bat_priv *bat_priv, static void gw_select(struct bat_priv *bat_priv, struct gw_node *new_gw_node)
struct gw_node *new_gw_node)
{ {
struct gw_node *curr_gw_node = bat_priv->curr_gw; struct gw_node *curr_gw_node = bat_priv->curr_gw;
if (new_gw_node) if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
kref_get(&new_gw_node->refcount); new_gw_node = NULL;
bat_priv->curr_gw = new_gw_node; bat_priv->curr_gw = new_gw_node;
return curr_gw_node;
if (curr_gw_node)
gw_node_free_ref(curr_gw_node);
} }
void gw_election(struct bat_priv *bat_priv) void gw_election(struct bat_priv *bat_priv)
{ {
struct hlist_node *node; struct hlist_node *node;
struct gw_node *gw_node, *curr_gw_tmp = NULL, *old_gw_node = NULL; struct gw_node *gw_node, *curr_gw_tmp = NULL;
uint8_t max_tq = 0; uint8_t max_tq = 0;
uint32_t max_gw_factor = 0, tmp_gw_factor = 0; uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
int down, up; int down, up;
...@@ -174,14 +173,10 @@ void gw_election(struct bat_priv *bat_priv) ...@@ -174,14 +173,10 @@ void gw_election(struct bat_priv *bat_priv)
curr_gw_tmp->orig_node->gw_flags, curr_gw_tmp->orig_node->gw_flags,
curr_gw_tmp->orig_node->router->tq_avg); curr_gw_tmp->orig_node->router->tq_avg);
old_gw_node = gw_select(bat_priv, curr_gw_tmp); gw_select(bat_priv, curr_gw_tmp);
} }
rcu_read_unlock(); rcu_read_unlock();
/* the kfree() has to be outside of the rcu lock */
if (old_gw_node)
kref_put(&old_gw_node->refcount, gw_node_free_ref);
} }
void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node) void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
...@@ -242,7 +237,7 @@ static void gw_node_add(struct bat_priv *bat_priv, ...@@ -242,7 +237,7 @@ static void gw_node_add(struct bat_priv *bat_priv,
memset(gw_node, 0, sizeof(struct gw_node)); memset(gw_node, 0, sizeof(struct gw_node));
INIT_HLIST_NODE(&gw_node->list); INIT_HLIST_NODE(&gw_node->list);
gw_node->orig_node = orig_node; gw_node->orig_node = orig_node;
kref_init(&gw_node->refcount); atomic_set(&gw_node->refcount, 1);
spin_lock_bh(&bat_priv->gw_list_lock); spin_lock_bh(&bat_priv->gw_list_lock);
hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list); hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
...@@ -325,7 +320,7 @@ void gw_node_purge(struct bat_priv *bat_priv) ...@@ -325,7 +320,7 @@ void gw_node_purge(struct bat_priv *bat_priv)
gw_deselect(bat_priv); gw_deselect(bat_priv);
hlist_del_rcu(&gw_node->list); hlist_del_rcu(&gw_node->list);
call_rcu(&gw_node->rcu, gw_node_free_rcu); gw_node_free_ref(gw_node);
} }
......
...@@ -98,7 +98,7 @@ struct gw_node { ...@@ -98,7 +98,7 @@ struct gw_node {
struct hlist_node list; struct hlist_node list;
struct orig_node *orig_node; struct orig_node *orig_node;
unsigned long deleted; unsigned long deleted;
struct kref refcount; atomic_t refcount;
struct rcu_head rcu; struct rcu_head rcu;
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册