Skip to content

Commit

Permalink
batman-adv: fix locking in hash_add()
Browse files Browse the repository at this point in the history
To ensure an entry isn't added twice all comparisons have to be protected by the
hash line write spinlock. This doesn't really hurt as the case that it is tried
to add an element already present to the hash shouldn't occur very often, so in
most cases the lock would have have to be taken anyways.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
Acked-by: Sven Eckelmann <sven@narfation.org>
Signed-off-by: Sven Eckelmann <sven@narfation.org>
  • Loading branch information
Matthias Schiffer authored and Antonio Quartulli committed Jun 18, 2012
1 parent ef3a409 commit 75c5a2e
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions net/batman-adv/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,23 @@ static inline int hash_add(struct hashtable_t *hash,
head = &hash->table[index];
list_lock = &hash->list_locks[index];

rcu_read_lock();
__hlist_for_each_rcu(node, head) {
spin_lock_bh(list_lock);

hlist_for_each(node, head) {
if (!compare(node, data))
continue;

ret = 1;
goto err_unlock;
goto unlock;
}
rcu_read_unlock();

/* no duplicate found in list, add new element */
spin_lock_bh(list_lock);
hlist_add_head_rcu(data_node, head);
spin_unlock_bh(list_lock);

ret = 0;
goto out;

err_unlock:
rcu_read_unlock();
unlock:
spin_unlock_bh(list_lock);
out:
return ret;
}
Expand Down

0 comments on commit 75c5a2e

Please sign in to comment.