locking/lockdep: Use hashtable.h for lock_keys_hash
PATCH 1/4 locking/lockdep: Use hashtable.h for lock_keys_hash
Convert the lock_keys_hash array in lockdep.c to use the generic hashtable implementation from hashtable.h instead of the manual hlist_head array implementation.
This simplifies the code and makes it more maintainable by using the standard hashtable API defined in hashtable.h, while preserving the RCU-safe behavior with proper RCU variants of the hashtable functions.
--- kernel/locking/lockdep.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-)
kernel/locking/lockdep.c +16 -18
@@ -57,6 +57,7 @@#include <linux/lockdep.h> #include <linux/context_tracking.h> #include <linux/console.h> +#include <linux/hashtable.h> #include <asm/sections.h>
@@ -212,8 +213,7 @@ static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);* in use. */ #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) -#define KEYHASH_SIZE (1UL << KEYHASH_BITS) -static struct hlist_head lock_keys_hash[KEYHASH_SIZE ]; +static DEFINE_HASHTABLE(lock_keys_hash, KEYHASH_BITS);unsigned long nr_lock_classes;unsigned long nr_zapped_classes;unsigned long max_lock_class_idx;
@@ -1209,32 +1209,28 @@ static void init_data_structures_once(void)init_chain_block_buckets (); } -static inline struct hlist_head *keyhashentry (const struct lock_class_key *key) -{ -unsigned long hash =hash_long ((uintptr_t )key,KEYHASH_BITS ); - -return lock_keys_hash + hash; -}/* Register a dynamically allocated key. */ void lockdep_register_key (struct lock_class_key *key) { -struct hlist_head *hash_head;struct lock_class_key *k; +unsigned long hash;unsigned long flags;if (WARN_ON_ONCE (static_obj (key)))return ; - hash_head =keyhashentry (key); + + hash =hash_long ((uintptr_t )key,KEYHASH_BITS );raw_local_irq_save (flags);if (!graph_lock ()) goto restore_irqs; -hlist_for_each_entry_rcu (k, hash_head, hash_entry) { + +hash_for_each_possible_rcu (lock_keys_hash, k, hash_entry, hash) {if (WARN_ON_ONCE (k == key)) goto out_unlock; } -hlist_add_head_rcu (&key->hash_entry, hash_head); +hash_add_rcu (lock_keys_hash, &key->hash_entry, hash); out_unlock:graph_unlock (); restore_irqs:
@@ -1245,8 +1241,8 @@ EXPORT_SYMBOL_GPL(lockdep_register_key);/* Check whether a key has been registered as a dynamic key. */ static bool is_dynamic_key (const struct lock_class_key *key) { -struct hlist_head *hash_head;struct lock_class_key *k; +unsigned long hash;bool found = false;if (WARN_ON_ONCE (static_obj (key)))
@@ -1260,10 +1256,10 @@ static bool is_dynamic_key(const struct lock_class_key *key)if (!debug_locks)return true; - hash_head =keyhashentry (key); + hash =hash_long ((uintptr_t )key,KEYHASH_BITS );rcu_read_lock (); -hlist_for_each_entry_rcu (k, hash_head, hash_entry) { +hash_for_each_possible_rcu (lock_keys_hash, k, hash_entry, hash) {if (k == key) { found = true;break ;
@@ -6561,9 +6557,9 @@ void lockdep_reset_lock(struct lockdep_map *lock)*/ void lockdep_unregister_key (struct lock_class_key *key) { -struct hlist_head *hash_head =keyhashentry (key);struct lock_class_key *k;struct pending_free *pf; +unsigned long hash;unsigned long flags;bool found = false;bool need_callback = false;
@@ -6573,12 +6569,14 @@ void lockdep_unregister_key(struct lock_class_key *key)if (WARN_ON_ONCE (static_obj (key)))return ; + hash =hash_long ((uintptr_t )key,KEYHASH_BITS ); +raw_local_irq_save (flags);lockdep_lock (); -hlist_for_each_entry_rcu (k, hash_head, hash_entry) { +hash_for_each_possible (lock_keys_hash, k, hash_entry, hash) {if (k == key) { -hlist_del_rcu (&k->hash_entry); +hash_del_rcu (&k->hash_entry); found = true;break ; }
Signature · 1 line
2.39.5
PATCH 2/4 locking/lockdep: Use hashtable.h for classhash_table
Convert classhash_table in lockdep.c to use the generic hashtable implementation from hashtable.h instead of the manual hlist_head array implementation.
This simplifies the code and makes it more maintainable by using the standard hashtable API and removes the need for custom hash lookup functions.
--- kernel/locking/lockdep.c | 69 ++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 45 deletions(-)
kernel/locking/lockdep.c +24 -45
@@ -404,12 +404,8 @@ static struct delayed_free {/* * The lockdep classes are in a hash-table as well, for fast lookup: */ -#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) -#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS) -#define __classhashfn (key) hash_long((unsigned long)key, CLASSHASH_BITS) -#define classhashentry (key) (classhash_table + __classhashfn((key))) - -static struct hlist_head classhash_table[CLASSHASH_SIZE ]; +#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS-1) +static DEFINE_HASHTABLE(classhash_table, CLASSHASH_BITS);/* * We put the lock dependency chains into a hash-table as well, to cache
@@ -886,8 +882,8 @@ static noinstr struct lock_class * look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass) {struct lockdep_subclass_key *key; -struct hlist_head *hash_head;struct lock_class *class; +unsigned long hash;if (unlikely (subclass >=MAX_LOCKDEP_SUBCLASSES )) {instrumentation_begin ();
@@ -920,8 +916,7 @@ look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass) sizeof(struct lockdep_map)); key = lock->key->subkeys + subclass; - - hash_head =classhashentry (key); + hash = (unsigned long )key;/* * We do an RCU walk of the hash, see lockdep_free_key_range().
@@ -929,7 +924,7 @@ look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)if (DEBUG_LOCKS_WARN_ON (!irqs_disabled ()))return NULL ; -hlist_for_each_entry_rcu_notrace (class, hash_head, hash_entry) { +hash_for_each_possible_rcu_notrace (classhash_table, class, hash_entry, hash) {if (class->key == key) {/* * Huh! same key, different name? Did someone trample
@@ -1084,7 +1079,6 @@ static bool __check_data_structures(void) {struct lock_class *class;struct lock_chain *chain; -struct hlist_head *head;struct lock_list *e;int i;
@@ -1110,12 +1104,9 @@ static bool __check_data_structures(void) }/* Check the chain_key of all lock chains. */ -for (i =0 ; i <ARRAY_SIZE (chainhash_table); i++) { - head = chainhash_table + i; -hlist_for_each_entry_rcu (chain, head, entry) { -if (!check_lock_chain_key (chain)) -return false; - } +hash_for_each_rcu (chainhash_table, i, chain, entry) { +if (!check_lock_chain_key (chain)) +return false; }/*
@@ -1279,8 +1270,8 @@ static struct lock_class * register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) { struct lockdep_subclass_key *key; -struct hlist_head *hash_head;struct lock_class *class; +unsigned long hash;int idx;DEBUG_LOCKS_WARN_ON (!irqs_disabled ());
@@ -1297,7 +1288,7 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) } key = lock->key->subkeys + subclass; - hash_head =classhashentry (key); + hash = (unsigned long )key;if (!graph_lock ()) {return NULL ;
@@ -1306,7 +1297,7 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)* We have to do the hash-walk again, to avoid races * with another CPU: */ -hlist_for_each_entry_rcu (class, hash_head, hash_entry) { +hash_for_each_possible_rcu (classhash_table, class, hash_entry, hash) {if (class->key == key) goto out_unlock_set; }
@@ -1343,7 +1334,7 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)* We use RCU's safe list-add method to make * parallel walking of the hash-list safe: */ -hlist_add_head_rcu (&class->hash_entry, hash_head); +hash_add_rcu (classhash_table, &class->hash_entry, hash);/* * Remove the class from the free list and add it to the global list * of classes.
@@ -6206,14 +6197,10 @@ static void remove_class_from_lock_chains(struct pending_free *pf, struct lock_class *class) {struct lock_chain *chain; -struct hlist_head *head;int i; -for (i =0 ; i <ARRAY_SIZE (chainhash_table); i++) { - head = chainhash_table + i; -hlist_for_each_entry_rcu (chain, head, entry) { -remove_class_from_lock_chain (pf, chain, class); - } +hash_for_each_rcu (chainhash_table, i, chain, entry) { +remove_class_from_lock_chain (pf, chain, class); } }
@@ -6370,18 +6357,14 @@ static void __lockdep_free_key_range(struct pending_free *pf, void *start, unsigned long size) {struct lock_class *class; -struct hlist_head *head;int i;/* Unhash all classes that were created by a module. */ -for (i =0 ; i <CLASSHASH_SIZE ; i++) { - head = classhash_table + i; -hlist_for_each_entry_rcu (class, head, hash_entry) { -if (!within (class->key, start, size) && - !within (class->name, start, size)) -continue ; -zap_class (pf, class); - } +hash_for_each_rcu (classhash_table, i, class, hash_entry) { +if (!within (class->key, start, size) && + !within (class->name, start, size)) +continue ; +zap_class (pf, class); } }
@@ -6454,16 +6437,12 @@ void lockdep_free_key_range(void *start, unsigned long size)static bool lock_class_cache_is_registered (struct lockdep_map *lock) {struct lock_class *class; -struct hlist_head *head;int i, j; -for (i =0 ; i <CLASSHASH_SIZE ; i++) { - head = classhash_table + i; -hlist_for_each_entry_rcu (class, head, hash_entry) { -for (j =0 ; j <NR_LOCKDEP_CACHING_CLASSES ; j++) -if (lock->class_cache[j] == class) -return true; - } +hash_for_each_rcu (classhash_table, i, class, hash_entry) { +for (j =0 ; j <NR_LOCKDEP_CACHING_CLASSES ; j++) +if (lock->class_cache[j] == class) +return true; }return false; }
@@ -6605,7 +6584,7 @@ void __init lockdep_init(void)pr_info ("... MAX_LOCKDEP_SUBCLASSES: %lu\n" ,MAX_LOCKDEP_SUBCLASSES );pr_info ("... MAX_LOCK_DEPTH: %lu\n" ,MAX_LOCK_DEPTH );pr_info ("... MAX_LOCKDEP_KEYS: %lu\n" ,MAX_LOCKDEP_KEYS ); -pr_info ("... CLASSHASH_SIZE: %lu\n" ,CLASSHASH_SIZE ); +pr_info ("... CLASSHASH_SIZE: %lu\n" ,HASH_SIZE (classhash_table));pr_info ("... MAX_LOCKDEP_ENTRIES: %lu\n" ,MAX_LOCKDEP_ENTRIES );pr_info ("... MAX_LOCKDEP_CHAINS: %lu\n" ,MAX_LOCKDEP_CHAINS );pr_info ("... CHAINHASH_SIZE: %lu\n" ,CHAINHASH_SIZE );
Signature · 1 line
2.39.5
PATCH 3/4 locking/lockdep: Use hashtable.h for chainhash_table
In reply to Sasha LevinConvert chainhash_table in lockdep.c to use the generic hashtable implementation from hashtable.h instead of the manual hlist_head array implementation.
This simplifies the code and makes it more maintainable by using the standard hashtable API and removes the need for custom hash lookup functions.
--- kernel/locking/lockdep.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-)
kernel/locking/lockdep.c +9 -17
@@ -412,11 +412,7 @@ static DEFINE_HASHTABLE(classhash_table, CLASSHASH_BITS);* their existence: */ #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1) -#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS) -#define __chainhashfn (chain) hash_long(chain, CHAINHASH_BITS) -#define chainhashentry (chain) (chainhash_table + __chainhashfn((chain))) - -static struct hlist_head chainhash_table[CHAINHASH_SIZE ]; +DEFINE_HASHTABLE (chainhash_table,CHAINHASH_BITS );/* * the id of held_lock
@@ -3716,7 +3712,6 @@ static inline int add_chain_cache(struct task_struct *curr, struct held_lock *hlock, u64 chain_key) { -struct hlist_head *hash_head =chainhashentry (chain_key);struct lock_chain *chain;int i, j;
@@ -3767,7 +3762,7 @@ static inline int add_chain_cache(struct task_struct *curr, chain_hlocks[chain->base + j] = lock_id; } chain_hlocks[chain->base + j] =hlock_id (hlock); -hlist_add_head_rcu (&chain->entry, hash_head); +hash_add_rcu (chainhash_table, &chain->entry, chain_key);debug_atomic_inc (chain_lookup_misses);inc_chains (chain->irq_context);
@@ -3780,10 +3775,9 @@ static inline int add_chain_cache(struct task_struct *curr,*/ static inline struct lock_chain *lookup_chain_cache (u64 chain_key) { -struct hlist_head *hash_head =chainhashentry (chain_key);struct lock_chain *chain; -hlist_for_each_entry_rcu (chain, hash_head, entry) { +hash_for_each_possible_rcu (chainhash_table, chain, entry, chain_key) {if (READ_ONCE (chain->chain_key) == chain_key) {debug_atomic_inc (chain_lookup_hits);return chain;
@@ -6142,7 +6136,6 @@ EXPORT_SYMBOL_GPL(lock_acquired);void lockdep_reset (void ) {unsigned long flags; -int i;raw_local_irq_save (flags);lockdep_init_task (current);
@@ -6151,8 +6144,7 @@ void lockdep_reset(void) nr_softirq_chains =0 ; nr_process_chains =0 ; debug_locks =1 ; -for (i =0 ; i <CHAINHASH_SIZE ; i++) -INIT_HLIST_HEAD (chainhash_table + i); +hash_init (chainhash_table);raw_local_irq_restore (flags); }
@@ -6183,10 +6175,10 @@ static void remove_class_from_lock_chain(struct pending_free *pf,dec_chains (chain->irq_context);/* -* Note: calling hlist_del_rcu() from inside a -* hlist_for_each_entry_rcu() loop is safe. +* Note: calling hash_del_rcu() from inside a +* hash_for_each_rcu() loop is safe. */ -hlist_del_rcu (&chain->entry); +hash_del_rcu (&chain->entry);__set_bit (chain - lock_chains, pf->lock_chains_being_freed); nr_zapped_lock_chains++;#endif
@@ -6229,7 +6221,7 @@ static void zap_class(struct pending_free *pf, struct lock_class *class)if (list_empty (&class->locks_after) &&list_empty (&class->locks_before)) {list_move_tail (&class->lock_entry, &pf->zapped); -hlist_del_rcu (&class->hash_entry); +hash_del_rcu (&class->hash_entry);WRITE_ONCE (class->key,NULL );WRITE_ONCE (class->name,NULL ); nr_lock_classes--;
@@ -6587,7 +6579,7 @@ void __init lockdep_init(void)pr_info ("... CLASSHASH_SIZE: %lu\n" ,HASH_SIZE (classhash_table));pr_info ("... MAX_LOCKDEP_ENTRIES: %lu\n" ,MAX_LOCKDEP_ENTRIES );pr_info ("... MAX_LOCKDEP_CHAINS: %lu\n" ,MAX_LOCKDEP_CHAINS ); -pr_info ("... CHAINHASH_SIZE: %lu\n" ,CHAINHASH_SIZE ); +pr_info ("... CHAINHASH_SIZE: %lu\n" ,HASH_SIZE (chainhash_table)); pr_info(" memory used by lock dependency info: %zu kB\n", (sizeof(lock_classes) +
Signature · 1 line
2.39.5
PATCH 4/4 locking/lockdep: Use hashtable.h for stack_trace_hash
In reply to Sasha LevinConvert stack_trace_hash in lockdep.c to use the generic hashtable implementation from hashtable.h instead of the manual hlist_head array implementation.
This simplifies the code and makes it more maintainable by using the standard hashtable API and removes the need for custom hash lookup functions.
--- kernel/locking/lockdep.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
kernel/locking/lockdep.c +9 -12
@@ -541,7 +541,8 @@ struct lock_trace {* Stack-trace: sequence of lock_trace structures. Protected by the graph_lock. */ static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES ]; -static struct hlist_head stack_trace_hash[STACK_TRACE_HASH_SIZE ]; +#define STACK_TRACE_HASH_BITS (ilog2(STACK_TRACE_HASH_SIZE)) +static DEFINE_HASHTABLE(stack_trace_hash, STACK_TRACE_HASH_BITS);static bool traces_identical (struct lock_trace *t1,struct lock_trace *t2) {
@@ -553,7 +554,6 @@ static bool traces_identical(struct lock_trace *t1, struct lock_trace *t2)static struct lock_trace *save_trace (void ) {struct lock_trace *trace, *t2; -struct hlist_head *hash_head;u32 hash;int max_entries;
@@ -580,13 +580,13 @@ static struct lock_trace *save_trace(void) hash =jhash (trace->entries, trace->nr_entries *sizeof (trace->entries[0 ]),0 ); trace->hash = hash; - hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE -1 )); -hlist_for_each_entry (t2, hash_head, hash_entry) { + +hash_for_each_possible (stack_trace_hash, t2, hash_entry, hash)if (traces_identical (trace, t2))return t2; - } + nr_stack_trace_entries +=LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries; -hlist_add_head (&trace->hash_entry, hash_head); +hash_add (stack_trace_hash, &trace->hash_entry, hash);return trace; }
@@ -598,11 +598,8 @@ u64 lockdep_stack_trace_count(void)u64 c =0 ;int i; -for (i =0 ; i <ARRAY_SIZE (stack_trace_hash); i++) { -hlist_for_each_entry (trace, &stack_trace_hash[i], hash_entry) { - c++; - } - } +hash_for_each (stack_trace_hash, i, trace, hash_entry) + c++;return c; }
@@ -613,7 +610,7 @@ u64 lockdep_stack_hash_count(void)u64 c =0 ;int i; -for (i =0 ; i <ARRAY_SIZE (stack_trace_hash); i++) +for (i =0 ; i <HASH_SIZE (stack_trace_hash); i++)if (!hlist_empty (&stack_trace_hash[i])) c++;
Signature · 1 line
2.39.5
Hi Sasha,
Thanks for the patches. I want to let you know that we are currently doing some changes on the hashlist usage in lockdep:
https://lore.kernel.org/lkml/[email protected]/
so, it may take a while for me to back looking into this (until that get sorted).
Regards,
Boqun
Quoted 128 lines · Sasha Levin
Convert the
lock_keys_hasharray inlockdep.cto use the generic hashtable implementation fromhashtable.hinstead of the manualhlist_headarray implementation.This simplifies the code and makes it more maintainable by using the standard hashtable API defined in
hashtable.h, while preserving the RCU-safe behavior with proper RCU variants of the hashtable functions.Signed-off-by Sasha Levin <[email protected]>--- kernel/locking/lockdep.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-)kernel/locking/lockdep.c +16
-18@@ -57,6 +57,7 @@#include <linux/lockdep.h> #include <linux/context_tracking.h> #include <linux/console.h> +#include <linux/hashtable.h> #include <asm/sections.h> @@ -212,8 +213,7 @@ static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);* in use. */ #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) -#define KEYHASH_SIZE (1UL << KEYHASH_BITS) -static struct hlist_head lock_keys_hash[KEYHASH_SIZE ]; +static DEFINE_HASHTABLE(lock_keys_hash, KEYHASH_BITS);unsigned long nr_lock_classes;unsigned long nr_zapped_classes;unsigned long max_lock_class_idx;@@ -1209,32 +1209,28 @@ static void init_data_structures_once(void)init_chain_block_buckets (); } -static inline struct hlist_head *keyhashentry (const struct lock_class_key *key) -{ -unsigned long hash =hash_long ((uintptr_t )key,KEYHASH_BITS ); - -return lock_keys_hash + hash; -}/* Register a dynamically allocated key. */ void lockdep_register_key (struct lock_class_key *key) { -struct hlist_head *hash_head;struct lock_class_key *k; +unsigned long hash;unsigned long flags;if (WARN_ON_ONCE (static_obj (key)))return ; - hash_head =keyhashentry (key); + + hash =hash_long ((uintptr_t )key,KEYHASH_BITS );raw_local_irq_save (flags);if (!graph_lock ()) goto restore_irqs; -hlist_for_each_entry_rcu (k, hash_head, hash_entry) { + +hash_for_each_possible_rcu (lock_keys_hash, k, hash_entry, hash) {if (WARN_ON_ONCE (k == key)) goto out_unlock; } -hlist_add_head_rcu (&key->hash_entry, hash_head); +hash_add_rcu (lock_keys_hash, &key->hash_entry, hash); out_unlock:graph_unlock (); restore_irqs:@@ -1245,8 +1241,8 @@ EXPORT_SYMBOL_GPL(lockdep_register_key);/* Check whether a key has been registered as a dynamic key. */ static bool is_dynamic_key (const struct lock_class_key *key) { -struct hlist_head *hash_head;struct lock_class_key *k; +unsigned long hash;bool found = false;if (WARN_ON_ONCE (static_obj (key)))@@ -1260,10 +1256,10 @@ static bool is_dynamic_key(const struct lock_class_key *key)if (!debug_locks)return true; - hash_head =keyhashentry (key); + hash =hash_long ((uintptr_t )key,KEYHASH_BITS );rcu_read_lock (); -hlist_for_each_entry_rcu (k, hash_head, hash_entry) { +hash_for_each_possible_rcu (lock_keys_hash, k, hash_entry, hash) {if (k == key) { found = true;break ;@@ -6561,9 +6557,9 @@ void lockdep_reset_lock(struct lockdep_map *lock)*/ void lockdep_unregister_key (struct lock_class_key *key) { -struct hlist_head *hash_head =keyhashentry (key);struct lock_class_key *k;struct pending_free *pf; +unsigned long hash;unsigned long flags;bool found = false;bool need_callback = false;@@ -6573,12 +6569,14 @@ void lockdep_unregister_key(struct lock_class_key *key)if (WARN_ON_ONCE (static_obj (key)))return ; + hash =hash_long ((uintptr_t )key,KEYHASH_BITS ); +raw_local_irq_save (flags);lockdep_lock (); -hlist_for_each_entry_rcu (k, hash_head, hash_entry) { +hash_for_each_possible (lock_keys_hash, k, hash_entry, hash) {if (k == key) { -hlist_del_rcu (&k->hash_entry); +hash_del_rcu (&k->hash_entry); found = true;break ; }Signature · 1 line
2.39.5