aboutsummaryrefslogtreecommitdiffstats
path: root/net/batman-adv/originator.c
diff options
context:
space:
mode:
authorMarek Lindner <lindner_marek@yahoo.de>2011-01-19 15:01:42 -0500
committerMarek Lindner <lindner_marek@yahoo.de>2011-03-05 06:49:59 -0500
commit16b1aba849eeb45d51a5de731cf103143439ffe1 (patch)
tree2bbda8638bd925014e8d6025d704cb2860d28606 /net/batman-adv/originator.c
parentfb778ea173fcd58b8fc3d75c674f07fab187b55f (diff)
batman-adv: protect originator nodes with reference counters
Signed-off-by: Marek Lindner <lindner_marek@yahoo.de>
Diffstat (limited to 'net/batman-adv/originator.c')
-rw-r--r--net/batman-adv/originator.c61
1 files changed, 52 insertions, 9 deletions
diff --git a/net/batman-adv/originator.c b/net/batman-adv/originator.c
index 5c32314f8279..fcdb0b7fe586 100644
--- a/net/batman-adv/originator.c
+++ b/net/batman-adv/originator.c
@@ -103,12 +103,13 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node,
103 return neigh_node; 103 return neigh_node;
104} 104}
105 105
106static void free_orig_node(void *data, void *arg) 106void orig_node_free_ref(struct kref *refcount)
107{ 107{
108 struct hlist_node *node, *node_tmp; 108 struct hlist_node *node, *node_tmp;
109 struct neigh_node *neigh_node; 109 struct neigh_node *neigh_node;
110 struct orig_node *orig_node = (struct orig_node *)data; 110 struct orig_node *orig_node;
111 struct bat_priv *bat_priv = (struct bat_priv *)arg; 111
112 orig_node = container_of(refcount, struct orig_node, refcount);
112 113
113 spin_lock_bh(&orig_node->neigh_list_lock); 114 spin_lock_bh(&orig_node->neigh_list_lock);
114 115
@@ -122,7 +123,8 @@ static void free_orig_node(void *data, void *arg)
122 spin_unlock_bh(&orig_node->neigh_list_lock); 123 spin_unlock_bh(&orig_node->neigh_list_lock);
123 124
124 frag_list_free(&orig_node->frag_list); 125 frag_list_free(&orig_node->frag_list);
125 hna_global_del_orig(bat_priv, orig_node, "originator timed out"); 126 hna_global_del_orig(orig_node->bat_priv, orig_node,
127 "originator timed out");
126 128
127 kfree(orig_node->bcast_own); 129 kfree(orig_node->bcast_own);
128 kfree(orig_node->bcast_own_sum); 130 kfree(orig_node->bcast_own_sum);
@@ -131,17 +133,53 @@ static void free_orig_node(void *data, void *arg)
131 133
132void originator_free(struct bat_priv *bat_priv) 134void originator_free(struct bat_priv *bat_priv)
133{ 135{
134 if (!bat_priv->orig_hash) 136 struct hashtable_t *hash = bat_priv->orig_hash;
137 struct hlist_node *walk, *safe;
138 struct hlist_head *head;
139 struct element_t *bucket;
140 spinlock_t *list_lock; /* spinlock to protect write access */
141 struct orig_node *orig_node;
142 int i;
143
144 if (!hash)
135 return; 145 return;
136 146
137 cancel_delayed_work_sync(&bat_priv->orig_work); 147 cancel_delayed_work_sync(&bat_priv->orig_work);
138 148
139 spin_lock_bh(&bat_priv->orig_hash_lock); 149 spin_lock_bh(&bat_priv->orig_hash_lock);
140 hash_delete(bat_priv->orig_hash, free_orig_node, bat_priv);
141 bat_priv->orig_hash = NULL; 150 bat_priv->orig_hash = NULL;
151
152 for (i = 0; i < hash->size; i++) {
153 head = &hash->table[i];
154 list_lock = &hash->list_locks[i];
155
156 spin_lock_bh(list_lock);
157 hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
158 orig_node = bucket->data;
159
160 hlist_del_rcu(walk);
161 call_rcu(&bucket->rcu, bucket_free_rcu);
162 kref_put(&orig_node->refcount, orig_node_free_ref);
163 }
164 spin_unlock_bh(list_lock);
165 }
166
167 hash_destroy(hash);
142 spin_unlock_bh(&bat_priv->orig_hash_lock); 168 spin_unlock_bh(&bat_priv->orig_hash_lock);
143} 169}
144 170
171static void bucket_free_orig_rcu(struct rcu_head *rcu)
172{
173 struct element_t *bucket;
174 struct orig_node *orig_node;
175
176 bucket = container_of(rcu, struct element_t, rcu);
177 orig_node = bucket->data;
178
179 kref_put(&orig_node->refcount, orig_node_free_ref);
180 kfree(bucket);
181}
182
145/* this function finds or creates an originator entry for the given 183/* this function finds or creates an originator entry for the given
146 * address if it does not exits */ 184 * address if it does not exits */
147struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr) 185struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
@@ -156,8 +194,10 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
156 addr)); 194 addr));
157 rcu_read_unlock(); 195 rcu_read_unlock();
158 196
159 if (orig_node) 197 if (orig_node) {
198 kref_get(&orig_node->refcount);
160 return orig_node; 199 return orig_node;
200 }
161 201
162 bat_dbg(DBG_BATMAN, bat_priv, 202 bat_dbg(DBG_BATMAN, bat_priv,
163 "Creating new originator: %pM\n", addr); 203 "Creating new originator: %pM\n", addr);
@@ -168,7 +208,9 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
168 208
169 INIT_HLIST_HEAD(&orig_node->neigh_list); 209 INIT_HLIST_HEAD(&orig_node->neigh_list);
170 spin_lock_init(&orig_node->neigh_list_lock); 210 spin_lock_init(&orig_node->neigh_list_lock);
211 kref_init(&orig_node->refcount);
171 212
213 orig_node->bat_priv = bat_priv;
172 memcpy(orig_node->orig, addr, ETH_ALEN); 214 memcpy(orig_node->orig, addr, ETH_ALEN);
173 orig_node->router = NULL; 215 orig_node->router = NULL;
174 orig_node->hna_buff = NULL; 216 orig_node->hna_buff = NULL;
@@ -197,6 +239,8 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
197 if (hash_added < 0) 239 if (hash_added < 0)
198 goto free_bcast_own_sum; 240 goto free_bcast_own_sum;
199 241
242 /* extra reference for return */
243 kref_get(&orig_node->refcount);
200 return orig_node; 244 return orig_node;
201free_bcast_own_sum: 245free_bcast_own_sum:
202 kfree(orig_node->bcast_own_sum); 246 kfree(orig_node->bcast_own_sum);
@@ -318,8 +362,7 @@ static void _purge_orig(struct bat_priv *bat_priv)
318 if (orig_node->gw_flags) 362 if (orig_node->gw_flags)
319 gw_node_delete(bat_priv, orig_node); 363 gw_node_delete(bat_priv, orig_node);
320 hlist_del_rcu(walk); 364 hlist_del_rcu(walk);
321 call_rcu(&bucket->rcu, bucket_free_rcu); 365 call_rcu(&bucket->rcu, bucket_free_orig_rcu);
322 free_orig_node(orig_node, bat_priv);
323 continue; 366 continue;
324 } 367 }
325 368