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;
55 static int debug_objects_pool_size __read_mostly
57 static int debug_objects_pool_min_level __read_mostly
58 = ODEBUG_POOL_MIN_LEVEL;
59 static struct debug_obj_descr *descr_test __read_mostly;
62 * Track numbers of kmem_cache_alloc and kmem_cache_free done.
64 static int debug_objects_alloc;
65 static int debug_objects_freed;
67 static void free_obj_work(struct work_struct *work);
68 static DECLARE_WORK(debug_obj_work, free_obj_work);
70 static int __init enable_object_debug(char *str)
72 debug_objects_enabled = 1;
76 static int __init disable_object_debug(char *str)
78 debug_objects_enabled = 0;
82 early_param("debug_objects", enable_object_debug);
83 early_param("no_debug_objects", disable_object_debug);
85 static const char *obj_states[ODEBUG_STATE_MAX] = {
86 [ODEBUG_STATE_NONE] = "none",
87 [ODEBUG_STATE_INIT] = "initialized",
88 [ODEBUG_STATE_INACTIVE] = "inactive",
89 [ODEBUG_STATE_ACTIVE] = "active",
90 [ODEBUG_STATE_DESTROYED] = "destroyed",
91 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
94 static void fill_pool(void)
96 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
97 struct debug_obj *new;
100 if (likely(obj_pool_free >= debug_objects_pool_min_level))
103 if (unlikely(!obj_cache))
106 while (obj_pool_free < debug_objects_pool_min_level) {
108 new = kmem_cache_zalloc(obj_cache, gfp);
112 raw_spin_lock_irqsave(&pool_lock, flags);
113 hlist_add_head(&new->node, &obj_pool);
114 debug_objects_alloc++;
116 raw_spin_unlock_irqrestore(&pool_lock, flags);
121 * Lookup an object in the hash bucket.
123 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
125 struct debug_obj *obj;
128 hlist_for_each_entry(obj, &b->list, node) {
130 if (obj->object == addr)
133 if (cnt > debug_objects_maxchain)
134 debug_objects_maxchain = cnt;
140 * Allocate a new object. If the pool is empty, switch off the debugger.
141 * Must be called with interrupts disabled.
143 static struct debug_obj *
144 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
146 struct debug_obj *obj = NULL;
148 raw_spin_lock(&pool_lock);
149 if (obj_pool.first) {
150 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
154 obj->state = ODEBUG_STATE_NONE;
156 hlist_del(&obj->node);
158 hlist_add_head(&obj->node, &b->list);
161 if (obj_pool_used > obj_pool_max_used)
162 obj_pool_max_used = obj_pool_used;
165 if (obj_pool_free < obj_pool_min_free)
166 obj_pool_min_free = obj_pool_free;
168 raw_spin_unlock(&pool_lock);
174 * workqueue function to free objects.
176 static void free_obj_work(struct work_struct *work)
178 struct debug_obj *obj;
181 raw_spin_lock_irqsave(&pool_lock, flags);
182 while (obj_pool_free > debug_objects_pool_size) {
183 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
184 hlist_del(&obj->node);
186 debug_objects_freed++;
188 * We release pool_lock across kmem_cache_free() to
189 * avoid contention on pool_lock.
191 raw_spin_unlock_irqrestore(&pool_lock, flags);
192 kmem_cache_free(obj_cache, obj);
193 raw_spin_lock_irqsave(&pool_lock, flags);
195 raw_spin_unlock_irqrestore(&pool_lock, flags);
199 * Put the object back into the pool and schedule work to free objects
202 static void free_object(struct debug_obj *obj)
207 raw_spin_lock_irqsave(&pool_lock, flags);
209 * schedule work when the pool is filled and the cache is
212 if (obj_pool_free > debug_objects_pool_size && obj_cache)
214 hlist_add_head(&obj->node, &obj_pool);
217 raw_spin_unlock_irqrestore(&pool_lock, flags);
219 schedule_work(&debug_obj_work);
223 * We run out of memory. That means we probably have tons of objects
226 static void debug_objects_oom(void)
228 struct debug_bucket *db = obj_hash;
229 struct hlist_node *tmp;
230 HLIST_HEAD(freelist);
231 struct debug_obj *obj;
235 pr_warn("Out of memory. ODEBUG disabled\n");
237 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
238 raw_spin_lock_irqsave(&db->lock, flags);
239 hlist_move_list(&db->list, &freelist);
240 raw_spin_unlock_irqrestore(&db->lock, flags);
243 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
244 hlist_del(&obj->node);
251 * We use the pfn of the address for the hash. That way we can check
252 * for freed objects simply by checking the affected bucket.
254 static struct debug_bucket *get_bucket(unsigned long addr)
258 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
259 return &obj_hash[hash];
262 static void debug_print_object(struct debug_obj *obj, char *msg)
264 struct debug_obj_descr *descr = obj->descr;
267 if (limit < 5 && descr != descr_test) {
268 void *hint = descr->debug_hint ?
269 descr->debug_hint(obj->object) : NULL;
271 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
272 "object type: %s hint: %pS\n",
273 msg, obj_states[obj->state], obj->astate,
276 debug_objects_warnings++;
280 * Try to repair the damage, so we have a better chance to get useful
284 debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
285 void * addr, enum debug_obj_state state)
287 if (fixup && fixup(addr, state)) {
288 debug_objects_fixups++;
294 static void debug_object_is_on_stack(void *addr, int onstack)
302 is_on_stack = object_is_on_stack(addr);
303 if (is_on_stack == onstack)
308 pr_warn("object is on stack, but not annotated\n");
310 pr_warn("object is not on stack, but annotated\n");
315 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
317 enum debug_obj_state state;
318 struct debug_bucket *db;
319 struct debug_obj *obj;
324 db = get_bucket((unsigned long) addr);
326 raw_spin_lock_irqsave(&db->lock, flags);
328 obj = lookup_object(addr, db);
330 obj = alloc_object(addr, db, descr);
332 debug_objects_enabled = 0;
333 raw_spin_unlock_irqrestore(&db->lock, flags);
337 debug_object_is_on_stack(addr, onstack);
340 switch (obj->state) {
341 case ODEBUG_STATE_NONE:
342 case ODEBUG_STATE_INIT:
343 case ODEBUG_STATE_INACTIVE:
344 obj->state = ODEBUG_STATE_INIT;
347 case ODEBUG_STATE_ACTIVE:
348 debug_print_object(obj, "init");
350 raw_spin_unlock_irqrestore(&db->lock, flags);
351 debug_object_fixup(descr->fixup_init, addr, state);
354 case ODEBUG_STATE_DESTROYED:
355 debug_print_object(obj, "init");
361 raw_spin_unlock_irqrestore(&db->lock, flags);
365 * debug_object_init - debug checks when an object is initialized
366 * @addr: address of the object
367 * @descr: pointer to an object specific debug description structure
369 void debug_object_init(void *addr, struct debug_obj_descr *descr)
371 if (!debug_objects_enabled)
374 __debug_object_init(addr, descr, 0);
376 EXPORT_SYMBOL_GPL(debug_object_init);
379 * debug_object_init_on_stack - debug checks when an object on stack is
381 * @addr: address of the object
382 * @descr: pointer to an object specific debug description structure
384 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
386 if (!debug_objects_enabled)
389 __debug_object_init(addr, descr, 1);
391 EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
394 * debug_object_activate - debug checks when an object is activated
395 * @addr: address of the object
396 * @descr: pointer to an object specific debug description structure
397 * Returns 0 for success, -EINVAL for check failed.
399 int debug_object_activate(void *addr, struct debug_obj_descr *descr)
401 enum debug_obj_state state;
402 struct debug_bucket *db;
403 struct debug_obj *obj;
406 struct debug_obj o = { .object = addr,
407 .state = ODEBUG_STATE_NOTAVAILABLE,
410 if (!debug_objects_enabled)
413 db = get_bucket((unsigned long) addr);
415 raw_spin_lock_irqsave(&db->lock, flags);
417 obj = lookup_object(addr, db);
419 switch (obj->state) {
420 case ODEBUG_STATE_INIT:
421 case ODEBUG_STATE_INACTIVE:
422 obj->state = ODEBUG_STATE_ACTIVE;
426 case ODEBUG_STATE_ACTIVE:
427 debug_print_object(obj, "activate");
429 raw_spin_unlock_irqrestore(&db->lock, flags);
430 ret = debug_object_fixup(descr->fixup_activate, addr, state);
431 return ret ? 0 : -EINVAL;
433 case ODEBUG_STATE_DESTROYED:
434 debug_print_object(obj, "activate");
441 raw_spin_unlock_irqrestore(&db->lock, flags);
445 raw_spin_unlock_irqrestore(&db->lock, flags);
447 * We are here when a static object is activated. We
448 * let the type specific code confirm whether this is
449 * true or not. if true, we just make sure that the
450 * static object is tracked in the object tracker. If
451 * not, this must be a bug, so we try to fix it up.
453 if (descr->is_static_object && descr->is_static_object(addr)) {
454 /* track this static object */
455 debug_object_init(addr, descr);
456 debug_object_activate(addr, descr);
458 debug_print_object(&o, "activate");
459 ret = debug_object_fixup(descr->fixup_activate, addr,
460 ODEBUG_STATE_NOTAVAILABLE);
461 return ret ? 0 : -EINVAL;
465 EXPORT_SYMBOL_GPL(debug_object_activate);
468 * debug_object_deactivate - debug checks when an object is deactivated
469 * @addr: address of the object
470 * @descr: pointer to an object specific debug description structure
472 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
474 struct debug_bucket *db;
475 struct debug_obj *obj;
478 if (!debug_objects_enabled)
481 db = get_bucket((unsigned long) addr);
483 raw_spin_lock_irqsave(&db->lock, flags);
485 obj = lookup_object(addr, db);
487 switch (obj->state) {
488 case ODEBUG_STATE_INIT:
489 case ODEBUG_STATE_INACTIVE:
490 case ODEBUG_STATE_ACTIVE:
492 obj->state = ODEBUG_STATE_INACTIVE;
494 debug_print_object(obj, "deactivate");
497 case ODEBUG_STATE_DESTROYED:
498 debug_print_object(obj, "deactivate");
504 struct debug_obj o = { .object = addr,
505 .state = ODEBUG_STATE_NOTAVAILABLE,
508 debug_print_object(&o, "deactivate");
511 raw_spin_unlock_irqrestore(&db->lock, flags);
513 EXPORT_SYMBOL_GPL(debug_object_deactivate);
516 * debug_object_destroy - debug checks when an object is destroyed
517 * @addr: address of the object
518 * @descr: pointer to an object specific debug description structure
520 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
522 enum debug_obj_state state;
523 struct debug_bucket *db;
524 struct debug_obj *obj;
527 if (!debug_objects_enabled)
530 db = get_bucket((unsigned long) addr);
532 raw_spin_lock_irqsave(&db->lock, flags);
534 obj = lookup_object(addr, db);
538 switch (obj->state) {
539 case ODEBUG_STATE_NONE:
540 case ODEBUG_STATE_INIT:
541 case ODEBUG_STATE_INACTIVE:
542 obj->state = ODEBUG_STATE_DESTROYED;
544 case ODEBUG_STATE_ACTIVE:
545 debug_print_object(obj, "destroy");
547 raw_spin_unlock_irqrestore(&db->lock, flags);
548 debug_object_fixup(descr->fixup_destroy, addr, state);
551 case ODEBUG_STATE_DESTROYED:
552 debug_print_object(obj, "destroy");
558 raw_spin_unlock_irqrestore(&db->lock, flags);
560 EXPORT_SYMBOL_GPL(debug_object_destroy);
563 * debug_object_free - debug checks when an object is freed
564 * @addr: address of the object
565 * @descr: pointer to an object specific debug description structure
567 void debug_object_free(void *addr, struct debug_obj_descr *descr)
569 enum debug_obj_state state;
570 struct debug_bucket *db;
571 struct debug_obj *obj;
574 if (!debug_objects_enabled)
577 db = get_bucket((unsigned long) addr);
579 raw_spin_lock_irqsave(&db->lock, flags);
581 obj = lookup_object(addr, db);
585 switch (obj->state) {
586 case ODEBUG_STATE_ACTIVE:
587 debug_print_object(obj, "free");
589 raw_spin_unlock_irqrestore(&db->lock, flags);
590 debug_object_fixup(descr->fixup_free, addr, state);
593 hlist_del(&obj->node);
594 raw_spin_unlock_irqrestore(&db->lock, flags);
599 raw_spin_unlock_irqrestore(&db->lock, flags);
601 EXPORT_SYMBOL_GPL(debug_object_free);
604 * debug_object_assert_init - debug checks when object should be init-ed
605 * @addr: address of the object
606 * @descr: pointer to an object specific debug description structure
608 void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
610 struct debug_bucket *db;
611 struct debug_obj *obj;
614 if (!debug_objects_enabled)
617 db = get_bucket((unsigned long) addr);
619 raw_spin_lock_irqsave(&db->lock, flags);
621 obj = lookup_object(addr, db);
623 struct debug_obj o = { .object = addr,
624 .state = ODEBUG_STATE_NOTAVAILABLE,
627 raw_spin_unlock_irqrestore(&db->lock, flags);
629 * Maybe the object is static, and we let the type specific
630 * code confirm. Track this static object if true, else invoke
633 if (descr->is_static_object && descr->is_static_object(addr)) {
634 /* Track this static object */
635 debug_object_init(addr, descr);
637 debug_print_object(&o, "assert_init");
638 debug_object_fixup(descr->fixup_assert_init, addr,
639 ODEBUG_STATE_NOTAVAILABLE);
644 raw_spin_unlock_irqrestore(&db->lock, flags);
646 EXPORT_SYMBOL_GPL(debug_object_assert_init);
649 * debug_object_active_state - debug checks object usage state machine
650 * @addr: address of the object
651 * @descr: pointer to an object specific debug description structure
652 * @expect: expected state
653 * @next: state to move to if expected state is found
656 debug_object_active_state(void *addr, struct debug_obj_descr *descr,
657 unsigned int expect, unsigned int next)
659 struct debug_bucket *db;
660 struct debug_obj *obj;
663 if (!debug_objects_enabled)
666 db = get_bucket((unsigned long) addr);
668 raw_spin_lock_irqsave(&db->lock, flags);
670 obj = lookup_object(addr, db);
672 switch (obj->state) {
673 case ODEBUG_STATE_ACTIVE:
674 if (obj->astate == expect)
677 debug_print_object(obj, "active_state");
681 debug_print_object(obj, "active_state");
685 struct debug_obj o = { .object = addr,
686 .state = ODEBUG_STATE_NOTAVAILABLE,
689 debug_print_object(&o, "active_state");
692 raw_spin_unlock_irqrestore(&db->lock, flags);
694 EXPORT_SYMBOL_GPL(debug_object_active_state);
696 #ifdef CONFIG_DEBUG_OBJECTS_FREE
697 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
699 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
700 struct hlist_node *tmp;
701 HLIST_HEAD(freelist);
702 struct debug_obj_descr *descr;
703 enum debug_obj_state state;
704 struct debug_bucket *db;
705 struct debug_obj *obj;
708 saddr = (unsigned long) address;
709 eaddr = saddr + size;
710 paddr = saddr & ODEBUG_CHUNK_MASK;
711 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
712 chunks >>= ODEBUG_CHUNK_SHIFT;
714 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
715 db = get_bucket(paddr);
719 raw_spin_lock_irqsave(&db->lock, flags);
720 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
722 oaddr = (unsigned long) obj->object;
723 if (oaddr < saddr || oaddr >= eaddr)
726 switch (obj->state) {
727 case ODEBUG_STATE_ACTIVE:
728 debug_print_object(obj, "free");
731 raw_spin_unlock_irqrestore(&db->lock, flags);
732 debug_object_fixup(descr->fixup_free,
733 (void *) oaddr, state);
736 hlist_del(&obj->node);
737 hlist_add_head(&obj->node, &freelist);
741 raw_spin_unlock_irqrestore(&db->lock, flags);
744 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
745 hlist_del(&obj->node);
749 if (cnt > debug_objects_maxchain)
750 debug_objects_maxchain = cnt;
754 void debug_check_no_obj_freed(const void *address, unsigned long size)
756 if (debug_objects_enabled)
757 __debug_check_no_obj_freed(address, size);
761 #ifdef CONFIG_DEBUG_FS
763 static int debug_stats_show(struct seq_file *m, void *v)
765 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
766 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
767 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
768 seq_printf(m, "pool_free :%d\n", obj_pool_free);
769 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
770 seq_printf(m, "pool_used :%d\n", obj_pool_used);
771 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
772 seq_printf(m, "objects_alloc :%d\n", debug_objects_alloc);
773 seq_printf(m, "objects_freed :%d\n", debug_objects_freed);
777 static int debug_stats_open(struct inode *inode, struct file *filp)
779 return single_open(filp, debug_stats_show, NULL);
782 static const struct file_operations debug_stats_fops = {
783 .open = debug_stats_open,
786 .release = single_release,
789 static int __init debug_objects_init_debugfs(void)
791 struct dentry *dbgdir, *dbgstats;
793 if (!debug_objects_enabled)
796 dbgdir = debugfs_create_dir("debug_objects", NULL);
800 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
808 debugfs_remove(dbgdir);
812 __initcall(debug_objects_init_debugfs);
815 static inline void debug_objects_init_debugfs(void) { }
818 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
820 /* Random data structure for the self test */
822 unsigned long dummy1[6];
824 unsigned long dummy2[3];
827 static __initdata struct debug_obj_descr descr_type_test;
829 static bool __init is_static_object(void *addr)
831 struct self_test *obj = addr;
833 return obj->static_init;
837 * fixup_init is called when:
838 * - an active object is initialized
840 static bool __init fixup_init(void *addr, enum debug_obj_state state)
842 struct self_test *obj = addr;
845 case ODEBUG_STATE_ACTIVE:
846 debug_object_deactivate(obj, &descr_type_test);
847 debug_object_init(obj, &descr_type_test);
855 * fixup_activate is called when:
856 * - an active object is activated
857 * - an unknown non-static object is activated
859 static bool __init fixup_activate(void *addr, enum debug_obj_state state)
861 struct self_test *obj = addr;
864 case ODEBUG_STATE_NOTAVAILABLE:
866 case ODEBUG_STATE_ACTIVE:
867 debug_object_deactivate(obj, &descr_type_test);
868 debug_object_activate(obj, &descr_type_test);
877 * fixup_destroy is called when:
878 * - an active object is destroyed
880 static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
882 struct self_test *obj = addr;
885 case ODEBUG_STATE_ACTIVE:
886 debug_object_deactivate(obj, &descr_type_test);
887 debug_object_destroy(obj, &descr_type_test);
895 * fixup_free is called when:
896 * - an active object is freed
898 static bool __init fixup_free(void *addr, enum debug_obj_state state)
900 struct self_test *obj = addr;
903 case ODEBUG_STATE_ACTIVE:
904 debug_object_deactivate(obj, &descr_type_test);
905 debug_object_free(obj, &descr_type_test);
913 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
915 struct debug_bucket *db;
916 struct debug_obj *obj;
920 db = get_bucket((unsigned long) addr);
922 raw_spin_lock_irqsave(&db->lock, flags);
924 obj = lookup_object(addr, db);
925 if (!obj && state != ODEBUG_STATE_NONE) {
926 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
929 if (obj && obj->state != state) {
930 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
934 if (fixups != debug_objects_fixups) {
935 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
936 fixups, debug_objects_fixups);
939 if (warnings != debug_objects_warnings) {
940 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
941 warnings, debug_objects_warnings);
946 raw_spin_unlock_irqrestore(&db->lock, flags);
948 debug_objects_enabled = 0;
952 static __initdata struct debug_obj_descr descr_type_test = {
954 .is_static_object = is_static_object,
955 .fixup_init = fixup_init,
956 .fixup_activate = fixup_activate,
957 .fixup_destroy = fixup_destroy,
958 .fixup_free = fixup_free,
961 static __initdata struct self_test obj = { .static_init = 0 };
963 static void __init debug_objects_selftest(void)
965 int fixups, oldfixups, warnings, oldwarnings;
968 local_irq_save(flags);
970 fixups = oldfixups = debug_objects_fixups;
971 warnings = oldwarnings = debug_objects_warnings;
972 descr_test = &descr_type_test;
974 debug_object_init(&obj, &descr_type_test);
975 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
977 debug_object_activate(&obj, &descr_type_test);
978 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
980 debug_object_activate(&obj, &descr_type_test);
981 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
983 debug_object_deactivate(&obj, &descr_type_test);
984 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
986 debug_object_destroy(&obj, &descr_type_test);
987 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
989 debug_object_init(&obj, &descr_type_test);
990 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
992 debug_object_activate(&obj, &descr_type_test);
993 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
995 debug_object_deactivate(&obj, &descr_type_test);
996 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
998 debug_object_free(&obj, &descr_type_test);
999 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1002 obj.static_init = 1;
1003 debug_object_activate(&obj, &descr_type_test);
1004 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1006 debug_object_init(&obj, &descr_type_test);
1007 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1009 debug_object_free(&obj, &descr_type_test);
1010 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1013 #ifdef CONFIG_DEBUG_OBJECTS_FREE
1014 debug_object_init(&obj, &descr_type_test);
1015 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1017 debug_object_activate(&obj, &descr_type_test);
1018 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1020 __debug_check_no_obj_freed(&obj, sizeof(obj));
1021 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1024 pr_info("selftest passed\n");
1027 debug_objects_fixups = oldfixups;
1028 debug_objects_warnings = oldwarnings;
1031 local_irq_restore(flags);
1034 static inline void debug_objects_selftest(void) { }
1038 * Called during early boot to initialize the hash buckets and link
1039 * the static object pool objects into the poll list. After this call
1040 * the object tracker is fully operational.
1042 void __init debug_objects_early_init(void)
1046 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1047 raw_spin_lock_init(&obj_hash[i].lock);
1049 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1050 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1054 * Convert the statically allocated objects to dynamic ones:
1056 static int __init debug_objects_replace_static_objects(void)
1058 struct debug_bucket *db = obj_hash;
1059 struct hlist_node *tmp;
1060 struct debug_obj *obj, *new;
1061 HLIST_HEAD(objects);
1064 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1065 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1068 hlist_add_head(&obj->node, &objects);
1072 * When debug_objects_mem_init() is called we know that only
1073 * one CPU is up, so disabling interrupts is enough
1074 * protection. This avoids the lockdep hell of lock ordering.
1076 local_irq_disable();
1078 /* Remove the statically allocated objects from the pool */
1079 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1080 hlist_del(&obj->node);
1081 /* Move the allocated objects to the pool */
1082 hlist_move_list(&objects, &obj_pool);
1084 /* Replace the active object references */
1085 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1086 hlist_move_list(&db->list, &objects);
1088 hlist_for_each_entry(obj, &objects, node) {
1089 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1090 hlist_del(&new->node);
1091 /* copy object data */
1093 hlist_add_head(&new->node, &db->list);
1099 pr_debug("%d of %d active objects replaced\n",
1100 cnt, obj_pool_used);
1103 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1104 hlist_del(&obj->node);
1105 kmem_cache_free(obj_cache, obj);
1111 * Called after the kmem_caches are functional to setup a dedicated
1112 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1113 * prevents that the debug code is called on kmem_cache_free() for the
1114 * debug tracker objects to avoid recursive calls.
1116 void __init debug_objects_mem_init(void)
1118 if (!debug_objects_enabled)
1121 obj_cache = kmem_cache_create("debug_objects_cache",
1122 sizeof (struct debug_obj), 0,
1123 SLAB_DEBUG_OBJECTS, NULL);
1125 if (!obj_cache || debug_objects_replace_static_objects()) {
1126 debug_objects_enabled = 0;
1128 kmem_cache_destroy(obj_cache);
1129 pr_warn("out of memory.\n");
1131 debug_objects_selftest();
1134 * Increase the thresholds for allocating and freeing objects
1135 * according to the number of possible CPUs available in the system.
1137 debug_objects_pool_size += num_possible_cpus() * 32;
1138 debug_objects_pool_min_level += num_possible_cpus() * 4;