From patchwork Tue May 8 20:31:57 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Schiffer X-Patchwork-Id: 1867 Return-Path: Received: from bear.g4t3.de (h1760320.stratoserver.net [85.214.77.231]) by open-mesh.org (Postfix) with ESMTP id 7F6C5600893 for ; Tue, 8 May 2012 22:32:08 +0200 (CEST) Received: from chaos.universe-factory.net (chaos.universe-factory.net [193.34.68.222]) by bear.g4t3.de (Postfix) with ESMTP id A1C08459D8 for ; Tue, 8 May 2012 22:38:13 +0200 (CEST) Received: from avalon.eduroam.uni-luebeck.de (unknown [141.83.106.130]) (using TLSv1.1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by chaos.universe-factory.net (Postfix) with ESMTPSA id 71BD2C6527 for ; Tue, 8 May 2012 20:32:07 +0000 (UTC) From: Matthias Schiffer To: b.a.t.m.a.n@lists.open-mesh.org Date: Tue, 8 May 2012 22:31:57 +0200 Message-Id: X-Mailer: git-send-email 1.7.10.1 In-Reply-To: <201205081359.26707.lindner_marek@yahoo.de> References: <201205081359.26707.lindner_marek@yahoo.de> Subject: [B.A.T.M.A.N.] [PATCH] batman-adv: fix locking in hash_add() X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.13 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 May 2012 20:32:08 -0000 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 Acked-by: Sven Eckelmann --- hash.h | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/hash.h b/hash.h index 7bcb98f..2e0409a 100644 --- a/hash.h +++ b/hash.h @@ -109,26 +109,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; }