2 * Generic infrastructure for lifetime debugging of objects.
4 * Started by Thomas Gleixner
6 * Copyright (C) 2008, Thomas Gleixner <tglx@linutronix.de>
8 * For licencing details see kernel-base/COPYING
10 #include <linux/debugobjects.h>
11 #include <linux/interrupt.h>
12 #include <linux/seq_file.h>
13 #include <linux/debugfs.h>
14 #include <linux/hash.h>
16 #define ODEBUG_HASH_BITS 14
17 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
19 #define ODEBUG_POOL_SIZE 512
20 #define ODEBUG_POOL_MIN_LEVEL 256
22 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
23 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
24 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
27 struct hlist_head list;
31 static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
33 static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE];
35 static DEFINE_SPINLOCK(pool_lock);
37 static HLIST_HEAD(obj_pool);
39 static int obj_pool_min_free = ODEBUG_POOL_SIZE;
40 static int obj_pool_free = ODEBUG_POOL_SIZE;
41 static int obj_pool_used;
42 static int obj_pool_max_used;
43 static struct kmem_cache *obj_cache;
45 static int debug_objects_maxchain __read_mostly;
46 static int debug_objects_fixups __read_mostly;
47 static int debug_objects_warnings __read_mostly;
48 static int debug_objects_enabled __read_mostly;
49 static struct debug_obj_descr *descr_test __read_mostly;
51 static int __init enable_object_debug(char *str)
53 debug_objects_enabled = 1;
56 early_param("debug_objects", enable_object_debug);
58 static const char *obj_states[ODEBUG_STATE_MAX] = {
59 [ODEBUG_STATE_NONE] = "none",
60 [ODEBUG_STATE_INIT] = "initialized",
61 [ODEBUG_STATE_INACTIVE] = "inactive",
62 [ODEBUG_STATE_ACTIVE] = "active",
63 [ODEBUG_STATE_DESTROYED] = "destroyed",
64 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
67 static int fill_pool(void)
69 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
70 struct debug_obj *new;
73 if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
76 if (unlikely(!obj_cache))
79 while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
81 new = kmem_cache_zalloc(obj_cache, gfp);
85 spin_lock_irqsave(&pool_lock, flags);
86 hlist_add_head(&new->node, &obj_pool);
88 spin_unlock_irqrestore(&pool_lock, flags);
94 * Lookup an object in the hash bucket.
96 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
98 struct hlist_node *node;
99 struct debug_obj *obj;
102 hlist_for_each_entry(obj, node, &b->list, node) {
104 if (obj->object == addr)
107 if (cnt > debug_objects_maxchain)
108 debug_objects_maxchain = cnt;
114 * Allocate a new object. If the pool is empty, switch off the debugger.
116 static struct debug_obj *
117 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
119 struct debug_obj *obj = NULL;
121 spin_lock(&pool_lock);
122 if (obj_pool.first) {
123 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
127 obj->state = ODEBUG_STATE_NONE;
128 hlist_del(&obj->node);
130 hlist_add_head(&obj->node, &b->list);
133 if (obj_pool_used > obj_pool_max_used)
134 obj_pool_max_used = obj_pool_used;
137 if (obj_pool_free < obj_pool_min_free)
138 obj_pool_min_free = obj_pool_free;
140 spin_unlock(&pool_lock);
146 * Put the object back into the pool or give it back to kmem_cache:
148 static void free_object(struct debug_obj *obj)
150 unsigned long idx = (unsigned long)(obj - obj_static_pool);
152 if (obj_pool_free < ODEBUG_POOL_SIZE || idx < ODEBUG_POOL_SIZE) {
153 spin_lock(&pool_lock);
154 hlist_add_head(&obj->node, &obj_pool);
157 spin_unlock(&pool_lock);
159 spin_lock(&pool_lock);
161 spin_unlock(&pool_lock);
162 kmem_cache_free(obj_cache, obj);
167 * We run out of memory. That means we probably have tons of objects
170 static void debug_objects_oom(void)
172 struct debug_bucket *db = obj_hash;
173 struct hlist_node *node, *tmp;
174 struct debug_obj *obj;
178 printk(KERN_WARNING "ODEBUG: Out of memory. ODEBUG disabled\n");
180 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
181 spin_lock_irqsave(&db->lock, flags);
182 hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
183 hlist_del(&obj->node);
186 spin_unlock_irqrestore(&db->lock, flags);
191 * We use the pfn of the address for the hash. That way we can check
192 * for freed objects simply by checking the affected bucket.
194 static struct debug_bucket *get_bucket(unsigned long addr)
198 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
199 return &obj_hash[hash];
202 static void debug_print_object(struct debug_obj *obj, char *msg)
206 if (limit < 5 && obj->descr != descr_test) {
208 WARN(1, KERN_ERR "ODEBUG: %s %s object type: %s\n", msg,
209 obj_states[obj->state], obj->descr->name);
211 debug_objects_warnings++;
215 * Try to repair the damage, so we have a better chance to get useful
219 debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state),
220 void * addr, enum debug_obj_state state)
223 debug_objects_fixups += fixup(addr, state);
226 static void debug_object_is_on_stack(void *addr, int onstack)
234 is_on_stack = object_is_on_stack(addr);
235 if (is_on_stack == onstack)
241 "ODEBUG: object is on stack, but not annotated\n");
244 "ODEBUG: object is not on stack, but annotated\n");
249 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
251 enum debug_obj_state state;
252 struct debug_bucket *db;
253 struct debug_obj *obj;
258 db = get_bucket((unsigned long) addr);
260 spin_lock_irqsave(&db->lock, flags);
262 obj = lookup_object(addr, db);
264 obj = alloc_object(addr, db, descr);
266 debug_objects_enabled = 0;
267 spin_unlock_irqrestore(&db->lock, flags);
271 debug_object_is_on_stack(addr, onstack);
274 switch (obj->state) {
275 case ODEBUG_STATE_NONE:
276 case ODEBUG_STATE_INIT:
277 case ODEBUG_STATE_INACTIVE:
278 obj->state = ODEBUG_STATE_INIT;
281 case ODEBUG_STATE_ACTIVE:
282 debug_print_object(obj, "init");
284 spin_unlock_irqrestore(&db->lock, flags);
285 debug_object_fixup(descr->fixup_init, addr, state);
288 case ODEBUG_STATE_DESTROYED:
289 debug_print_object(obj, "init");
295 spin_unlock_irqrestore(&db->lock, flags);
299 * debug_object_init - debug checks when an object is initialized
300 * @addr: address of the object
301 * @descr: pointer to an object specific debug description structure
303 void debug_object_init(void *addr, struct debug_obj_descr *descr)
305 if (!debug_objects_enabled)
308 __debug_object_init(addr, descr, 0);
312 * debug_object_init_on_stack - debug checks when an object on stack is
314 * @addr: address of the object
315 * @descr: pointer to an object specific debug description structure
317 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
319 if (!debug_objects_enabled)
322 __debug_object_init(addr, descr, 1);
326 * debug_object_activate - debug checks when an object is activated
327 * @addr: address of the object
328 * @descr: pointer to an object specific debug description structure
330 void debug_object_activate(void *addr, struct debug_obj_descr *descr)
332 enum debug_obj_state state;
333 struct debug_bucket *db;
334 struct debug_obj *obj;
337 if (!debug_objects_enabled)
340 db = get_bucket((unsigned long) addr);
342 spin_lock_irqsave(&db->lock, flags);
344 obj = lookup_object(addr, db);
346 switch (obj->state) {
347 case ODEBUG_STATE_INIT:
348 case ODEBUG_STATE_INACTIVE:
349 obj->state = ODEBUG_STATE_ACTIVE;
352 case ODEBUG_STATE_ACTIVE:
353 debug_print_object(obj, "activate");
355 spin_unlock_irqrestore(&db->lock, flags);
356 debug_object_fixup(descr->fixup_activate, addr, state);
359 case ODEBUG_STATE_DESTROYED:
360 debug_print_object(obj, "activate");
365 spin_unlock_irqrestore(&db->lock, flags);
369 spin_unlock_irqrestore(&db->lock, flags);
371 * This happens when a static object is activated. We
372 * let the type specific code decide whether this is
375 debug_object_fixup(descr->fixup_activate, addr,
376 ODEBUG_STATE_NOTAVAILABLE);
380 * debug_object_deactivate - debug checks when an object is deactivated
381 * @addr: address of the object
382 * @descr: pointer to an object specific debug description structure
384 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
386 struct debug_bucket *db;
387 struct debug_obj *obj;
390 if (!debug_objects_enabled)
393 db = get_bucket((unsigned long) addr);
395 spin_lock_irqsave(&db->lock, flags);
397 obj = lookup_object(addr, db);
399 switch (obj->state) {
400 case ODEBUG_STATE_INIT:
401 case ODEBUG_STATE_INACTIVE:
402 case ODEBUG_STATE_ACTIVE:
403 obj->state = ODEBUG_STATE_INACTIVE;
406 case ODEBUG_STATE_DESTROYED:
407 debug_print_object(obj, "deactivate");
413 struct debug_obj o = { .object = addr,
414 .state = ODEBUG_STATE_NOTAVAILABLE,
417 debug_print_object(&o, "deactivate");
420 spin_unlock_irqrestore(&db->lock, flags);
424 * debug_object_destroy - debug checks when an object is destroyed
425 * @addr: address of the object
426 * @descr: pointer to an object specific debug description structure
428 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
430 enum debug_obj_state state;
431 struct debug_bucket *db;
432 struct debug_obj *obj;
435 if (!debug_objects_enabled)
438 db = get_bucket((unsigned long) addr);
440 spin_lock_irqsave(&db->lock, flags);
442 obj = lookup_object(addr, db);
446 switch (obj->state) {
447 case ODEBUG_STATE_NONE:
448 case ODEBUG_STATE_INIT:
449 case ODEBUG_STATE_INACTIVE:
450 obj->state = ODEBUG_STATE_DESTROYED;
452 case ODEBUG_STATE_ACTIVE:
453 debug_print_object(obj, "destroy");
455 spin_unlock_irqrestore(&db->lock, flags);
456 debug_object_fixup(descr->fixup_destroy, addr, state);
459 case ODEBUG_STATE_DESTROYED:
460 debug_print_object(obj, "destroy");
466 spin_unlock_irqrestore(&db->lock, flags);
470 * debug_object_free - debug checks when an object is freed
471 * @addr: address of the object
472 * @descr: pointer to an object specific debug description structure
474 void debug_object_free(void *addr, struct debug_obj_descr *descr)
476 enum debug_obj_state state;
477 struct debug_bucket *db;
478 struct debug_obj *obj;
481 if (!debug_objects_enabled)
484 db = get_bucket((unsigned long) addr);
486 spin_lock_irqsave(&db->lock, flags);
488 obj = lookup_object(addr, db);
492 switch (obj->state) {
493 case ODEBUG_STATE_ACTIVE:
494 debug_print_object(obj, "free");
496 spin_unlock_irqrestore(&db->lock, flags);
497 debug_object_fixup(descr->fixup_free, addr, state);
500 hlist_del(&obj->node);
505 spin_unlock_irqrestore(&db->lock, flags);
508 #ifdef CONFIG_DEBUG_OBJECTS_FREE
509 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
511 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
512 struct hlist_node *node, *tmp;
513 struct debug_obj_descr *descr;
514 enum debug_obj_state state;
515 struct debug_bucket *db;
516 struct debug_obj *obj;
519 saddr = (unsigned long) address;
520 eaddr = saddr + size;
521 paddr = saddr & ODEBUG_CHUNK_MASK;
522 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
523 chunks >>= ODEBUG_CHUNK_SHIFT;
525 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
526 db = get_bucket(paddr);
530 spin_lock_irqsave(&db->lock, flags);
531 hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
533 oaddr = (unsigned long) obj->object;
534 if (oaddr < saddr || oaddr >= eaddr)
537 switch (obj->state) {
538 case ODEBUG_STATE_ACTIVE:
539 debug_print_object(obj, "free");
542 spin_unlock_irqrestore(&db->lock, flags);
543 debug_object_fixup(descr->fixup_free,
544 (void *) oaddr, state);
547 hlist_del(&obj->node);
552 spin_unlock_irqrestore(&db->lock, flags);
553 if (cnt > debug_objects_maxchain)
554 debug_objects_maxchain = cnt;
558 void debug_check_no_obj_freed(const void *address, unsigned long size)
560 if (debug_objects_enabled)
561 __debug_check_no_obj_freed(address, size);
565 #ifdef CONFIG_DEBUG_FS
567 static int debug_stats_show(struct seq_file *m, void *v)
569 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
570 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
571 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
572 seq_printf(m, "pool_free :%d\n", obj_pool_free);
573 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
574 seq_printf(m, "pool_used :%d\n", obj_pool_used);
575 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
579 static int debug_stats_open(struct inode *inode, struct file *filp)
581 return single_open(filp, debug_stats_show, NULL);
584 static const struct file_operations debug_stats_fops = {
585 .open = debug_stats_open,
588 .release = single_release,
591 static int __init debug_objects_init_debugfs(void)
593 struct dentry *dbgdir, *dbgstats;
595 if (!debug_objects_enabled)
598 dbgdir = debugfs_create_dir("debug_objects", NULL);
602 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
610 debugfs_remove(dbgdir);
614 __initcall(debug_objects_init_debugfs);
617 static inline void debug_objects_init_debugfs(void) { }
620 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
622 /* Random data structure for the self test */
624 unsigned long dummy1[6];
626 unsigned long dummy2[3];
629 static __initdata struct debug_obj_descr descr_type_test;
632 * fixup_init is called when:
633 * - an active object is initialized
635 static int __init fixup_init(void *addr, enum debug_obj_state state)
637 struct self_test *obj = addr;
640 case ODEBUG_STATE_ACTIVE:
641 debug_object_deactivate(obj, &descr_type_test);
642 debug_object_init(obj, &descr_type_test);
650 * fixup_activate is called when:
651 * - an active object is activated
652 * - an unknown object is activated (might be a statically initialized object)
654 static int __init fixup_activate(void *addr, enum debug_obj_state state)
656 struct self_test *obj = addr;
659 case ODEBUG_STATE_NOTAVAILABLE:
660 if (obj->static_init == 1) {
661 debug_object_init(obj, &descr_type_test);
662 debug_object_activate(obj, &descr_type_test);
664 * Real code should return 0 here ! This is
665 * not a fixup of some bad behaviour. We
666 * merily call the debug_init function to keep
667 * track of the object.
671 /* Real code needs to emit a warning here */
675 case ODEBUG_STATE_ACTIVE:
676 debug_object_deactivate(obj, &descr_type_test);
677 debug_object_activate(obj, &descr_type_test);
686 * fixup_destroy is called when:
687 * - an active object is destroyed
689 static int __init fixup_destroy(void *addr, enum debug_obj_state state)
691 struct self_test *obj = addr;
694 case ODEBUG_STATE_ACTIVE:
695 debug_object_deactivate(obj, &descr_type_test);
696 debug_object_destroy(obj, &descr_type_test);
704 * fixup_free is called when:
705 * - an active object is freed
707 static int __init fixup_free(void *addr, enum debug_obj_state state)
709 struct self_test *obj = addr;
712 case ODEBUG_STATE_ACTIVE:
713 debug_object_deactivate(obj, &descr_type_test);
714 debug_object_free(obj, &descr_type_test);
722 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
724 struct debug_bucket *db;
725 struct debug_obj *obj;
729 db = get_bucket((unsigned long) addr);
731 spin_lock_irqsave(&db->lock, flags);
733 obj = lookup_object(addr, db);
734 if (!obj && state != ODEBUG_STATE_NONE) {
735 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
738 if (obj && obj->state != state) {
739 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
743 if (fixups != debug_objects_fixups) {
744 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
745 fixups, debug_objects_fixups);
748 if (warnings != debug_objects_warnings) {
749 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
750 warnings, debug_objects_warnings);
755 spin_unlock_irqrestore(&db->lock, flags);
757 debug_objects_enabled = 0;
761 static __initdata struct debug_obj_descr descr_type_test = {
763 .fixup_init = fixup_init,
764 .fixup_activate = fixup_activate,
765 .fixup_destroy = fixup_destroy,
766 .fixup_free = fixup_free,
769 static __initdata struct self_test obj = { .static_init = 0 };
771 static void __init debug_objects_selftest(void)
773 int fixups, oldfixups, warnings, oldwarnings;
776 local_irq_save(flags);
778 fixups = oldfixups = debug_objects_fixups;
779 warnings = oldwarnings = debug_objects_warnings;
780 descr_test = &descr_type_test;
782 debug_object_init(&obj, &descr_type_test);
783 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
785 debug_object_activate(&obj, &descr_type_test);
786 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
788 debug_object_activate(&obj, &descr_type_test);
789 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
791 debug_object_deactivate(&obj, &descr_type_test);
792 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
794 debug_object_destroy(&obj, &descr_type_test);
795 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
797 debug_object_init(&obj, &descr_type_test);
798 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
800 debug_object_activate(&obj, &descr_type_test);
801 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
803 debug_object_deactivate(&obj, &descr_type_test);
804 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
806 debug_object_free(&obj, &descr_type_test);
807 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
811 debug_object_activate(&obj, &descr_type_test);
812 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
814 debug_object_init(&obj, &descr_type_test);
815 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
817 debug_object_free(&obj, &descr_type_test);
818 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
821 #ifdef CONFIG_DEBUG_OBJECTS_FREE
822 debug_object_init(&obj, &descr_type_test);
823 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
825 debug_object_activate(&obj, &descr_type_test);
826 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
828 __debug_check_no_obj_freed(&obj, sizeof(obj));
829 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
832 printk(KERN_INFO "ODEBUG: selftest passed\n");
835 debug_objects_fixups = oldfixups;
836 debug_objects_warnings = oldwarnings;
839 local_irq_restore(flags);
842 static inline void debug_objects_selftest(void) { }
846 * Called during early boot to initialize the hash buckets and link
847 * the static object pool objects into the poll list. After this call
848 * the object tracker is fully operational.
850 void __init debug_objects_early_init(void)
854 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
855 spin_lock_init(&obj_hash[i].lock);
857 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
858 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
862 * Called after the kmem_caches are functional to setup a dedicated
863 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
864 * prevents that the debug code is called on kmem_cache_free() for the
865 * debug tracker objects to avoid recursive calls.
867 void __init debug_objects_mem_init(void)
869 if (!debug_objects_enabled)
872 obj_cache = kmem_cache_create("debug_objects_cache",
873 sizeof (struct debug_obj), 0,
874 SLAB_DEBUG_OBJECTS, NULL);
877 debug_objects_enabled = 0;
879 debug_objects_selftest();