Skip to content

Commit 673d62c

Browse files
vegardKAGA-KOKO
authored andcommitted
debugobjects: fix lockdep warning
Daniel J. Blueman reported: > ======================================================= > [ INFO: possible circular locking dependency detected ] > 2.6.27-rc4-224c raspberrypi#1 > ------------------------------------------------------- > hald/4680 is trying to acquire lock: > (&n->list_lock){++..}, at: [<ffffffff802bfa26>] add_partial+0x26/0x80 > > but task is already holding lock: > (&obj_hash[i].lock){++..}, at: [<ffffffff8041cfdc>] > debug_object_free+0x5c/0x120 We fix it by moving the actual freeing to outside the lock (the lock now only protects the list). The pool lock is also promoted to irq-safe (suggested by Dan). It's necessary because free_pool is now called outside the irq disabled region. So we need to protect against an interrupt handler which calls debug_object_init(). [tglx@linutronix.de: added hlist_move_list helper to avoid looping through the list twice] Reported-by: Daniel J Blueman <daniel.blueman@gmail.com> Signed-off-by: Vegard Nossum <vegard.nossum@gmail.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent bef69ea commit 673d62c

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

‎include/linux/list.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,19 @@ static inline void hlist_add_after(struct hlist_node *n,
619619
next->next->pprev = &next->next;
620620
}
621621

622+
/*
623+
* Move a list from one list head to another. Fixup the pprev
624+
* reference of the first entry if it exists.
625+
*/
626+
static inline void hlist_move_list(struct hlist_head *old,
627+
struct hlist_head *new)
628+
{
629+
new->first = old->first;
630+
if (new->first)
631+
new->first->pprev = &new->first;
632+
old->first = NULL;
633+
}
634+
622635
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
623636

624637
#define hlist_for_each(pos, head) \

‎lib/debugobjects.c‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
112112

113113
/*
114114
* Allocate a new object. If the pool is empty, switch off the debugger.
115+
* Must be called with interrupts disabled.
115116
*/
116117
static struct debug_obj *
117118
alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
@@ -148,17 +149,18 @@ alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
148149
static void free_object(struct debug_obj *obj)
149150
{
150151
unsigned long idx = (unsigned long)(obj - obj_static_pool);
152+
unsigned long flags;
151153

152154
if (obj_pool_free < ODEBUG_POOL_SIZE || idx < ODEBUG_POOL_SIZE) {
153-
spin_lock(&pool_lock);
155+
spin_lock_irqsave(&pool_lock, flags);
154156
hlist_add_head(&obj->node, &obj_pool);
155157
obj_pool_free++;
156158
obj_pool_used--;
157-
spin_unlock(&pool_lock);
159+
spin_unlock_irqrestore(&pool_lock, flags);
158160
} else {
159-
spin_lock(&pool_lock);
161+
spin_lock_irqsave(&pool_lock, flags);
160162
obj_pool_used--;
161-
spin_unlock(&pool_lock);
163+
spin_unlock_irqrestore(&pool_lock, flags);
162164
kmem_cache_free(obj_cache, obj);
163165
}
164166
}
@@ -171,6 +173,7 @@ static void debug_objects_oom(void)
171173
{
172174
struct debug_bucket *db = obj_hash;
173175
struct hlist_node *node, *tmp;
176+
HLIST_HEAD(freelist);
174177
struct debug_obj *obj;
175178
unsigned long flags;
176179
int i;
@@ -179,11 +182,14 @@ static void debug_objects_oom(void)
179182

180183
for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
181184
spin_lock_irqsave(&db->lock, flags);
182-
hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
185+
hlist_move_list(&db->list, &freelist);
186+
spin_unlock_irqrestore(&db->lock, flags);
187+
188+
/* Now free them */
189+
hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
183190
hlist_del(&obj->node);
184191
free_object(obj);
185192
}
186-
spin_unlock_irqrestore(&db->lock, flags);
187193
}
188194
}
189195

@@ -498,8 +504,9 @@ void debug_object_free(void *addr, struct debug_obj_descr *descr)
498504
return;
499505
default:
500506
hlist_del(&obj->node);
507+
spin_unlock_irqrestore(&db->lock, flags);
501508
free_object(obj);
502-
break;
509+
return;
503510
}
504511
out_unlock:
505512
spin_unlock_irqrestore(&db->lock, flags);
@@ -510,6 +517,7 @@ static void __debug_check_no_obj_freed(const void *address, unsigned long size)
510517
{
511518
unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
512519
struct hlist_node *node, *tmp;
520+
HLIST_HEAD(freelist);
513521
struct debug_obj_descr *descr;
514522
enum debug_obj_state state;
515523
struct debug_bucket *db;
@@ -545,11 +553,18 @@ static void __debug_check_no_obj_freed(const void *address, unsigned long size)
545553
goto repeat;
546554
default:
547555
hlist_del(&obj->node);
548-
free_object(obj);
556+
hlist_add_head(&obj->node, &freelist);
549557
break;
550558
}
551559
}
552560
spin_unlock_irqrestore(&db->lock, flags);
561+
562+
/* Now free them */
563+
hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
564+
hlist_del(&obj->node);
565+
free_object(obj);
566+
}
567+
553568
if (cnt > debug_objects_maxchain)
554569
debug_objects_maxchain = cnt;
555570
}

0 commit comments

Comments
 (0)