Skip to contentSkip to conversation

batman-adv: Remove dynamically allocated hashtables

PATCH In review Sven Eckelmann 1 participant 4 messages Apr 21, 2015 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/2 batman-adv: Remove dynamically allocated hashtables

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 side-effect 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 withouttriggering a lockdep warning.

This also fixes the problem of unprotected access to the pointer to the hashtables. These were dynamically allocated/freed and the pointers were stored in batadv_priv*. Access to these pointers were never protected by any lock and using them could always lead to problems when the batman-adv device was removed.

---
v6:

* remove kerneldoc about hash_lock in types.h when there was no hash_lock in the struct batadv_priv_dat

v5: Rebased on top of patches:

* batman-adv: Remove explicit compat.h include and split compat.h
* batman-adv: Add required to includes to all files
* batman-adv: Remove unused IFF_BRIDGE_PORT live patching hack
* batman-adv: Automatically create nested kfree_rcu helper functions

 Makefile.kbuild         |   1 -
 bat_iv_ogm.c            |  13 ++--
 bridge_loop_avoidance.c | 131 ++++++++++++-------------------------
 distributed-arp-table.c |  49 ++++----------
 hash.c                  |  80 -----------------------
 hash.h                  | 106 +++++++++++++-----------------
 network-coding.c        | 146 ++++++++++++++++++++---------------------
 originator.c            |  56 +++++-----------
 originator.h            |  12 ++--
 translation-table.c     | 170 ++++++++++++++++++++----------------------------
 types.h                 |  26 +++++---
 11 files changed, 286 insertions(+), 504 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 +7 -6
index f3214ed..f2df840 100644
@@ -19,6 +19,7 @@
 #include "main.h"
 
 #include <linux/atomic.h>
+#include <linux/kernel.h>
 #include <linux/bitmap.h>
 #include <linux/bitops.h>
 #include <linux/bug.h>
@@ -892,7 +893,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_hashbucket *hash = bat_priv->orig_hash;
 	struct hlist_head *head;
 	struct batadv_orig_node *orig_node;
 	unsigned long *word;
@@ -901,8 +902,8 @@ 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++) {
-		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) {
@@ -1828,7 +1829,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_hashbucket *hash = bat_priv->orig_hash;
 	int last_seen_msecs, last_seen_secs;
 	struct batadv_orig_node *orig_node;
 	struct batadv_neigh_ifinfo *n_ifinfo;
@@ -1841,8 +1842,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 < hash->size; 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 +43 -88

294 lines not on this page · raw message

distributed-arp-table.c +13 -36

115 lines not on this page · raw message

hash.c deleted +0 -80

80 lines not on this page · raw message

hash.h +45 -61

153 lines not on this page · raw message

network-coding.c +72 -74

314 lines not on this page · raw message

originator.c +15 -41

137 lines not on this page · raw message

originator.h +4 -8
index 39085cc..a2e4fe7 100644
@@ -21,6 +21,7 @@
 #include "main.h"
 
 #include <linux/atomic.h>
+#include <linux/kernel.h>
 #include <linux/compiler.h>
 #include <linux/if_ether.h>
 #include <linux/jhash.h>
@@ -29,8 +30,6 @@
 #include <linux/stddef.h>
 #include <linux/types.h>
 
-#include "hash.h"
-
 struct seq_file;
 
 int batadv_compare_orig(const struct hlist_node *node, const void *data2);
@@ -97,16 +96,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_hashbucket *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, hash->size);
-	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 +69 -101

393 lines not on this page · raw message

types.h +18 -8
index 1d4781e..f025cc0 100644
@@ -49,6 +49,16 @@ struct seq_file;
 #endif /* CONFIG_BATMAN_ADV_DAT */
 
 /**
+ * struct batadv_hashbucket - bucket for concurrent, list based hashtable
+ * @head: pointer to the data of this bucket
+ * @lock: lock protecting writes to this bucket
+ */
+struct batadv_hashbucket {
+	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
@@ -532,8 +542,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_hashbucket local_hash[1024];
+	struct batadv_hashbucket global_hash[1024];
 	struct list_head req_list;
 	struct list_head roam_list;
 	spinlock_t changes_list_lock; /* protects changes */
@@ -563,8 +573,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_hashbucket claim_hash[128];
+	struct batadv_hashbucket backbone_hash[32];
 	struct batadv_bcast_duplist_entry bcast_duplist[BATADV_DUPLIST_SIZE];
 	int bcast_duplist_curr;
 	/* protects bcast_duplist & bcast_duplist_curr */
@@ -633,7 +643,7 @@ struct batadv_priv_tvlv {
 #ifdef CONFIG_BATMAN_ADV_DAT
 struct batadv_priv_dat {
 	batadv_dat_addr_t addr;
-	struct batadv_hashtable *hash;
+	struct batadv_hashbucket hash[1024];
 	struct delayed_work work;
 };
 #endif
@@ -696,8 +706,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_hashbucket coding_hash[128];
+	struct batadv_hashbucket decoding_hash[128];
 };
 
 /**
@@ -810,7 +820,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_hashbucket orig_hash[1024];
 	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
#1 lore raw

PATCH v6 2/2 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.

---
v6:

* no changes

v5: Rebased on top of patches:

* batman-adv: Remove explicit compat.h include and split compat.h
* batman-adv: Add required to includes to all files
* batman-adv: Remove unused IFF_BRIDGE_PORT live patching hack
* batman-adv: Automatically create nested kfree_rcu helper functions

 main.h  |  9 +++++++++
 types.h | 24 ++++++++++++++++--------
 2 files changed, 25 insertions(+), 8 deletions(-)
main.h +9 -0
index dee8072..c1cb6db 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
types.h +16 -8
index f025cc0..e311b95 100644
@@ -59,6 +59,14 @@ struct batadv_hashbucket {
 };
 
 /**
+ * BATADV_DECLARE_HASHTABLE - declare an array of buckets for an hashtable
+ * @name: name of the declared array of buckets
+ * @bits: number of bits used from a hash to find a bucket in the hashtable
+ */
+#define BATADV_DECLARE_HASHTABLE(name, bits) \
+		struct batadv_hashbucket name[1u << bits]
+
+/**
  * 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
@@ -542,8 +550,8 @@ struct batadv_priv_tt {
 	atomic_t ogm_append_cnt;
 	atomic_t local_changes;
 	struct list_head changes_list;
-	struct batadv_hashbucket local_hash[1024];
-	struct batadv_hashbucket global_hash[1024];
+	BATADV_DECLARE_HASHTABLE(local_hash, BATADV_TT_LOCAL_HASH_BITS);
+	BATADV_DECLARE_HASHTABLE(global_hash, BATADV_TT_GLOBAL_HASH_BITS);
 	struct list_head req_list;
 	struct list_head roam_list;
 	spinlock_t changes_list_lock; /* protects changes */
@@ -573,8 +581,8 @@ struct batadv_priv_tt {
 #ifdef CONFIG_BATMAN_ADV_BLA
 struct batadv_priv_bla {
 	atomic_t num_requests;
-	struct batadv_hashbucket claim_hash[128];
-	struct batadv_hashbucket backbone_hash[32];
+	BATADV_DECLARE_HASHTABLE(claim_hash, BATADV_BLA_CLAIM_HASH_BITS);
+	BATADV_DECLARE_HASHTABLE(backbone_hash, 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 */
@@ -643,7 +651,7 @@ struct batadv_priv_tvlv {
 #ifdef CONFIG_BATMAN_ADV_DAT
 struct batadv_priv_dat {
 	batadv_dat_addr_t addr;
-	struct batadv_hashbucket hash[1024];
+	BATADV_DECLARE_HASHTABLE(hash, BATADV_DAT_HASH_BITS);
 	struct delayed_work work;
 };
 #endif
@@ -706,8 +714,8 @@ struct batadv_priv_nc {
 	u32 max_buffer_time;
 	unsigned long timestamp_fwd_flush;
 	unsigned long timestamp_sniffed_purge;
-	struct batadv_hashbucket coding_hash[128];
-	struct batadv_hashbucket decoding_hash[128];
+	BATADV_DECLARE_HASHTABLE(coding_hash, BATADV_NC_CODING_HASH_BITS);
+	BATADV_DECLARE_HASHTABLE(decoding_hash, BATADV_NC_DECODING_HASH_BITS);
 };
 
 /**
@@ -820,7 +828,7 @@ struct batadv_priv {
 	struct dentry *debug_dir;
 	struct hlist_head forw_bat_list;
 	struct hlist_head forw_bcast_list;
-	struct batadv_hashbucket orig_hash[1024];
+	BATADV_DECLARE_HASHTABLE(orig_hash, 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
#2 lore raw
In reply to Sven Eckelmann
Quoted 6 lines · On Tuesday 21 April 2015 10:42:42 Sven Eckelmann wrote:

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.

I was now informed that the rhashtable can now (Linux 4.0) support per bucket locks. The last statement is therefore wrong since the last Linux release.

Kind regards,
Sven

#3 lore raw
Quoted 9 lines · On Tuesday 21 April 2015 11:37:37 Sven Eckelmann wrote:
Quoted 7 lines · On Tuesday 21 April 2015 10:42:42 Sven Eckelmann wrote:

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.

Quoted 3 lines

I was now informed that the rhashtable can now (Linux 4.0) support per bucket locks. The last statement is therefore wrong since the last Linux release.

I cannot find a good reason why David should accept such a patch. I will therefore retract this patchset. The remaining patches will be rebased and submitted later (during lunch time or this evening).

Kind regards,
Sven