Skip to content

Commit a63f38c

Browse files
akpm00ingomolnar
authored andcommitted
locking/lockdep: Convert hash tables to hlists
Mike said: : CONFIG_UBSAN_ALIGNMENT breaks x86-64 kernel with lockdep enabled, i.e. : kernel with CONFIG_UBSAN_ALIGNMENT=y fails to load without even any error : message. : : The problem is that ubsan callbacks use spinlocks and might be called : before lockdep is initialized. Particularly this line in the : reserve_ebda_region function causes problem: : : lowmem = *(unsigned short *)__va(BIOS_LOWMEM_KILOBYTES); : : If i put lockdep_init() before reserve_ebda_region call in : x86_64_start_reservations kernel loads well. Fix this ordering issue permanently: change lockdep so that it uses hlists for the hash tables. Unlike a list_head, an hlist_head is in its initialized state when it is all-zeroes, so lockdep is ready for operation immediately upon boot - lockdep_init() need not have run. The patch will also save some memory. Probably lockdep_init() and lockdep_initialized can be done away with now. Suggested-by: Mike Krinkin <krinkin.m.u@gmail.com> Reported-by: Mike Krinkin <krinkin.m.u@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Cc: mm-commits@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 3aa6b46 commit a63f38c

2 files changed

Lines changed: 21 additions & 25 deletions

File tree

‎include/linux/lockdep.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct lock_class {
6666
/*
6767
* class-hash:
6868
*/
69-
struct list_head hash_entry;
69+
struct hlist_node hash_entry;
7070

7171
/*
7272
* global list of all lock-classes:
@@ -199,7 +199,7 @@ struct lock_chain {
199199
u8 irq_context;
200200
u8 depth;
201201
u16 base;
202-
struct list_head entry;
202+
struct hlist_node entry;
203203
u64 chain_key;
204204
};
205205

‎kernel/locking/lockdep.c‎

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ LIST_HEAD(all_lock_classes);
292292
#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
293293
#define classhashentry(key) (classhash_table + __classhashfn((key)))
294294

295-
static struct list_head classhash_table[CLASSHASH_SIZE];
295+
static struct hlist_head classhash_table[CLASSHASH_SIZE];
296296

297297
/*
298298
* We put the lock dependency chains into a hash-table as well, to cache
@@ -303,7 +303,7 @@ static struct list_head classhash_table[CLASSHASH_SIZE];
303303
#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
304304
#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
305305

306-
static struct list_head chainhash_table[CHAINHASH_SIZE];
306+
static struct hlist_head chainhash_table[CHAINHASH_SIZE];
307307

308308
/*
309309
* The hash key of the lock dependency chains is a hash itself too:
@@ -666,7 +666,7 @@ static inline struct lock_class *
666666
look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
667667
{
668668
struct lockdep_subclass_key *key;
669-
struct list_head *hash_head;
669+
struct hlist_head *hash_head;
670670
struct lock_class *class;
671671

672672
#ifdef CONFIG_DEBUG_LOCKDEP
@@ -719,7 +719,7 @@ look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
719719
if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
720720
return NULL;
721721

722-
list_for_each_entry_rcu(class, hash_head, hash_entry) {
722+
hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
723723
if (class->key == key) {
724724
/*
725725
* Huh! same key, different name? Did someone trample
@@ -742,7 +742,7 @@ static inline struct lock_class *
742742
register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
743743
{
744744
struct lockdep_subclass_key *key;
745-
struct list_head *hash_head;
745+
struct hlist_head *hash_head;
746746
struct lock_class *class;
747747

748748
DEBUG_LOCKS_WARN_ON(!irqs_disabled());
@@ -774,7 +774,7 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
774774
* We have to do the hash-walk again, to avoid races
775775
* with another CPU:
776776
*/
777-
list_for_each_entry_rcu(class, hash_head, hash_entry) {
777+
hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
778778
if (class->key == key)
779779
goto out_unlock_set;
780780
}
@@ -805,7 +805,7 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
805805
* We use RCU's safe list-add method to make
806806
* parallel walking of the hash-list safe:
807807
*/
808-
list_add_tail_rcu(&class->hash_entry, hash_head);
808+
hlist_add_head_rcu(&class->hash_entry, hash_head);
809809
/*
810810
* Add it to the global list of classes:
811811
*/
@@ -2021,7 +2021,7 @@ static inline int lookup_chain_cache(struct task_struct *curr,
20212021
u64 chain_key)
20222022
{
20232023
struct lock_class *class = hlock_class(hlock);
2024-
struct list_head *hash_head = chainhashentry(chain_key);
2024+
struct hlist_head *hash_head = chainhashentry(chain_key);
20252025
struct lock_chain *chain;
20262026
struct held_lock *hlock_curr;
20272027
int i, j;
@@ -2037,7 +2037,7 @@ static inline int lookup_chain_cache(struct task_struct *curr,
20372037
* We can walk it lock-free, because entries only get added
20382038
* to the hash:
20392039
*/
2040-
list_for_each_entry_rcu(chain, hash_head, entry) {
2040+
hlist_for_each_entry_rcu(chain, hash_head, entry) {
20412041
if (chain->chain_key == chain_key) {
20422042
cache_hit:
20432043
debug_atomic_inc(chain_lookup_hits);
@@ -2061,7 +2061,7 @@ static inline int lookup_chain_cache(struct task_struct *curr,
20612061
/*
20622062
* We have to walk the chain again locked - to avoid duplicates:
20632063
*/
2064-
list_for_each_entry(chain, hash_head, entry) {
2064+
hlist_for_each_entry(chain, hash_head, entry) {
20652065
if (chain->chain_key == chain_key) {
20662066
graph_unlock();
20672067
goto cache_hit;
@@ -2095,7 +2095,7 @@ static inline int lookup_chain_cache(struct task_struct *curr,
20952095
}
20962096
chain_hlocks[chain->base + j] = class - lock_classes;
20972097
}
2098-
list_add_tail_rcu(&chain->entry, hash_head);
2098+
hlist_add_head_rcu(&chain->entry, hash_head);
20992099
debug_atomic_inc(chain_lookup_misses);
21002100
inc_chains();
21012101

@@ -3879,7 +3879,7 @@ void lockdep_reset(void)
38793879
nr_process_chains = 0;
38803880
debug_locks = 1;
38813881
for (i = 0; i < CHAINHASH_SIZE; i++)
3882-
INIT_LIST_HEAD(chainhash_table + i);
3882+
INIT_HLIST_HEAD(chainhash_table + i);
38833883
raw_local_irq_restore(flags);
38843884
}
38853885

@@ -3898,7 +3898,7 @@ static void zap_class(struct lock_class *class)
38983898
/*
38993899
* Unhash the class and remove it from the all_lock_classes list:
39003900
*/
3901-
list_del_rcu(&class->hash_entry);
3901+
hlist_del_rcu(&class->hash_entry);
39023902
list_del_rcu(&class->lock_entry);
39033903

39043904
RCU_INIT_POINTER(class->key, NULL);
@@ -3921,7 +3921,7 @@ static inline int within(const void *addr, void *start, unsigned long size)
39213921
void lockdep_free_key_range(void *start, unsigned long size)
39223922
{
39233923
struct lock_class *class;
3924-
struct list_head *head;
3924+
struct hlist_head *head;
39253925
unsigned long flags;
39263926
int i;
39273927
int locked;
@@ -3934,9 +3934,7 @@ void lockdep_free_key_range(void *start, unsigned long size)
39343934
*/
39353935
for (i = 0; i < CLASSHASH_SIZE; i++) {
39363936
head = classhash_table + i;
3937-
if (list_empty(head))
3938-
continue;
3939-
list_for_each_entry_rcu(class, head, hash_entry) {
3937+
hlist_for_each_entry_rcu(class, head, hash_entry) {
39403938
if (within(class->key, start, size))
39413939
zap_class(class);
39423940
else if (within(class->name, start, size))
@@ -3966,7 +3964,7 @@ void lockdep_free_key_range(void *start, unsigned long size)
39663964
void lockdep_reset_lock(struct lockdep_map *lock)
39673965
{
39683966
struct lock_class *class;
3969-
struct list_head *head;
3967+
struct hlist_head *head;
39703968
unsigned long flags;
39713969
int i, j;
39723970
int locked;
@@ -3991,9 +3989,7 @@ void lockdep_reset_lock(struct lockdep_map *lock)
39913989
locked = graph_lock();
39923990
for (i = 0; i < CLASSHASH_SIZE; i++) {
39933991
head = classhash_table + i;
3994-
if (list_empty(head))
3995-
continue;
3996-
list_for_each_entry_rcu(class, head, hash_entry) {
3992+
hlist_for_each_entry_rcu(class, head, hash_entry) {
39973993
int match = 0;
39983994

39993995
for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
@@ -4031,10 +4027,10 @@ void lockdep_init(void)
40314027
return;
40324028

40334029
for (i = 0; i < CLASSHASH_SIZE; i++)
4034-
INIT_LIST_HEAD(classhash_table + i);
4030+
INIT_HLIST_HEAD(classhash_table + i);
40354031

40364032
for (i = 0; i < CHAINHASH_SIZE; i++)
4037-
INIT_LIST_HEAD(chainhash_table + i);
4033+
INIT_HLIST_HEAD(chainhash_table + i);
40384034

40394035
lockdep_initialized = 1;
40404036
}

0 commit comments

Comments
 (0)