Signed-off-by: Antonio Quartulli ordex@autistici.org --- translation-table.c | 19 ++++++++++++++----- types.h | 1 + 2 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/translation-table.c b/translation-table.c index 245cc9a..0f02514 100644 --- a/translation-table.c +++ b/translation-table.c @@ -152,9 +152,12 @@ static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu) static void batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry) { - /* to avoid race conditions, immediately decrease the tt counter */ - atomic_dec(&orig_entry->orig_node->tt_size); - call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu); + if (atomic_dec_and_test(&orig_entry->refcount)) { + /* to avoid race conditions, immediately decrease the tt counter + */ + atomic_dec(&orig_entry->orig_node->tt_size); + call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu); + } }
static void batadv_tt_local_event(struct batadv_priv *bat_priv, @@ -639,12 +642,17 @@ batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry, rcu_read_lock(); head = &entry->orig_list; hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) { - if (tmp_orig_entry->orig_node == orig_node) { + if (tmp_orig_entry->orig_node != orig_node) + continue; + if (!atomic_inc_not_zero(&tmp_orig_entry->refcount)) + continue; + found = true; + batadv_tt_orig_list_entry_free_ref(tmp_orig_entry); break; - } } rcu_read_unlock(); + return found; }
@@ -663,6 +671,7 @@ batadv_tt_global_add_orig_entry(struct batadv_tt_global_entry *tt_global_entry, atomic_inc(&orig_node->tt_size); orig_entry->orig_node = orig_node; orig_entry->ttvn = ttvn; + atomic_set(&orig_entry->refcount, 0);
spin_lock_bh(&tt_global_entry->list_lock); hlist_add_head_rcu(&orig_entry->list, diff --git a/types.h b/types.h index 64b4317..82b97c3 100644 --- a/types.h +++ b/types.h @@ -281,6 +281,7 @@ struct batadv_tt_global_entry { struct batadv_tt_orig_list_entry { struct batadv_orig_node *orig_node; uint8_t ttvn; + atomic_t refcount; struct rcu_head rcu; struct hlist_node list; };