Skip to content

Commit fb778ea

Browse files
author
Marek Lindner
committed
batman-adv: protect each hash row with rcu locks
Signed-off-by: Marek Lindner <lindner_marek@yahoo.de>
1 parent a775eb8 commit fb778ea

8 files changed

Lines changed: 141 additions & 45 deletions

File tree

‎net/batman-adv/hash.c‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ static void hash_init(struct hashtable_t *hash)
2727
{
2828
int i;
2929

30-
for (i = 0 ; i < hash->size; i++)
30+
for (i = 0 ; i < hash->size; i++) {
3131
INIT_HLIST_HEAD(&hash->table[i]);
32+
spin_lock_init(&hash->list_locks[i]);
33+
}
3234
}
3335

3436
/* free only the hashtable and the hash itself. */
3537
void hash_destroy(struct hashtable_t *hash)
3638
{
39+
kfree(hash->list_locks);
3740
kfree(hash->table);
3841
kfree(hash);
3942
}
@@ -43,20 +46,33 @@ struct hashtable_t *hash_new(int size)
4346
{
4447
struct hashtable_t *hash;
4548

46-
hash = kmalloc(sizeof(struct hashtable_t) , GFP_ATOMIC);
47-
49+
hash = kmalloc(sizeof(struct hashtable_t), GFP_ATOMIC);
4850
if (!hash)
4951
return NULL;
5052

51-
hash->size = size;
5253
hash->table = kmalloc(sizeof(struct element_t *) * size, GFP_ATOMIC);
54+
if (!hash->table)
55+
goto free_hash;
5356

54-
if (!hash->table) {
55-
kfree(hash);
56-
return NULL;
57-
}
57+
hash->list_locks = kmalloc(sizeof(spinlock_t) * size, GFP_ATOMIC);
58+
if (!hash->list_locks)
59+
goto free_table;
5860

61+
hash->size = size;
5962
hash_init(hash);
60-
6163
return hash;
64+
65+
free_table:
66+
kfree(hash->table);
67+
free_hash:
68+
kfree(hash);
69+
return NULL;
70+
}
71+
72+
void bucket_free_rcu(struct rcu_head *rcu)
73+
{
74+
struct element_t *bucket;
75+
76+
bucket = container_of(rcu, struct element_t, rcu);
77+
kfree(bucket);
6278
}

‎net/batman-adv/hash.h‎

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ typedef void (*hashdata_free_cb)(void *, void *);
3939
struct element_t {
4040
void *data; /* pointer to the data */
4141
struct hlist_node hlist; /* bucket list pointer */
42+
struct rcu_head rcu;
4243
};
4344

4445
struct hashtable_t {
45-
struct hlist_head *table; /* the hashtable itself, with the buckets */
46+
struct hlist_head *table; /* the hashtable itself with the buckets */
47+
spinlock_t *list_locks; /* spinlock for each hash list entry */
4648
int size; /* size of hashtable */
4749
};
4850

@@ -52,6 +54,8 @@ struct hashtable_t *hash_new(int size);
5254
/* free only the hashtable and the hash itself. */
5355
void hash_destroy(struct hashtable_t *hash);
5456

57+
void bucket_free_rcu(struct rcu_head *rcu);
58+
5559
/* remove the hash structure. if hashdata_free_cb != NULL, this function will be
5660
* called to remove the elements inside of the hash. if you don't remove the
5761
* elements, memory might be leaked. */
@@ -61,19 +65,22 @@ static inline void hash_delete(struct hashtable_t *hash,
6165
struct hlist_head *head;
6266
struct hlist_node *walk, *safe;
6367
struct element_t *bucket;
68+
spinlock_t *list_lock; /* spinlock to protect write access */
6469
int i;
6570

6671
for (i = 0; i < hash->size; i++) {
6772
head = &hash->table[i];
73+
list_lock = &hash->list_locks[i];
6874

69-
hlist_for_each_safe(walk, safe, head) {
70-
bucket = hlist_entry(walk, struct element_t, hlist);
75+
spin_lock_bh(list_lock);
76+
hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
7177
if (free_cb)
7278
free_cb(bucket->data, arg);
7379

74-
hlist_del(walk);
75-
kfree(bucket);
80+
hlist_del_rcu(walk);
81+
call_rcu(&bucket->rcu, bucket_free_rcu);
7682
}
83+
spin_unlock_bh(list_lock);
7784
}
7885

7986
hash_destroy(hash);
@@ -88,29 +95,39 @@ static inline int hash_add(struct hashtable_t *hash,
8895
struct hlist_head *head;
8996
struct hlist_node *walk, *safe;
9097
struct element_t *bucket;
98+
spinlock_t *list_lock; /* spinlock to protect write access */
9199

92100
if (!hash)
93-
return -1;
101+
goto err;
94102

95103
index = choose(data, hash->size);
96104
head = &hash->table[index];
105+
list_lock = &hash->list_locks[index];
97106

98-
hlist_for_each_safe(walk, safe, head) {
99-
bucket = hlist_entry(walk, struct element_t, hlist);
107+
rcu_read_lock();
108+
hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
100109
if (compare(bucket->data, data))
101-
return -1;
110+
goto err_unlock;
102111
}
112+
rcu_read_unlock();
103113

104114
/* no duplicate found in list, add new element */
105115
bucket = kmalloc(sizeof(struct element_t), GFP_ATOMIC);
106-
107116
if (!bucket)
108-
return -1;
117+
goto err;
109118

110119
bucket->data = data;
111-
hlist_add_head(&bucket->hlist, head);
120+
121+
spin_lock_bh(list_lock);
122+
hlist_add_head_rcu(&bucket->hlist, head);
123+
spin_unlock_bh(list_lock);
112124

113125
return 0;
126+
127+
err_unlock:
128+
rcu_read_unlock();
129+
err:
130+
return -1;
114131
}
115132

116133
/* removes data from hash, if found. returns pointer do data on success, so you
@@ -125,25 +142,31 @@ static inline void *hash_remove(struct hashtable_t *hash,
125142
struct hlist_node *walk;
126143
struct element_t *bucket;
127144
struct hlist_head *head;
128-
void *data_save;
145+
void *data_save = NULL;
129146

130147
index = choose(data, hash->size);
131148
head = &hash->table[index];
132149

150+
spin_lock_bh(&hash->list_locks[index]);
133151
hlist_for_each_entry(bucket, walk, head, hlist) {
134152
if (compare(bucket->data, data)) {
135153
data_save = bucket->data;
136-
hlist_del(walk);
137-
kfree(bucket);
138-
return data_save;
154+
hlist_del_rcu(walk);
155+
call_rcu(&bucket->rcu, bucket_free_rcu);
156+
break;
139157
}
140158
}
159+
spin_unlock_bh(&hash->list_locks[index]);
141160

142-
return NULL;
161+
return data_save;
143162
}
144163

145-
/* finds data, based on the key in keydata. returns the found data on success,
146-
* or NULL on error */
164+
/**
165+
* finds data, based on the key in keydata. returns the found data on success,
166+
* or NULL on error
167+
*
168+
* caller must lock with rcu_read_lock() / rcu_read_unlock()
169+
**/
147170
static inline void *hash_find(struct hashtable_t *hash,
148171
hashdata_compare_cb compare,
149172
hashdata_choose_cb choose, void *keydata)
@@ -152,20 +175,22 @@ static inline void *hash_find(struct hashtable_t *hash,
152175
struct hlist_head *head;
153176
struct hlist_node *walk;
154177
struct element_t *bucket;
178+
void *bucket_data = NULL;
155179

156180
if (!hash)
157181
return NULL;
158182

159183
index = choose(keydata , hash->size);
160184
head = &hash->table[index];
161185

162-
hlist_for_each(walk, head) {
163-
bucket = hlist_entry(walk, struct element_t, hlist);
164-
if (compare(bucket->data, keydata))
165-
return bucket->data;
186+
hlist_for_each_entry(bucket, walk, head, hlist) {
187+
if (compare(bucket->data, keydata)) {
188+
bucket_data = bucket->data;
189+
break;
190+
}
166191
}
167192

168-
return NULL;
193+
return bucket_data;
169194
}
170195

171196
#endif /* _NET_BATMAN_ADV_HASH_H_ */

‎net/batman-adv/icmp_socket.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,
220220
goto dst_unreach;
221221

222222
spin_lock_bh(&bat_priv->orig_hash_lock);
223+
rcu_read_lock();
223224
orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
224225
compare_orig, choose_orig,
225226
icmp_packet->dst));
227+
rcu_read_unlock();
226228

227229
if (!orig_node)
228230
goto unlock;

‎net/batman-adv/originator.c‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,11 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
150150
int size;
151151
int hash_added;
152152

153+
rcu_read_lock();
153154
orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
154155
compare_orig, choose_orig,
155156
addr));
157+
rcu_read_unlock();
156158

157159
if (orig_node)
158160
return orig_node;
@@ -294,6 +296,7 @@ static void _purge_orig(struct bat_priv *bat_priv)
294296
struct hlist_node *walk, *safe;
295297
struct hlist_head *head;
296298
struct element_t *bucket;
299+
spinlock_t *list_lock; /* spinlock to protect write access */
297300
struct orig_node *orig_node;
298301
int i;
299302

@@ -305,22 +308,26 @@ static void _purge_orig(struct bat_priv *bat_priv)
305308
/* for all origins... */
306309
for (i = 0; i < hash->size; i++) {
307310
head = &hash->table[i];
311+
list_lock = &hash->list_locks[i];
308312

313+
spin_lock_bh(list_lock);
309314
hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
310315
orig_node = bucket->data;
311316

312317
if (purge_orig_node(bat_priv, orig_node)) {
313318
if (orig_node->gw_flags)
314319
gw_node_delete(bat_priv, orig_node);
315-
hlist_del(walk);
316-
kfree(bucket);
320+
hlist_del_rcu(walk);
321+
call_rcu(&bucket->rcu, bucket_free_rcu);
317322
free_orig_node(orig_node, bat_priv);
323+
continue;
318324
}
319325

320326
if (time_after(jiffies, orig_node->last_frag_packet +
321327
msecs_to_jiffies(FRAG_TIMEOUT)))
322328
frag_list_free(&orig_node->frag_list);
323329
}
330+
spin_unlock_bh(list_lock);
324331
}
325332

326333
spin_unlock_bh(&bat_priv->orig_hash_lock);
@@ -387,7 +394,8 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)
387394
for (i = 0; i < hash->size; i++) {
388395
head = &hash->table[i];
389396

390-
hlist_for_each_entry(bucket, walk, head, hlist) {
397+
rcu_read_lock();
398+
hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
391399
orig_node = bucket->data;
392400

393401
if (!orig_node->router)
@@ -408,17 +416,16 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)
408416
neigh_node->addr,
409417
neigh_node->if_incoming->net_dev->name);
410418

411-
rcu_read_lock();
412419
hlist_for_each_entry_rcu(neigh_node, node,
413420
&orig_node->neigh_list, list) {
414421
seq_printf(seq, " %pM (%3i)", neigh_node->addr,
415422
neigh_node->tq_avg);
416423
}
417-
rcu_read_unlock();
418424

419425
seq_printf(seq, "\n");
420426
batman_count++;
421427
}
428+
rcu_read_unlock();
422429
}
423430

424431
spin_unlock_bh(&bat_priv->orig_hash_lock);
@@ -476,18 +483,21 @@ int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
476483
for (i = 0; i < hash->size; i++) {
477484
head = &hash->table[i];
478485

479-
hlist_for_each_entry(bucket, walk, head, hlist) {
486+
rcu_read_lock();
487+
hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
480488
orig_node = bucket->data;
481489

482490
if (orig_node_add_if(orig_node, max_if_num) == -1)
483491
goto err;
484492
}
493+
rcu_read_unlock();
485494
}
486495

487496
spin_unlock_bh(&bat_priv->orig_hash_lock);
488497
return 0;
489498

490499
err:
500+
rcu_read_unlock();
491501
spin_unlock_bh(&bat_priv->orig_hash_lock);
492502
return -ENOMEM;
493503
}
@@ -562,7 +572,8 @@ int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
562572
for (i = 0; i < hash->size; i++) {
563573
head = &hash->table[i];
564574

565-
hlist_for_each_entry(bucket, walk, head, hlist) {
575+
rcu_read_lock();
576+
hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
566577
orig_node = bucket->data;
567578

568579
ret = orig_node_del_if(orig_node, max_if_num,
@@ -571,6 +582,7 @@ int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
571582
if (ret == -1)
572583
goto err;
573584
}
585+
rcu_read_unlock();
574586
}
575587

576588
/* renumber remaining batman interfaces _inside_ of orig_hash_lock */
@@ -595,6 +607,7 @@ int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
595607
return 0;
596608

597609
err:
610+
rcu_read_unlock();
598611
spin_unlock_bh(&bat_priv->orig_hash_lock);
599612
return -ENOMEM;
600613
}

0 commit comments

Comments
 (0)