Skip to contentSkip to conversation

batman-adv: Define the size of hashtables by hash bits

RFC Superseded Sven Eckelmann 1 participant 3 messages Mar 1, 2015 Kbuild

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

RFC v3 1/3 batman-adv: Define the size of hashtables by hash bits

The common hashtable implementation in the kernel uses bits of the hash to compute the final size of the hastable. Similar can be done for the partially locked, concurrent hashtables in batman-adv. The requirement of sizes with power two will allow better compiler optimization in the future.

---
v3:
- Rebased on master

 bridge_loop_avoidance.c | 6 ++++--
 distributed-arp-table.c | 2 +-
 main.h                  | 9 +++++++++
 network-coding.c        | 7 +++++--
 originator.c            | 2 +-
 translation-table.c     | 8 ++++++--
 6 files changed, 26 insertions(+), 8 deletions(-)
bridge_loop_avoidance.c +4 -2
index 6927589..c999d6b 100644
@@ -1220,6 +1220,8 @@ int batadv_bla_init(struct batadv_priv *bat_priv)
 	struct batadv_hard_iface *primary_if;
 	uint16_t crc;
 	unsigned long entrytime;
+	uint32_t hash_claim_size = 1u << BATADV_BLA_CLAIM_HASH_BITS;
+	uint32_t hash_backbone_size = 1u << BATADV_BLA_BACKBONE_HASH_BITS;
 
 	spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
 
@@ -1246,8 +1248,8 @@ int batadv_bla_init(struct batadv_priv *bat_priv)
 	if (bat_priv->bla.claim_hash)
 		return 0;
 
-	bat_priv->bla.claim_hash = batadv_hash_new(128);
-	bat_priv->bla.backbone_hash = batadv_hash_new(32);
+	bat_priv->bla.claim_hash = batadv_hash_new(hash_claim_size);
+	bat_priv->bla.backbone_hash = batadv_hash_new(hash_backbone_size);
 
 	if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
 		return -ENOMEM;
distributed-arp-table.c +1 -1
index 107ad62..87ab637 100644
@@ -725,7 +725,7 @@ int batadv_dat_init(struct batadv_priv *bat_priv)
 	if (bat_priv->dat.hash)
 		return 0;
 
-	bat_priv->dat.hash = batadv_hash_new(1024);
+	bat_priv->dat.hash = batadv_hash_new(1u << BATADV_DAT_HASH_BITS);
 
 	if (!bat_priv->dat.hash)
 		return -ENOMEM;
main.h +9 -0
index 4deacfd..26a20ae 100644
@@ -154,6 +154,15 @@ enum batadv_uev_type {
 #define BATADV_DAT_CANDIDATE_NOT_FOUND	0
 #define BATADV_DAT_CANDIDATE_ORIG	1
 
+#define BATADV_BLA_CLAIM_HASH_BITS 7
+#define BATADV_BLA_BACKBONE_HASH_BITS 5
+#define BATADV_NC_CODING_HASH_BITS 7
+#define BATADV_NC_DECODING_HASH_BITS 7
+#define BATADV_DAT_HASH_BITS 10
+#define BATADV_ORIG_HASH_BITS 10
+#define BATADV_TT_LOCAL_HASH_BITS 10
+#define BATADV_TT_GLOBAL_HASH_BITS 10
+
 /* Debug Messages */
 #ifdef pr_fmt
 #undef pr_fmt
network-coding.c +5 -2
index d128c3b..b70c34e 100644
@@ -116,20 +116,23 @@ static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  */
 int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
 {
+	uint32_t hash_coding_size = 1u << BATADV_NC_CODING_HASH_BITS;
+	uint32_t hash_decoding_size = 1u << BATADV_NC_DECODING_HASH_BITS;
+
 	bat_priv->nc.timestamp_fwd_flush = jiffies;
 	bat_priv->nc.timestamp_sniffed_purge = jiffies;
 
 	if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
 		return 0;
 
-	bat_priv->nc.coding_hash = batadv_hash_new(128);
+	bat_priv->nc.coding_hash = batadv_hash_new(hash_coding_size);
 	if (!bat_priv->nc.coding_hash)
 		goto err;
 
 	batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
 				   &batadv_nc_coding_hash_lock_class_key);
 
-	bat_priv->nc.decoding_hash = batadv_hash_new(128);
+	bat_priv->nc.decoding_hash = batadv_hash_new(hash_decoding_size);
 	if (!bat_priv->nc.decoding_hash)
 		goto err;
 
originator.c +1 -1
index 90e805a..34ca176 100644
@@ -130,7 +130,7 @@ int batadv_originator_init(struct batadv_priv *bat_priv)
 	if (bat_priv->orig_hash)
 		return 0;
 
-	bat_priv->orig_hash = batadv_hash_new(1024);
+	bat_priv->orig_hash = batadv_hash_new(1u << BATADV_ORIG_HASH_BITS);
 
 	if (!bat_priv->orig_hash)
 		goto err;
translation-table.c +6 -2
index b20812b..1562e26 100644
@@ -460,10 +460,12 @@ static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
 
 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
 {
+	uint32_t hash_size = 1u << BATADV_TT_LOCAL_HASH_BITS;
+
 	if (bat_priv->tt.local_hash)
 		return 0;
 
-	bat_priv->tt.local_hash = batadv_hash_new(1024);
+	bat_priv->tt.local_hash = batadv_hash_new(hash_size);
 
 	if (!bat_priv->tt.local_hash)
 		return -ENOMEM;
@@ -1158,10 +1160,12 @@ static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
 
 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
 {
+	uint32_t hash_size = 1u << BATADV_TT_GLOBAL_HASH_BITS;
+
 	if (bat_priv->tt.global_hash)
 		return 0;
 
-	bat_priv->tt.global_hash = batadv_hash_new(1024);
+	bat_priv->tt.global_hash = batadv_hash_new(hash_size);
 
 	if (!bat_priv->tt.global_hash)
 		return -ENOMEM;
Signature · 1 line
2.1.4
#1 lore raw

RFC v3 2/3 batman-adv: Remove size information from hash table

The size of an hashtable is known by the user of the hash and doesn't have to be stored inside the batadv_hashtable structure. This is an intermediate step before the size information is extracted by the compiler instead of manually added by the code.

---
v3:
- Rebased on master

 bat_iv_ogm.c            |  8 ++++---
 bridge_loop_avoidance.c | 32 +++++++++++++++-----------
 distributed-arp-table.c | 12 +++++-----
 hash.c                  | 11 +++++----
 hash.h                  | 13 ++++++-----
 network-coding.c        | 44 +++++++++++++++++++++++++-----------
 originator.c            |  9 ++++----
 originator.h            |  2 +-
 translation-table.c     | 60 ++++++++++++++++++++++++++++---------------------
 9 files changed, 116 insertions(+), 75 deletions(-)
bat_iv_ogm.c +5 -3
index 554e0e0..7b30cd4 100644
@@ -245,7 +245,9 @@ batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr)
 	if (!orig_node->bat_iv.bcast_own_sum)
 		goto free_orig_node;
 
-	hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
+	hash_added = batadv_hash_add(bat_priv->orig_hash,
+				     1u << BATADV_ORIG_HASH_BITS,
+				     batadv_compare_orig,
 				     batadv_choose_orig, orig_node,
 				     &orig_node->hash_entry);
 	if (hash_added != 0)
@@ -871,7 +873,7 @@ batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
 	uint8_t *w;
 	int if_num;
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_ORIG_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
@@ -1812,7 +1814,7 @@ static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
 		   "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
 		   "Nexthop", "outgoingIF", "Potential nexthops");
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_ORIG_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
bridge_loop_avoidance.c +19 -13
index c999d6b..d19f7c2 100644
@@ -142,7 +142,7 @@ static struct batadv_bla_claim
 	if (!hash)
 		return NULL;
 
-	index = batadv_choose_claim(data, hash->size);
+	index = batadv_choose_claim(data, 1u << BATADV_BLA_CLAIM_HASH_BITS);
 	head = &hash->table[index];
 
 	rcu_read_lock();
@@ -178,6 +178,7 @@ batadv_backbone_hash_find(struct batadv_priv *bat_priv,
 	struct batadv_bla_backbone_gw search_entry, *backbone_gw;
 	struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
 	int index;
+	uint32_t hash_size =  1u << BATADV_BLA_BACKBONE_HASH_BITS;
 
 	if (!hash)
 		return NULL;
@@ -185,7 +186,7 @@ batadv_backbone_hash_find(struct batadv_priv *bat_priv,
 	ether_addr_copy(search_entry.orig, addr);
 	search_entry.vid = vid;
 
-	index = batadv_choose_backbone_gw(&search_entry, hash->size);
+	index = batadv_choose_backbone_gw(&search_entry, hash_size);
 	head = &hash->table[index];
 
 	rcu_read_lock();
@@ -220,7 +221,7 @@ batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
 	if (!hash)
 		return;
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_BLA_CLAIM_HASH_BITS; i++) {
 		head = &hash->table[i];
 		list_lock = &hash->list_locks[i];
 
@@ -393,6 +394,7 @@ batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig,
 	atomic_set(&entry->refcount, 2);
 
 	hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
+				     1u << BATADV_BLA_BACKBONE_HASH_BITS,
 				     batadv_compare_backbone_gw,
 				     batadv_choose_backbone_gw, entry,
 				     &entry->hash_entry);
@@ -472,7 +474,7 @@ static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
 		return;
 
 	hash = bat_priv->bla.claim_hash;
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_BLA_CLAIM_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
@@ -576,6 +578,7 @@ static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
 			   "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
 			   mac, BATADV_PRINT_VID(vid));
 		hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
+					     1u << BATADV_BLA_CLAIM_HASH_BITS,
 					     batadv_compare_claim,
 					     batadv_choose_claim, claim,
 					     &claim->hash_entry);
@@ -626,8 +629,9 @@ static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
 	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
 		   mac, BATADV_PRINT_VID(vid));
 
-	batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
-			   batadv_choose_claim, claim);
+	batadv_hash_remove(bat_priv->bla.claim_hash,
+			   1u << BATADV_BLA_CLAIM_HASH_BITS,
+			   batadv_compare_claim, batadv_choose_claim, claim);
 	batadv_claim_free_ref(claim); /* reference from the hash is gone */
 
 	claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
@@ -994,7 +998,7 @@ static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
 	if (!hash)
 		return;
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_BLA_BACKBONE_HASH_BITS; i++) {
 		head = &hash->table[i];
 		list_lock = &hash->list_locks[i];
 
@@ -1047,7 +1051,7 @@ static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
 	if (!hash)
 		return;
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_BLA_CLAIM_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
@@ -1110,7 +1114,7 @@ void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
 	if (!hash)
 		return;
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_BLA_BACKBONE_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
@@ -1163,7 +1167,7 @@ static void batadv_bla_periodic_work(struct work_struct *work)
 	if (!hash)
 		goto out;
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_BLA_BACKBONE_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
@@ -1255,8 +1259,10 @@ int batadv_bla_init(struct batadv_priv *bat_priv)
 		return -ENOMEM;
 
 	batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
+				   1u << BATADV_BLA_CLAIM_HASH_BITS,
 				   &batadv_claim_hash_lock_class_key);
 	batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
+				   1u << BATADV_BLA_CLAIM_HASH_BITS,
 				   &batadv_backbone_hash_lock_class_key);
 
 	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
@@ -1362,7 +1368,7 @@ bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig,
 	if (!hash)
 		return false;
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_BLA_BACKBONE_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
@@ -1642,7 +1648,7 @@ int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
 		   ntohs(bat_priv->bla.claim_dest.group));
 	seq_printf(seq, "   %-17s    %-5s    %-17s [o] (%-6s)\n",
 		   "Client", "VID", "Originator", "CRC");
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_BLA_CLAIM_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
@@ -1687,7 +1693,7 @@ int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
 		   ntohs(bat_priv->bla.claim_dest.group));
 	seq_printf(seq, "   %-17s    %-5s %-9s (%-6s)\n",
 		   "Originator", "VID", "last seen", "CRC");
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_BLA_BACKBONE_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
distributed-arp-table.c +7 -5
index 87ab637..a027e75 100644
@@ -88,7 +88,7 @@ static void __batadv_dat_purge(struct batadv_priv *bat_priv,
 	if (!bat_priv->dat.hash)
 		return;
 
-	for (i = 0; i < bat_priv->dat.hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_DAT_HASH_BITS; i++) {
 		head = &bat_priv->dat.hash->table[i];
 		list_lock = &bat_priv->dat.hash->list_locks[i];
 
@@ -254,7 +254,7 @@ batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
 	to_find.ip = ip;
 	to_find.vid = vid;
 
-	index = batadv_hash_dat(&to_find, hash->size);
+	index = batadv_hash_dat(&to_find, 1u << BATADV_DAT_HASH_BITS);
 	head = &hash->table[index];
 
 	rcu_read_lock();
@@ -309,7 +309,9 @@ static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
 	dat_entry->last_update = jiffies;
 	atomic_set(&dat_entry->refcount, 2);
 
-	hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
+	hash_added = batadv_hash_add(bat_priv->dat.hash,
+				     1u << BATADV_DAT_HASH_BITS,
+				     batadv_compare_dat,
 				     batadv_hash_dat, dat_entry,
 				     &dat_entry->hash_entry);
 
@@ -492,7 +494,7 @@ static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
 	/* iterate over the originator list and find the node with the closest
 	 * dat_address which has not been selected yet
 	 */
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_ORIG_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
@@ -778,7 +780,7 @@ int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
 	seq_printf(seq, "          %-7s          %-9s %4s %11s\n", "IPv4",
 		   "MAC", "VID", "last-seen");
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_DAT_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
hash.c +5 -6
index 7c1c630..8e8d934 100644
@@ -19,11 +19,11 @@
 #include "hash.h"
 
 /* clears the hash */
-static void batadv_hash_init(struct batadv_hashtable *hash)
+static void batadv_hash_init(struct batadv_hashtable *hash, uint32_t size)
 {
 	uint32_t i;
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < size; i++) {
 		INIT_HLIST_HEAD(&hash->table[i]);
 		spin_lock_init(&hash->list_locks[i]);
 	}
@@ -55,8 +55,7 @@ struct batadv_hashtable *batadv_hash_new(uint32_t size)
 	if (!hash->list_locks)
 		goto free_table;
 
-	hash->size = size;
-	batadv_hash_init(hash);
+	batadv_hash_init(hash, size);
 	return hash;
 
 free_table:
@@ -66,11 +65,11 @@ free_hash:
 	return NULL;
 }
 
-void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
+void batadv_hash_set_lock_class(struct batadv_hashtable *hash, uint32_t size,
 				struct lock_class_key *key)
 {
 	uint32_t i;
 
-	for (i = 0; i < hash->size; i++)
+	for (i = 0; i < size; i++)
 		lockdep_set_class(&hash->list_locks[i], key);
 }
hash.h +8 -5
index a1d0980..c88166b 100644
@@ -36,14 +36,13 @@ typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
 struct batadv_hashtable {
 	struct hlist_head *table;   /* the hashtable itself with the buckets */
 	spinlock_t *list_locks;     /* spinlock for each hash list entry */
-	uint32_t size;		    /* size of hashtable */
 };
 
 /* allocates and clears the hash */
 struct batadv_hashtable *batadv_hash_new(uint32_t size);
 
 /* set class key for all locks */
-void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
+void batadv_hash_set_lock_class(struct batadv_hashtable *hash, uint32_t size,
 				struct lock_class_key *key);
 
 /* free only the hashtable and the hash itself. */
@@ -54,6 +53,7 @@ void batadv_hash_destroy(struct batadv_hashtable *hash);
  * elements, memory might be leaked.
  */
 static inline void batadv_hash_delete(struct batadv_hashtable *hash,
+				      uint32_t size,
 				      batadv_hashdata_free_cb free_cb,
 				      void *arg)
 {
@@ -62,7 +62,7 @@ static inline void batadv_hash_delete(struct batadv_hashtable *hash,
 	spinlock_t *list_lock; /* spinlock to protect write access */
 	uint32_t i;
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < size; i++) {
 		head = &hash->table[i];
 		list_lock = &hash->list_locks[i];
 
@@ -82,6 +82,7 @@ static inline void batadv_hash_delete(struct batadv_hashtable *hash,
 /**
  *	batadv_hash_add - adds data to the hashtable
  *	@hash: storage hash table
+ *	@size: size of the hashtable
  *	@compare: callback to determine if 2 hash elements are identical
  *	@choose: callback calculating the hash index
  *	@data: data passed to the aforementioned callbacks as argument
@@ -91,6 +92,7 @@ static inline void batadv_hash_delete(struct batadv_hashtable *hash,
  *	and -1 on error.
  */
 static inline int batadv_hash_add(struct batadv_hashtable *hash,
+				  uint32_t size,
 				  batadv_hashdata_compare_cb compare,
 				  batadv_hashdata_choose_cb choose,
 				  const void *data,
@@ -105,7 +107,7 @@ static inline int batadv_hash_add(struct batadv_hashtable *hash,
 	if (!hash)
 		goto out;
 
-	index = choose(data, hash->size);
+	index = choose(data, size);
 	head = &hash->table[index];
 	list_lock = &hash->list_locks[index];
 
@@ -136,6 +138,7 @@ out:
  * comparing.
  */
 static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
+				       uint32_t size,
 				       batadv_hashdata_compare_cb compare,
 				       batadv_hashdata_choose_cb choose,
 				       void *data)
@@ -145,7 +148,7 @@ static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
 	struct hlist_head *head;
 	void *data_save = NULL;
 
-	index = choose(data, hash->size);
+	index = choose(data, size);
 	head = &hash->table[index];
 
 	spin_lock_bh(&hash->list_locks[index]);
network-coding.c +31 -13

196 lines not on this page · raw message

originator.c +5 -4
index 34ca176..55baa3d 100644
@@ -136,6 +136,7 @@ int batadv_originator_init(struct batadv_priv *bat_priv)
 		goto err;
 
 	batadv_hash_set_lock_class(bat_priv->orig_hash,
+				   1u << BATADV_ORIG_HASH_BITS,
 				   &batadv_orig_hash_lock_class_key);
 
 	INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
@@ -615,7 +616,7 @@ void batadv_originator_free(struct batadv_priv *bat_priv)
 
 	bat_priv->orig_hash = NULL;
 
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_ORIG_HASH_BITS; i++) {
 		head = &hash->table[i];
 		list_lock = &hash->list_locks[i];
 
@@ -964,7 +965,7 @@ static void _batadv_purge_orig(struct batadv_priv *bat_priv)
 		return;
 
 	/* for all origins... */
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_ORIG_HASH_BITS; i++) {
 		head = &hash->table[i];
 		list_lock = &hash->list_locks[i];
 
@@ -1098,7 +1099,7 @@ int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
 	/* resize all orig nodes because orig_node->bcast_own(_sum) depend on
 	 * if_num
 	 */
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_ORIG_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
@@ -1135,7 +1136,7 @@ int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
 	/* resize all orig nodes because orig_node->bcast_own(_sum) depend on
 	 * if_num
 	 */
-	for (i = 0; i < hash->size; i++) {
+	for (i = 0; i < 1u << BATADV_ORIG_HASH_BITS; i++) {
 		head = &hash->table[i];
 
 		rcu_read_lock();
originator.h +1 -1
index a179c03..89b810d 100644
@@ -92,7 +92,7 @@ batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data)
 	if (!hash)
 		return NULL;
 
-	index = batadv_choose_orig(data, hash->size);
+	index = batadv_choose_orig(data, 1u << BATADV_ORIG_HASH_BITS);
 	head = &hash->table[index];
 
 	rcu_read_lock();
translation-table.c +35 -25

210 lines not on this page · raw message

Signature · 1 line
2.1.4
#2 lore raw

RFC v3 3/3 batman-adv: Remove wrapper structure for hash tables

In reply to Sven Eckelmann

From: "[email protected]" <[email protected]>

It is unnecessary to allocate an extra memory region for hashtables and the corresponding locks. This brings the hashes used in batman-adv slightly in the direction of the common statically sized hash table implementation. More common hashtable functionality cannot be used batman-adv wide because the simple hashtable and rhashtable implementations don't provide bucket based locking.

A sideeffect of this change is the initialization of each array of locks for each hashtable with a different lock_class. This allows to correct nesting of write access to two different hashtables without triggering a lockdep warning.

---
v3:
- Rebased on master
- combine hlist_head and spinlock_t in own mini structure

The only thing I don't like at the moment are these checks whether hash was initialized/not initialized. Right now they are just removed because this check doesn't make sense anymore.

 Makefile.kbuild         |   1 -
 bat_iv_ogm.c            |  13 ++--
 bridge_loop_avoidance.c | 139 +++++++++++++--------------------------
 distributed-arp-table.c |  52 ++++-----------
 hash.c                  |  75 ---------------------
 hash.h                  |  87 ++++++++++++-------------
 network-coding.c        | 134 ++++++++++++++------------------------
 originator.c            |  56 +++++-----------
 originator.h            |   9 +--
 translation-table.c     | 168 ++++++++++++++++--------------------------------
 types.h                 |  27 +++++---
 11 files changed, 249 insertions(+), 512 deletions(-)
 delete mode 100644 hash.c
Makefile.kbuild +0 -1
index 6903703..6ee8b04 100644
@@ -26,7 +26,6 @@ batman-adv-y += fragmentation.o
 batman-adv-y += gateway_client.o
 batman-adv-y += gateway_common.o
 batman-adv-y += hard-interface.o
-batman-adv-y += hash.o
 batman-adv-y += icmp_socket.o
 batman-adv-y += main.o
 batman-adv-$(CONFIG_BATMAN_ADV_MCAST) += multicast.o
bat_iv_ogm.c +6 -7
index 7b30cd4..60bb3d7 100644
@@ -246,7 +246,6 @@ batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr)
 		goto free_orig_node;
 
 	hash_added = batadv_hash_add(bat_priv->orig_hash,
-				     1u << BATADV_ORIG_HASH_BITS,
 				     batadv_compare_orig,
 				     batadv_choose_orig, orig_node,
 				     &orig_node->hash_entry);
@@ -864,7 +863,7 @@ static void
 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
 {
 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
-	struct batadv_hashtable *hash = bat_priv->orig_hash;
+	struct batadv_bucket *hash = bat_priv->orig_hash;
 	struct hlist_head *head;
 	struct batadv_orig_node *orig_node;
 	unsigned long *word;
@@ -873,8 +872,8 @@ batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
 	uint8_t *w;
 	int if_num;
 
-	for (i = 0; i < 1u << BATADV_ORIG_HASH_BITS; i++) {
-		head = &hash->table[i];
+	for (i = 0; i < ARRAY_SIZE(bat_priv->orig_hash); i++) {
+		head = &hash[i].head;
 
 		rcu_read_lock();
 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
@@ -1801,7 +1800,7 @@ static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
 				     struct batadv_hard_iface *if_outgoing)
 {
 	struct batadv_neigh_node *neigh_node;
-	struct batadv_hashtable *hash = bat_priv->orig_hash;
+	struct batadv_bucket *hash = bat_priv->orig_hash;
 	int last_seen_msecs, last_seen_secs;
 	struct batadv_orig_node *orig_node;
 	struct batadv_neigh_ifinfo *n_ifinfo;
@@ -1814,8 +1813,8 @@ static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
 		   "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
 		   "Nexthop", "outgoingIF", "Potential nexthops");
 
-	for (i = 0; i < 1u << BATADV_ORIG_HASH_BITS; i++) {
-		head = &hash->table[i];
+	for (i = 0; i < ARRAY_SIZE(bat_priv->orig_hash); i++) {
+		head = &hash[i].head;
 
 		rcu_read_lock();
 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
bridge_loop_avoidance.c +44 -95

315 lines not on this page · raw message

distributed-arp-table.c +14 -38

118 lines not on this page · raw message

hash.c deleted +0 -75

75 lines not on this page · raw message

hash.h +44 -43

151 lines not on this page · raw message

network-coding.c +47 -87

318 lines not on this page · raw message

originator.c +15 -41

131 lines not on this page · raw message

originator.h +3 -6
index 89b810d..e0eb8fc 100644
@@ -84,16 +84,13 @@ static inline uint32_t batadv_choose_orig(const void *data, uint32_t size)
 static inline struct batadv_orig_node *
 batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data)
 {
-	struct batadv_hashtable *hash = bat_priv->orig_hash;
+	struct batadv_bucket *hash = bat_priv->orig_hash;
 	struct hlist_head *head;
 	struct batadv_orig_node *orig_node, *orig_node_tmp = NULL;
 	int index;
 
-	if (!hash)
-		return NULL;
-
-	index = batadv_choose_orig(data, 1u << BATADV_ORIG_HASH_BITS);
-	head = &hash->table[index];
+	index = batadv_choose_orig(data, ARRAY_SIZE(bat_priv->orig_hash));
+	head = &hash[index].head;
 
 	rcu_read_lock();
 	hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
translation-table.c +57 -111

404 lines not on this page · raw message

types.h +19 -8
index de09807..60fb1b5 100644
@@ -35,6 +35,16 @@
 #endif /* CONFIG_BATMAN_ADV_DAT */
 
 /**
+ * struct batadv_bucket - bucket for concurrent, list based hashtable
+ * @head: pointer to the data of this bucket
+ * @lock: lock protecting writes to this bucket
+ */
+struct batadv_bucket {
+	struct hlist_head head;
+	spinlock_t lock; /* Protects head */
+};
+
+/**
  * enum batadv_dhcp_recipient - dhcp destination
  * @BATADV_DHCP_NO: packet is not a dhcp message
  * @BATADV_DHCP_TO_SERVER: dhcp message is directed to a server
@@ -517,8 +527,8 @@ struct batadv_priv_tt {
 	atomic_t ogm_append_cnt;
 	atomic_t local_changes;
 	struct list_head changes_list;
-	struct batadv_hashtable *local_hash;
-	struct batadv_hashtable *global_hash;
+	struct batadv_bucket local_hash[1u << BATADV_TT_LOCAL_HASH_BITS];
+	struct batadv_bucket global_hash[1u << BATADV_TT_GLOBAL_HASH_BITS];
 	struct list_head req_list;
 	struct list_head roam_list;
 	spinlock_t changes_list_lock; /* protects changes */
@@ -548,8 +558,8 @@ struct batadv_priv_tt {
 #ifdef CONFIG_BATMAN_ADV_BLA
 struct batadv_priv_bla {
 	atomic_t num_requests;
-	struct batadv_hashtable *claim_hash;
-	struct batadv_hashtable *backbone_hash;
+	struct batadv_bucket claim_hash[1u << BATADV_BLA_CLAIM_HASH_BITS];
+	struct batadv_bucket backbone_hash[1u << BATADV_BLA_BACKBONE_HASH_BITS];
 	struct batadv_bcast_duplist_entry bcast_duplist[BATADV_DUPLIST_SIZE];
 	int bcast_duplist_curr;
 	/* protects bcast_duplist & bcast_duplist_curr */
@@ -613,12 +623,13 @@ struct batadv_priv_tvlv {
  * struct batadv_priv_dat - per mesh interface DAT private data
  * @addr: node DAT address
  * @hash: hashtable representing the local ARP cache
+ * @hash_lock: locks for each hashtable buckets
  * @work: work queue callback item for cache purging
  */
 #ifdef CONFIG_BATMAN_ADV_DAT
 struct batadv_priv_dat {
 	batadv_dat_addr_t addr;
-	struct batadv_hashtable *hash;
+	struct batadv_bucket hash[1u << BATADV_DAT_HASH_BITS];
 	struct delayed_work work;
 };
 #endif
@@ -681,8 +692,8 @@ struct batadv_priv_nc {
 	u32 max_buffer_time;
 	unsigned long timestamp_fwd_flush;
 	unsigned long timestamp_sniffed_purge;
-	struct batadv_hashtable *coding_hash;
-	struct batadv_hashtable *decoding_hash;
+	struct batadv_bucket coding_hash[1u << BATADV_NC_CODING_HASH_BITS];
+	struct batadv_bucket decoding_hash[1u << BATADV_NC_DECODING_HASH_BITS];
 };
 
 /**
@@ -795,7 +806,7 @@ struct batadv_priv {
 	struct dentry *debug_dir;
 	struct hlist_head forw_bat_list;
 	struct hlist_head forw_bcast_list;
-	struct batadv_hashtable *orig_hash;
+	struct batadv_bucket orig_hash[1u << BATADV_ORIG_HASH_BITS];
 	spinlock_t forw_bat_list_lock; /* protects forw_bat_list */
 	spinlock_t forw_bcast_list_lock; /* protects forw_bcast_list */
 	struct delayed_work orig_work;
Signature · 1 line
2.1.4