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
11 #define pr_fmt(fmt) "ODEBUG: " fmt
13 #include <linux/debugobjects.h>
14 #include <linux/interrupt.h>
15 #include <linux/sched.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
18 #include <linux/slab.h>
19 #include <linux/hash.h>
21 #define ODEBUG_HASH_BITS 14
22 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
24 #define ODEBUG_POOL_SIZE 1024
25 #define ODEBUG_POOL_MIN_LEVEL 256
27 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
28 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
29 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
32 struct hlist_head list;
36 static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
38 static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
40 static DEFINE_RAW_SPINLOCK(pool_lock);
42 static HLIST_HEAD(obj_pool);
44 static int obj_pool_min_free = ODEBUG_POOL_SIZE;
45 static int obj_pool_free = ODEBUG_POOL_SIZE;
46 static int obj_pool_used;
47 static int obj_pool_max_used;
48 static struct kmem_cache *obj_cache;
50 static int debug_objects_maxchain __read_mostly;
51 static int debug_objects_fixups __read_mostly;
52 static int debug_objects_warnings __read_mostly;
53 static int debug_objects_enabled __read_mostly
54 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
56 static struct debug_obj_descr *descr_test __read_mostly;
59 * Track numbers of kmem_cache_alloc and kmem_cache_free done.
61 static int debug_objects_alloc;
62 static int debug_objects_freed;
64 static void free_obj_work(struct work_struct *work);
65 static DECLARE_WORK(debug_obj_work, free_obj_work);
67 static int __init enable_object_debug(char *str)
69 debug_objects_enabled = 1;
73 static int __init disable_object_debug(char *str)
75 debug_objects_enabled = 0;
79 early_param("debug_objects", enable_object_debug);
80 early_param("no_debug_objects", disable_object_debug);
82 static const char *obj_states[ODEBUG_STATE_MAX] = {
83 [ODEBUG_STATE_NONE] = "none",
84 [ODEBUG_STATE_INIT] = "initialized",
85 [ODEBUG_STATE_INACTIVE] = "inactive",
86 [ODEBUG_STATE_ACTIVE] = "active",
87 [ODEBUG_STATE_DESTROYED] = "destroyed",
88 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
91 static void fill_pool(void)
93 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
94 struct debug_obj *new;
97 if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
100 if (unlikely(!obj_cache))
103 while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
105 new = kmem_cache_zalloc(obj_cache, gfp);
109 raw_spin_lock_irqsave(&pool_lock, flags);
110 hlist_add_head(&new->node, &obj_pool);
111 debug_objects_alloc++;
113 raw_spin_unlock_irqrestore(&pool_lock, flags);
118 * Lookup an object in the hash bucket.
120 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
122 struct debug_obj *obj;
125 hlist_for_each_entry(obj, &b->list, node) {
127 if (obj->object == addr)
130 if (cnt > debug_objects_maxchain)
131 debug_objects_maxchain = cnt;
137 * Allocate a new object. If the pool is empty, switch off the debugger.
138 * Must be called with interrupts disabled.
140 static struct debug_obj *
141 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
143 struct debug_obj *obj = NULL;
145 raw_spin_lock(&pool_lock);
146 if (obj_pool.first) {
147 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
151 obj->state = ODEBUG_STATE_NONE;
153 hlist_del(&obj->node);
155 hlist_add_head(&obj->node, &b->list);
158 if (obj_pool_used > obj_pool_max_used)
159 obj_pool_max_used = obj_pool_used;
162 if (obj_pool_free < obj_pool_min_free)
163 obj_pool_min_free = obj_pool_free;
165 raw_spin_unlock(&pool_lock);
171 * workqueue function to free objects.
173 static void free_obj_work(struct work_struct *work)
175 struct debug_obj *obj;
178 raw_spin_lock_irqsave(&pool_lock, flags);
179 while (obj_pool_free > ODEBUG_POOL_SIZE) {
180 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
181 hlist_del(&obj->node);
183 debug_objects_freed++;
185 * We release pool_lock across kmem_cache_free() to
186 * avoid contention on pool_lock.
188 raw_spin_unlock_irqrestore(&pool_lock, flags);
189 kmem_cache_free(obj_cache, obj);
190 raw_spin_lock_irqsave(&pool_lock, flags);
192 raw_spin_unlock_irqrestore(&pool_lock, flags);
196 * Put the object back into the pool and schedule work to free objects
199 static void free_object(struct debug_obj *obj)
204 raw_spin_lock_irqsave(&pool_lock, flags);
206 * schedule work when the pool is filled and the cache is
209 if (obj_pool_free > ODEBUG_POOL_SIZE && obj_cache)
211 hlist_add_head(&obj->node, &obj_pool);
214 raw_spin_unlock_irqrestore(&pool_lock, flags);
216 schedule_work(&debug_obj_work);
220 * We run out of memory. That means we probably have tons of objects
223 static void debug_objects_oom(void)
225 struct debug_bucket *db = obj_hash;
226 struct hlist_node *tmp;
227 HLIST_HEAD(freelist);
228 struct debug_obj *obj;
232 pr_warn("Out of memory. ODEBUG disabled\n");
234 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
235 raw_spin_lock_irqsave(&db->lock, flags);
236 hlist_move_list(&db->list, &freelist);
237 raw_spin_unlock_irqrestore(&db->lock, flags);
240 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
241 hlist_del(&obj->node);
248 * We use the pfn of the address for the hash. That way we can check
249 * for freed objects simply by checking the affected bucket.
251 static struct debug_bucket *get_bucket(unsigned long addr)
255 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
256 return &obj_hash[hash];
259 static void debug_print_object(struct debug_obj *obj, char *msg)
261 struct debug_obj_descr *descr = obj->descr;
264 if (limit < 5 && descr != descr_test) {
265 void *hint = descr->debug_hint ?
266 descr->debug_hint(obj->object) : NULL;
268 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
269 "object type: %s hint: %pS\n",
270 msg, obj_states[obj->state], obj->astate,
273 debug_objects_warnings++;
277 * Try to repair the damage, so we have a better chance to get useful
281 debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
282 void * addr, enum debug_obj_state state)
284 if (fixup && fixup(addr, state)) {
285 debug_objects_fixups++;
291 static void debug_object_is_on_stack(void *addr, int onstack)
299 is_on_stack = object_is_on_stack(addr);
300 if (is_on_stack == onstack)
305 pr_warn("object is on stack, but not annotated\n");
307 pr_warn("object is not on stack, but annotated\n");
312 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
314 enum debug_obj_state state;
315 struct debug_bucket *db;
316 struct debug_obj *obj;
321 db = get_bucket((unsigned long) addr);
323 raw_spin_lock_irqsave(&db->lock, flags);
325 obj = lookup_object(addr, db);
327 obj = alloc_object(addr, db, descr);
329 debug_objects_enabled = 0;
330 raw_spin_unlock_irqrestore(&db->lock, flags);
334 debug_object_is_on_stack(addr, onstack);
337 switch (obj->state) {
338 case ODEBUG_STATE_NONE:
339 case ODEBUG_STATE_INIT:
340 case ODEBUG_STATE_INACTIVE:
341 obj->state = ODEBUG_STATE_INIT;
344 case ODEBUG_STATE_ACTIVE:
345 debug_print_object(obj, "init");
347 raw_spin_unlock_irqrestore(&db->lock, flags);
348 debug_object_fixup(descr->fixup_init, addr, state);
351 case ODEBUG_STATE_DESTROYED:
352 debug_print_object(obj, "init");
358 raw_spin_unlock_irqrestore(&db->lock, flags);
362 * debug_object_init - debug checks when an object is initialized
363 * @addr: address of the object
364 * @descr: pointer to an object specific debug description structure
366 void debug_object_init(void *addr, struct debug_obj_descr *descr)
368 if (!debug_objects_enabled)
371 __debug_object_init(addr, descr, 0);
373 EXPORT_SYMBOL_GPL(debug_object_init);
376 * debug_object_init_on_stack - debug checks when an object on stack is
378 * @addr: address of the object
379 * @descr: pointer to an object specific debug description structure
381 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
383 if (!debug_objects_enabled)
386 __debug_object_init(addr, descr, 1);
388 EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
391 * debug_object_activate - debug checks when an object is activated
392 * @addr: address of the object
393 * @descr: pointer to an object specific debug description structure
394 * Returns 0 for success, -EINVAL for check failed.
396 int debug_object_activate(void *addr, struct debug_obj_descr *descr)
398 enum debug_obj_state state;
399 struct debug_bucket *db;
400 struct debug_obj *obj;
403 struct debug_obj o = { .object = addr,
404 .state = ODEBUG_STATE_NOTAVAILABLE,
407 if (!debug_objects_enabled)
410 db = get_bucket((unsigned long) addr);
412 raw_spin_lock_irqsave(&db->lock, flags);
414 obj = lookup_object(addr, db);
416 switch (obj->state) {
417 case ODEBUG_STATE_INIT:
418 case ODEBUG_STATE_INACTIVE:
419 obj->state = ODEBUG_STATE_ACTIVE;
423 case ODEBUG_STATE_ACTIVE:
424 debug_print_object(obj, "activate");
426 raw_spin_unlock_irqrestore(&db->lock, flags);
427 ret = debug_object_fixup(descr->fixup_activate, addr, state);
428 return ret ? 0 : -EINVAL;
430 case ODEBUG_STATE_DESTROYED:
431 debug_print_object(obj, "activate");
438 raw_spin_unlock_irqrestore(&db->lock, flags);
442 raw_spin_unlock_irqrestore(&db->lock, flags);
444 * We are here when a static object is activated. We
445 * let the type specific code confirm whether this is
446 * true or not. if true, we just make sure that the
447 * static object is tracked in the object tracker. If
448 * not, this must be a bug, so we try to fix it up.
450 if (descr->is_static_object && descr->is_static_object(addr)) {
451 /* track this static object */
452 debug_object_init(addr, descr);
453 debug_object_activate(addr, descr);
455 debug_print_object(&o, "activate");
456 ret = debug_object_fixup(descr->fixup_activate, addr,
457 ODEBUG_STATE_NOTAVAILABLE);
458 return ret ? 0 : -EINVAL;
462 EXPORT_SYMBOL_GPL(debug_object_activate);
465 * debug_object_deactivate - debug checks when an object is deactivated
466 * @addr: address of the object
467 * @descr: pointer to an object specific debug description structure
469 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
471 struct debug_bucket *db;
472 struct debug_obj *obj;
475 if (!debug_objects_enabled)
478 db = get_bucket((unsigned long) addr);
480 raw_spin_lock_irqsave(&db->lock, flags);
482 obj = lookup_object(addr, db);
484 switch (obj->state) {
485 case ODEBUG_STATE_INIT:
486 case ODEBUG_STATE_INACTIVE:
487 case ODEBUG_STATE_ACTIVE:
489 obj->state = ODEBUG_STATE_INACTIVE;
491 debug_print_object(obj, "deactivate");
494 case ODEBUG_STATE_DESTROYED:
495 debug_print_object(obj, "deactivate");
501 struct debug_obj o = { .object = addr,
502 .state = ODEBUG_STATE_NOTAVAILABLE,
505 debug_print_object(&o, "deactivate");
508 raw_spin_unlock_irqrestore(&db->lock, flags);
510 EXPORT_SYMBOL_GPL(debug_object_deactivate);
513 * debug_object_destroy - debug checks when an object is destroyed
514 * @addr: address of the object
515 * @descr: pointer to an object specific debug description structure
517 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
519 enum debug_obj_state state;
520 struct debug_bucket *db;
521 struct debug_obj *obj;
524 if (!debug_objects_enabled)
527 db = get_bucket((unsigned long) addr);
529 raw_spin_lock_irqsave(&db->lock, flags);
531 obj = lookup_object(addr, db);
535 switch (obj->state) {
536 case ODEBUG_STATE_NONE:
537 case ODEBUG_STATE_INIT:
538 case ODEBUG_STATE_INACTIVE:
539 obj->state = ODEBUG_STATE_DESTROYED;
541 case ODEBUG_STATE_ACTIVE:
542 debug_print_object(obj, "destroy");
544 raw_spin_unlock_irqrestore(&db->lock, flags);
545 debug_object_fixup(descr->fixup_destroy, addr, state);
548 case ODEBUG_STATE_DESTROYED:
549 debug_print_object(obj, "destroy");
555 raw_spin_unlock_irqrestore(&db->lock, flags);
557 EXPORT_SYMBOL_GPL(debug_object_destroy);
560 * debug_object_free - debug checks when an object is freed
561 * @addr: address of the object
562 * @descr: pointer to an object specific debug description structure
564 void debug_object_free(void *addr, struct debug_obj_descr *descr)
566 enum debug_obj_state state;
567 struct debug_bucket *db;
568 struct debug_obj *obj;
571 if (!debug_objects_enabled)
574 db = get_bucket((unsigned long) addr);
576 raw_spin_lock_irqsave(&db->lock, flags);
578 obj = lookup_object(addr, db);
582 switch (obj->state) {
583 case ODEBUG_STATE_ACTIVE:
584 debug_print_object(obj, "free");
586 raw_spin_unlock_irqrestore(&db->lock, flags);
587 debug_object_fixup(descr->fixup_free, addr, state);
590 hlist_del(&obj->node);
591 raw_spin_unlock_irqrestore(&db->lock, flags);
596 raw_spin_unlock_irqrestore(&db->lock, flags);
598 EXPORT_SYMBOL_GPL(debug_object_free);
601 * debug_object_assert_init - debug checks when object should be init-ed
602 * @addr: address of the object
603 * @descr: pointer to an object specific debug description structure
605 void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
607 struct debug_bucket *db;
608 struct debug_obj *obj;
611 if (!debug_objects_enabled)
614 db = get_bucket((unsigned long) addr);
616 raw_spin_lock_irqsave(&db->lock, flags);
618 obj = lookup_object(addr, db);
620 struct debug_obj o = { .object = addr,
621 .state = ODEBUG_STATE_NOTAVAILABLE,
624 raw_spin_unlock_irqrestore(&db->lock, flags);
626 * Maybe the object is static, and we let the type specific
627 * code confirm. Track this static object if true, else invoke
630 if (descr->is_static_object && descr->is_static_object(addr)) {
631 /* Track this static object */
632 debug_object_init(addr, descr);
634 debug_print_object(&o, "assert_init");
635 debug_object_fixup(descr->fixup_assert_init, addr,
636 ODEBUG_STATE_NOTAVAILABLE);
641 raw_spin_unlock_irqrestore(&db->lock, flags);
643 EXPORT_SYMBOL_GPL(debug_object_assert_init);
646 * debug_object_active_state - debug checks object usage state machine
647 * @addr: address of the object
648 * @descr: pointer to an object specific debug description structure
649 * @expect: expected state
650 * @next: state to move to if expected state is found
653 debug_object_active_state(void *addr, struct debug_obj_descr *descr,
654 unsigned int expect, unsigned int next)
656 struct debug_bucket *db;
657 struct debug_obj *obj;
660 if (!debug_objects_enabled)
663 db = get_bucket((unsigned long) addr);
665 raw_spin_lock_irqsave(&db->lock, flags);
667 obj = lookup_object(addr, db);
669 switch (obj->state) {
670 case ODEBUG_STATE_ACTIVE:
671 if (obj->astate == expect)
674 debug_print_object(obj, "active_state");
678 debug_print_object(obj, "active_state");
682 struct debug_obj o = { .object = addr,
683 .state = ODEBUG_STATE_NOTAVAILABLE,
686 debug_print_object(&o, "active_state");
689 raw_spin_unlock_irqrestore(&db->lock, flags);
691 EXPORT_SYMBOL_GPL(debug_object_active_state);
693 #ifdef CONFIG_DEBUG_OBJECTS_FREE
694 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
696 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
697 struct hlist_node *tmp;
698 HLIST_HEAD(freelist);
699 struct debug_obj_descr *descr;
700 enum debug_obj_state state;
701 struct debug_bucket *db;
702 struct debug_obj *obj;
705 saddr = (unsigned long) address;
706 eaddr = saddr + size;
707 paddr = saddr & ODEBUG_CHUNK_MASK;
708 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
709 chunks >>= ODEBUG_CHUNK_SHIFT;
711 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
712 db = get_bucket(paddr);
716 raw_spin_lock_irqsave(&db->lock, flags);
717 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
719 oaddr = (unsigned long) obj->object;
720 if (oaddr < saddr || oaddr >= eaddr)
723 switch (obj->state) {
724 case ODEBUG_STATE_ACTIVE:
725 debug_print_object(obj, "free");
728 raw_spin_unlock_irqrestore(&db->lock, flags);
729 debug_object_fixup(descr->fixup_free,
730 (void *) oaddr, state);
733 hlist_del(&obj->node);
734 hlist_add_head(&obj->node, &freelist);
738 raw_spin_unlock_irqrestore(&db->lock, flags);
741 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
742 hlist_del(&obj->node);
746 if (cnt > debug_objects_maxchain)
747 debug_objects_maxchain = cnt;
751 void debug_check_no_obj_freed(const void *address, unsigned long size)
753 if (debug_objects_enabled)
754 __debug_check_no_obj_freed(address, size);
758 #ifdef CONFIG_DEBUG_FS
760 static int debug_stats_show(struct seq_file *m, void *v)
762 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
763 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
764 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
765 seq_printf(m, "pool_free :%d\n", obj_pool_free);
766 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
767 seq_printf(m, "pool_used :%d\n", obj_pool_used);
768 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
769 seq_printf(m, "objects_alloc :%d\n", debug_objects_alloc);
770 seq_printf(m, "objects_freed :%d\n", debug_objects_freed);
774 static int debug_stats_open(struct inode *inode, struct file *filp)
776 return single_open(filp, debug_stats_show, NULL);
779 static const struct file_operations debug_stats_fops = {
780 .open = debug_stats_open,
783 .release = single_release,
786 static int __init debug_objects_init_debugfs(void)
788 struct dentry *dbgdir, *dbgstats;
790 if (!debug_objects_enabled)
793 dbgdir = debugfs_create_dir("debug_objects", NULL);
797 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
805 debugfs_remove(dbgdir);
809 __initcall(debug_objects_init_debugfs);
812 static inline void debug_objects_init_debugfs(void) { }
815 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
817 /* Random data structure for the self test */
819 unsigned long dummy1[6];
821 unsigned long dummy2[3];
824 static __initdata struct debug_obj_descr descr_type_test;
826 static bool __init is_static_object(void *addr)
828 struct self_test *obj = addr;
830 return obj->static_init;
834 * fixup_init is called when:
835 * - an active object is initialized
837 static bool __init fixup_init(void *addr, enum debug_obj_state state)
839 struct self_test *obj = addr;
842 case ODEBUG_STATE_ACTIVE:
843 debug_object_deactivate(obj, &descr_type_test);
844 debug_object_init(obj, &descr_type_test);
852 * fixup_activate is called when:
853 * - an active object is activated
854 * - an unknown non-static object is activated
856 static bool __init fixup_activate(void *addr, enum debug_obj_state state)
858 struct self_test *obj = addr;
861 case ODEBUG_STATE_NOTAVAILABLE:
863 case ODEBUG_STATE_ACTIVE:
864 debug_object_deactivate(obj, &descr_type_test);
865 debug_object_activate(obj, &descr_type_test);
874 * fixup_destroy is called when:
875 * - an active object is destroyed
877 static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
879 struct self_test *obj = addr;
882 case ODEBUG_STATE_ACTIVE:
883 debug_object_deactivate(obj, &descr_type_test);
884 debug_object_destroy(obj, &descr_type_test);
892 * fixup_free is called when:
893 * - an active object is freed
895 static bool __init fixup_free(void *addr, enum debug_obj_state state)
897 struct self_test *obj = addr;
900 case ODEBUG_STATE_ACTIVE:
901 debug_object_deactivate(obj, &descr_type_test);
902 debug_object_free(obj, &descr_type_test);
910 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
912 struct debug_bucket *db;
913 struct debug_obj *obj;
917 db = get_bucket((unsigned long) addr);
919 raw_spin_lock_irqsave(&db->lock, flags);
921 obj = lookup_object(addr, db);
922 if (!obj && state != ODEBUG_STATE_NONE) {
923 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
926 if (obj && obj->state != state) {
927 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
931 if (fixups != debug_objects_fixups) {
932 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
933 fixups, debug_objects_fixups);
936 if (warnings != debug_objects_warnings) {
937 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
938 warnings, debug_objects_warnings);
943 raw_spin_unlock_irqrestore(&db->lock, flags);
945 debug_objects_enabled = 0;
949 static __initdata struct debug_obj_descr descr_type_test = {
951 .is_static_object = is_static_object,
952 .fixup_init = fixup_init,
953 .fixup_activate = fixup_activate,
954 .fixup_destroy = fixup_destroy,
955 .fixup_free = fixup_free,
958 static __initdata struct self_test obj = { .static_init = 0 };
960 static void __init debug_objects_selftest(void)
962 int fixups, oldfixups, warnings, oldwarnings;
965 local_irq_save(flags);
967 fixups = oldfixups = debug_objects_fixups;
968 warnings = oldwarnings = debug_objects_warnings;
969 descr_test = &descr_type_test;
971 debug_object_init(&obj, &descr_type_test);
972 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
974 debug_object_activate(&obj, &descr_type_test);
975 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
977 debug_object_activate(&obj, &descr_type_test);
978 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
980 debug_object_deactivate(&obj, &descr_type_test);
981 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
983 debug_object_destroy(&obj, &descr_type_test);
984 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
986 debug_object_init(&obj, &descr_type_test);
987 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
989 debug_object_activate(&obj, &descr_type_test);
990 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
992 debug_object_deactivate(&obj, &descr_type_test);
993 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
995 debug_object_free(&obj, &descr_type_test);
996 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1000 debug_object_activate(&obj, &descr_type_test);
1001 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1003 debug_object_init(&obj, &descr_type_test);
1004 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1006 debug_object_free(&obj, &descr_type_test);
1007 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1010 #ifdef CONFIG_DEBUG_OBJECTS_FREE
1011 debug_object_init(&obj, &descr_type_test);
1012 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1014 debug_object_activate(&obj, &descr_type_test);
1015 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1017 __debug_check_no_obj_freed(&obj, sizeof(obj));
1018 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1021 pr_info("selftest passed\n");
1024 debug_objects_fixups = oldfixups;
1025 debug_objects_warnings = oldwarnings;
1028 local_irq_restore(flags);
1031 static inline void debug_objects_selftest(void) { }
1035 * Called during early boot to initialize the hash buckets and link
1036 * the static object pool objects into the poll list. After this call
1037 * the object tracker is fully operational.
1039 void __init debug_objects_early_init(void)
1043 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1044 raw_spin_lock_init(&obj_hash[i].lock);
1046 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1047 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1051 * Convert the statically allocated objects to dynamic ones:
1053 static int __init debug_objects_replace_static_objects(void)
1055 struct debug_bucket *db = obj_hash;
1056 struct hlist_node *tmp;
1057 struct debug_obj *obj, *new;
1058 HLIST_HEAD(objects);
1061 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1062 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1065 hlist_add_head(&obj->node, &objects);
1069 * When debug_objects_mem_init() is called we know that only
1070 * one CPU is up, so disabling interrupts is enough
1071 * protection. This avoids the lockdep hell of lock ordering.
1073 local_irq_disable();
1075 /* Remove the statically allocated objects from the pool */
1076 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1077 hlist_del(&obj->node);
1078 /* Move the allocated objects to the pool */
1079 hlist_move_list(&objects, &obj_pool);
1081 /* Replace the active object references */
1082 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1083 hlist_move_list(&db->list, &objects);
1085 hlist_for_each_entry(obj, &objects, node) {
1086 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1087 hlist_del(&new->node);
1088 /* copy object data */
1090 hlist_add_head(&new->node, &db->list);
1096 pr_debug("%d of %d active objects replaced\n",
1097 cnt, obj_pool_used);
1100 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1101 hlist_del(&obj->node);
1102 kmem_cache_free(obj_cache, obj);
1108 * Called after the kmem_caches are functional to setup a dedicated
1109 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1110 * prevents that the debug code is called on kmem_cache_free() for the
1111 * debug tracker objects to avoid recursive calls.
1113 void __init debug_objects_mem_init(void)
1115 if (!debug_objects_enabled)
1118 obj_cache = kmem_cache_create("debug_objects_cache",
1119 sizeof (struct debug_obj), 0,
1120 SLAB_DEBUG_OBJECTS, NULL);
1122 if (!obj_cache || debug_objects_replace_static_objects()) {
1123 debug_objects_enabled = 0;
1125 kmem_cache_destroy(obj_cache);
1126 pr_warn("out of memory.\n");
1128 debug_objects_selftest();