batman-adv: Remove dynamically allocated hashtables
PATCH v4 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 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.
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.
---
v4:
- updated commit message
- renamed batadv_bucket to batadv_hashbucket
- added BATADV_DECLARE_HASHTABLE to avoid all these "1u <<"
- squashed RFCv3 2/3 with RFCv3 3/3 because 3/3 changed mostly all lines again which 2/3 modified and even deletes some of them again
- Remove unused batadv_hash_delete
- Added some missing function documentation blocks
- replaced some 'size' in function parameters with 'hash_size'
- Moved this patch to the first place to reduce number of modified lines in whole patchset
--- Makefile.kbuild | 1 - bat_iv_ogm.c | 12 ++-- bridge_loop_avoidance.c | 129 +++++++++++++------------------------ distributed-arp-table.c | 48 ++++---------- hash.c | 76 ---------------------- hash.h | 108 +++++++++++++------------------ network-coding.c | 145 +++++++++++++++++++++-------------------- originator.c | 55 +++++----------- originator.h | 9 +-- translation-table.c | 168 ++++++++++++++++++++---------------------------- types.h | 27 +++++--- 11 files changed, 286 insertions(+), 492 deletions(-) delete mode 100644 hash.c
Makefile.kbuild +0 -1
@@ -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 -6
@@ -862,7 +862,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;
@@ -871,8 +871,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) {
@@ -1799,7 +1799,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;
@@ -1812,8 +1812,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 -86
280 lines not on this page · raw message
distributed-arp-table.c +13 -35
108 lines not on this page · raw message
hash.c deleted +0 -76
76 lines not on this page · raw message
hash.h +46 -62
151 lines not on this page · raw message
network-coding.c +72 -73
307 lines not on this page · raw message
originator.c +15 -40
130 lines not on this page · raw message
originator.h +3 -6
@@ -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_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 -99
379 lines not on this page · raw message
types.h +19 -8
@@ -35,6 +35,16 @@#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
@@ -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_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 */
@@ -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_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 */
@@ -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_hashbucket hash[1024 ];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_hashbucket coding_hash[128 ]; +struct batadv_hashbucket decoding_hash[128 ]; };/**
@@ -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_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
PATCH v4 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.
--- main.h | 9 +++++++++ types.h | 24 ++++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-)
main.h +9 -0
@@ -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
@@ -45,6 +45,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
@@ -527,8 +535,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 */
@@ -558,8 +566,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 */
@@ -629,7 +637,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
@@ -692,8 +700,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 ); };/**
@@ -806,7 +814,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