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/sched/task_stack.h>
17 #include <linux/seq_file.h>
18 #include <linux/debugfs.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/kmemleak.h>
23 #define ODEBUG_HASH_BITS 14
24 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
26 #define ODEBUG_POOL_SIZE 1024
27 #define ODEBUG_POOL_MIN_LEVEL 256
29 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
30 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
31 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
34 struct hlist_head list;
38 static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
40 static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
42 static DEFINE_RAW_SPINLOCK(pool_lock);
44 static HLIST_HEAD(obj_pool);
46 static int obj_pool_min_free = ODEBUG_POOL_SIZE;
47 static int obj_pool_free = ODEBUG_POOL_SIZE;
48 static int obj_pool_used;
49 static int obj_pool_max_used;
50 static struct kmem_cache *obj_cache;
52 static int debug_objects_maxchain __read_mostly;
53 static int debug_objects_fixups __read_mostly;
54 static int debug_objects_warnings __read_mostly;
55 static int debug_objects_enabled __read_mostly
56 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
57 static int debug_objects_pool_size __read_mostly
59 static int debug_objects_pool_min_level __read_mostly
60 = ODEBUG_POOL_MIN_LEVEL;
61 static struct debug_obj_descr *descr_test __read_mostly;
64 * Track numbers of kmem_cache_alloc()/free() calls done.
66 static int debug_objects_allocated;
67 static int debug_objects_freed;
69 static void free_obj_work(struct work_struct *work);
70 static DECLARE_WORK(debug_obj_work, free_obj_work);
72 static int __init enable_object_debug(char *str)
74 debug_objects_enabled = 1;
78 static int __init disable_object_debug(char *str)
80 debug_objects_enabled = 0;
84 early_param("debug_objects", enable_object_debug);
85 early_param("no_debug_objects", disable_object_debug);
87 static const char *obj_states[ODEBUG_STATE_MAX] = {
88 [ODEBUG_STATE_NONE] = "none",
89 [ODEBUG_STATE_INIT] = "initialized",
90 [ODEBUG_STATE_INACTIVE] = "inactive",
91 [ODEBUG_STATE_ACTIVE] = "active",
92 [ODEBUG_STATE_DESTROYED] = "destroyed",
93 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
96 static void fill_pool(void)
98 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
99 struct debug_obj *new;
102 if (likely(obj_pool_free >= debug_objects_pool_min_level))
105 if (unlikely(!obj_cache))
108 while (obj_pool_free < debug_objects_pool_min_level) {
110 new = kmem_cache_zalloc(obj_cache, gfp);
114 raw_spin_lock_irqsave(&pool_lock, flags);
115 hlist_add_head(&new->node, &obj_pool);
116 debug_objects_allocated++;
118 raw_spin_unlock_irqrestore(&pool_lock, flags);
123 * Lookup an object in the hash bucket.
125 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
127 struct debug_obj *obj;
130 hlist_for_each_entry(obj, &b->list, node) {
132 if (obj->object == addr)
135 if (cnt > debug_objects_maxchain)
136 debug_objects_maxchain = cnt;
142 * Allocate a new object. If the pool is empty, switch off the debugger.
143 * Must be called with interrupts disabled.
145 static struct debug_obj *
146 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
148 struct debug_obj *obj = NULL;
150 raw_spin_lock(&pool_lock);
151 if (obj_pool.first) {
152 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
156 obj->state = ODEBUG_STATE_NONE;
158 hlist_del(&obj->node);
160 hlist_add_head(&obj->node, &b->list);
163 if (obj_pool_used > obj_pool_max_used)
164 obj_pool_max_used = obj_pool_used;
167 if (obj_pool_free < obj_pool_min_free)
168 obj_pool_min_free = obj_pool_free;
170 raw_spin_unlock(&pool_lock);
176 * workqueue function to free objects.
178 * To reduce contention on the global pool_lock, the actual freeing of
179 * debug objects will be delayed if the pool_lock is busy. We also free
180 * the objects in a batch of 4 for each lock/unlock cycle.
182 #define ODEBUG_FREE_BATCH 4
184 static void free_obj_work(struct work_struct *work)
186 struct debug_obj *objs[ODEBUG_FREE_BATCH];
190 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
192 while (obj_pool_free >= debug_objects_pool_size + ODEBUG_FREE_BATCH) {
193 for (i = 0; i < ODEBUG_FREE_BATCH; i++) {
194 objs[i] = hlist_entry(obj_pool.first,
195 typeof(*objs[0]), node);
196 hlist_del(&objs[i]->node);
199 obj_pool_free -= ODEBUG_FREE_BATCH;
200 debug_objects_freed += ODEBUG_FREE_BATCH;
202 * We release pool_lock across kmem_cache_free() to
203 * avoid contention on pool_lock.
205 raw_spin_unlock_irqrestore(&pool_lock, flags);
206 for (i = 0; i < ODEBUG_FREE_BATCH; i++)
207 kmem_cache_free(obj_cache, objs[i]);
208 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
211 raw_spin_unlock_irqrestore(&pool_lock, flags);
215 * Put the object back into the pool and schedule work to free objects
218 static void free_object(struct debug_obj *obj)
223 raw_spin_lock_irqsave(&pool_lock, flags);
225 * schedule work when the pool is filled and the cache is
228 if (obj_pool_free > debug_objects_pool_size && obj_cache)
230 hlist_add_head(&obj->node, &obj_pool);
233 raw_spin_unlock_irqrestore(&pool_lock, flags);
235 schedule_work(&debug_obj_work);
239 * We run out of memory. That means we probably have tons of objects
242 static void debug_objects_oom(void)
244 struct debug_bucket *db = obj_hash;
245 struct hlist_node *tmp;
246 HLIST_HEAD(freelist);
247 struct debug_obj *obj;
251 pr_warn("Out of memory. ODEBUG disabled\n");
253 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
254 raw_spin_lock_irqsave(&db->lock, flags);
255 hlist_move_list(&db->list, &freelist);
256 raw_spin_unlock_irqrestore(&db->lock, flags);
259 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
260 hlist_del(&obj->node);
267 * We use the pfn of the address for the hash. That way we can check
268 * for freed objects simply by checking the affected bucket.
270 static struct debug_bucket *get_bucket(unsigned long addr)
274 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
275 return &obj_hash[hash];
278 static void debug_print_object(struct debug_obj *obj, char *msg)
280 struct debug_obj_descr *descr = obj->descr;
283 if (limit < 5 && descr != descr_test) {
284 void *hint = descr->debug_hint ?
285 descr->debug_hint(obj->object) : NULL;
287 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
288 "object type: %s hint: %pS\n",
289 msg, obj_states[obj->state], obj->astate,
292 debug_objects_warnings++;
296 * Try to repair the damage, so we have a better chance to get useful
300 debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
301 void * addr, enum debug_obj_state state)
303 if (fixup && fixup(addr, state)) {
304 debug_objects_fixups++;
310 static void debug_object_is_on_stack(void *addr, int onstack)
318 is_on_stack = object_is_on_stack(addr);
319 if (is_on_stack == onstack)
324 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
325 task_stack_page(current));
327 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
328 task_stack_page(current));
334 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
336 enum debug_obj_state state;
337 struct debug_bucket *db;
338 struct debug_obj *obj;
343 db = get_bucket((unsigned long) addr);
345 raw_spin_lock_irqsave(&db->lock, flags);
347 obj = lookup_object(addr, db);
349 obj = alloc_object(addr, db, descr);
351 debug_objects_enabled = 0;
352 raw_spin_unlock_irqrestore(&db->lock, flags);
356 debug_object_is_on_stack(addr, onstack);
359 switch (obj->state) {
360 case ODEBUG_STATE_NONE:
361 case ODEBUG_STATE_INIT:
362 case ODEBUG_STATE_INACTIVE:
363 obj->state = ODEBUG_STATE_INIT;
366 case ODEBUG_STATE_ACTIVE:
367 debug_print_object(obj, "init");
369 raw_spin_unlock_irqrestore(&db->lock, flags);
370 debug_object_fixup(descr->fixup_init, addr, state);
373 case ODEBUG_STATE_DESTROYED:
374 debug_print_object(obj, "init");
380 raw_spin_unlock_irqrestore(&db->lock, flags);
384 * debug_object_init - debug checks when an object is initialized
385 * @addr: address of the object
386 * @descr: pointer to an object specific debug description structure
388 void debug_object_init(void *addr, struct debug_obj_descr *descr)
390 if (!debug_objects_enabled)
393 __debug_object_init(addr, descr, 0);
395 EXPORT_SYMBOL_GPL(debug_object_init);
398 * debug_object_init_on_stack - debug checks when an object on stack is
400 * @addr: address of the object
401 * @descr: pointer to an object specific debug description structure
403 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
405 if (!debug_objects_enabled)
408 __debug_object_init(addr, descr, 1);
410 EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
413 * debug_object_activate - debug checks when an object is activated
414 * @addr: address of the object
415 * @descr: pointer to an object specific debug description structure
416 * Returns 0 for success, -EINVAL for check failed.
418 int debug_object_activate(void *addr, struct debug_obj_descr *descr)
420 enum debug_obj_state state;
421 struct debug_bucket *db;
422 struct debug_obj *obj;
425 struct debug_obj o = { .object = addr,
426 .state = ODEBUG_STATE_NOTAVAILABLE,
429 if (!debug_objects_enabled)
432 db = get_bucket((unsigned long) addr);
434 raw_spin_lock_irqsave(&db->lock, flags);
436 obj = lookup_object(addr, db);
438 switch (obj->state) {
439 case ODEBUG_STATE_INIT:
440 case ODEBUG_STATE_INACTIVE:
441 obj->state = ODEBUG_STATE_ACTIVE;
445 case ODEBUG_STATE_ACTIVE:
446 debug_print_object(obj, "activate");
448 raw_spin_unlock_irqrestore(&db->lock, flags);
449 ret = debug_object_fixup(descr->fixup_activate, addr, state);
450 return ret ? 0 : -EINVAL;
452 case ODEBUG_STATE_DESTROYED:
453 debug_print_object(obj, "activate");
460 raw_spin_unlock_irqrestore(&db->lock, flags);
464 raw_spin_unlock_irqrestore(&db->lock, flags);
466 * We are here when a static object is activated. We
467 * let the type specific code confirm whether this is
468 * true or not. if true, we just make sure that the
469 * static object is tracked in the object tracker. If
470 * not, this must be a bug, so we try to fix it up.
472 if (descr->is_static_object && descr->is_static_object(addr)) {
473 /* track this static object */
474 debug_object_init(addr, descr);
475 debug_object_activate(addr, descr);
477 debug_print_object(&o, "activate");
478 ret = debug_object_fixup(descr->fixup_activate, addr,
479 ODEBUG_STATE_NOTAVAILABLE);
480 return ret ? 0 : -EINVAL;
484 EXPORT_SYMBOL_GPL(debug_object_activate);
487 * debug_object_deactivate - debug checks when an object is deactivated
488 * @addr: address of the object
489 * @descr: pointer to an object specific debug description structure
491 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
493 struct debug_bucket *db;
494 struct debug_obj *obj;
497 if (!debug_objects_enabled)
500 db = get_bucket((unsigned long) addr);
502 raw_spin_lock_irqsave(&db->lock, flags);
504 obj = lookup_object(addr, db);
506 switch (obj->state) {
507 case ODEBUG_STATE_INIT:
508 case ODEBUG_STATE_INACTIVE:
509 case ODEBUG_STATE_ACTIVE:
511 obj->state = ODEBUG_STATE_INACTIVE;
513 debug_print_object(obj, "deactivate");
516 case ODEBUG_STATE_DESTROYED:
517 debug_print_object(obj, "deactivate");
523 struct debug_obj o = { .object = addr,
524 .state = ODEBUG_STATE_NOTAVAILABLE,
527 debug_print_object(&o, "deactivate");
530 raw_spin_unlock_irqrestore(&db->lock, flags);
532 EXPORT_SYMBOL_GPL(debug_object_deactivate);
535 * debug_object_destroy - debug checks when an object is destroyed
536 * @addr: address of the object
537 * @descr: pointer to an object specific debug description structure
539 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
541 enum debug_obj_state state;
542 struct debug_bucket *db;
543 struct debug_obj *obj;
546 if (!debug_objects_enabled)
549 db = get_bucket((unsigned long) addr);
551 raw_spin_lock_irqsave(&db->lock, flags);
553 obj = lookup_object(addr, db);
557 switch (obj->state) {
558 case ODEBUG_STATE_NONE:
559 case ODEBUG_STATE_INIT:
560 case ODEBUG_STATE_INACTIVE:
561 obj->state = ODEBUG_STATE_DESTROYED;
563 case ODEBUG_STATE_ACTIVE:
564 debug_print_object(obj, "destroy");
566 raw_spin_unlock_irqrestore(&db->lock, flags);
567 debug_object_fixup(descr->fixup_destroy, addr, state);
570 case ODEBUG_STATE_DESTROYED:
571 debug_print_object(obj, "destroy");
577 raw_spin_unlock_irqrestore(&db->lock, flags);
579 EXPORT_SYMBOL_GPL(debug_object_destroy);
582 * debug_object_free - debug checks when an object is freed
583 * @addr: address of the object
584 * @descr: pointer to an object specific debug description structure
586 void debug_object_free(void *addr, struct debug_obj_descr *descr)
588 enum debug_obj_state state;
589 struct debug_bucket *db;
590 struct debug_obj *obj;
593 if (!debug_objects_enabled)
596 db = get_bucket((unsigned long) addr);
598 raw_spin_lock_irqsave(&db->lock, flags);
600 obj = lookup_object(addr, db);
604 switch (obj->state) {
605 case ODEBUG_STATE_ACTIVE:
606 debug_print_object(obj, "free");
608 raw_spin_unlock_irqrestore(&db->lock, flags);
609 debug_object_fixup(descr->fixup_free, addr, state);
612 hlist_del(&obj->node);
613 raw_spin_unlock_irqrestore(&db->lock, flags);
618 raw_spin_unlock_irqrestore(&db->lock, flags);
620 EXPORT_SYMBOL_GPL(debug_object_free);
623 * debug_object_assert_init - debug checks when object should be init-ed
624 * @addr: address of the object
625 * @descr: pointer to an object specific debug description structure
627 void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
629 struct debug_bucket *db;
630 struct debug_obj *obj;
633 if (!debug_objects_enabled)
636 db = get_bucket((unsigned long) addr);
638 raw_spin_lock_irqsave(&db->lock, flags);
640 obj = lookup_object(addr, db);
642 struct debug_obj o = { .object = addr,
643 .state = ODEBUG_STATE_NOTAVAILABLE,
646 raw_spin_unlock_irqrestore(&db->lock, flags);
648 * Maybe the object is static, and we let the type specific
649 * code confirm. Track this static object if true, else invoke
652 if (descr->is_static_object && descr->is_static_object(addr)) {
653 /* Track this static object */
654 debug_object_init(addr, descr);
656 debug_print_object(&o, "assert_init");
657 debug_object_fixup(descr->fixup_assert_init, addr,
658 ODEBUG_STATE_NOTAVAILABLE);
663 raw_spin_unlock_irqrestore(&db->lock, flags);
665 EXPORT_SYMBOL_GPL(debug_object_assert_init);
668 * debug_object_active_state - debug checks object usage state machine
669 * @addr: address of the object
670 * @descr: pointer to an object specific debug description structure
671 * @expect: expected state
672 * @next: state to move to if expected state is found
675 debug_object_active_state(void *addr, struct debug_obj_descr *descr,
676 unsigned int expect, unsigned int next)
678 struct debug_bucket *db;
679 struct debug_obj *obj;
682 if (!debug_objects_enabled)
685 db = get_bucket((unsigned long) addr);
687 raw_spin_lock_irqsave(&db->lock, flags);
689 obj = lookup_object(addr, db);
691 switch (obj->state) {
692 case ODEBUG_STATE_ACTIVE:
693 if (obj->astate == expect)
696 debug_print_object(obj, "active_state");
700 debug_print_object(obj, "active_state");
704 struct debug_obj o = { .object = addr,
705 .state = ODEBUG_STATE_NOTAVAILABLE,
708 debug_print_object(&o, "active_state");
711 raw_spin_unlock_irqrestore(&db->lock, flags);
713 EXPORT_SYMBOL_GPL(debug_object_active_state);
715 #ifdef CONFIG_DEBUG_OBJECTS_FREE
716 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
718 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
719 struct hlist_node *tmp;
720 HLIST_HEAD(freelist);
721 struct debug_obj_descr *descr;
722 enum debug_obj_state state;
723 struct debug_bucket *db;
724 struct debug_obj *obj;
727 saddr = (unsigned long) address;
728 eaddr = saddr + size;
729 paddr = saddr & ODEBUG_CHUNK_MASK;
730 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
731 chunks >>= ODEBUG_CHUNK_SHIFT;
733 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
734 db = get_bucket(paddr);
738 raw_spin_lock_irqsave(&db->lock, flags);
739 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
741 oaddr = (unsigned long) obj->object;
742 if (oaddr < saddr || oaddr >= eaddr)
745 switch (obj->state) {
746 case ODEBUG_STATE_ACTIVE:
747 debug_print_object(obj, "free");
750 raw_spin_unlock_irqrestore(&db->lock, flags);
751 debug_object_fixup(descr->fixup_free,
752 (void *) oaddr, state);
755 hlist_del(&obj->node);
756 hlist_add_head(&obj->node, &freelist);
760 raw_spin_unlock_irqrestore(&db->lock, flags);
763 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
764 hlist_del(&obj->node);
768 if (cnt > debug_objects_maxchain)
769 debug_objects_maxchain = cnt;
773 void debug_check_no_obj_freed(const void *address, unsigned long size)
775 if (debug_objects_enabled)
776 __debug_check_no_obj_freed(address, size);
780 #ifdef CONFIG_DEBUG_FS
782 static int debug_stats_show(struct seq_file *m, void *v)
784 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
785 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
786 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
787 seq_printf(m, "pool_free :%d\n", obj_pool_free);
788 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
789 seq_printf(m, "pool_used :%d\n", obj_pool_used);
790 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
791 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
792 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
796 static int debug_stats_open(struct inode *inode, struct file *filp)
798 return single_open(filp, debug_stats_show, NULL);
801 static const struct file_operations debug_stats_fops = {
802 .open = debug_stats_open,
805 .release = single_release,
808 static int __init debug_objects_init_debugfs(void)
810 struct dentry *dbgdir, *dbgstats;
812 if (!debug_objects_enabled)
815 dbgdir = debugfs_create_dir("debug_objects", NULL);
819 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
827 debugfs_remove(dbgdir);
831 __initcall(debug_objects_init_debugfs);
834 static inline void debug_objects_init_debugfs(void) { }
837 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
839 /* Random data structure for the self test */
841 unsigned long dummy1[6];
843 unsigned long dummy2[3];
846 static __initdata struct debug_obj_descr descr_type_test;
848 static bool __init is_static_object(void *addr)
850 struct self_test *obj = addr;
852 return obj->static_init;
856 * fixup_init is called when:
857 * - an active object is initialized
859 static bool __init fixup_init(void *addr, enum debug_obj_state state)
861 struct self_test *obj = addr;
864 case ODEBUG_STATE_ACTIVE:
865 debug_object_deactivate(obj, &descr_type_test);
866 debug_object_init(obj, &descr_type_test);
874 * fixup_activate is called when:
875 * - an active object is activated
876 * - an unknown non-static object is activated
878 static bool __init fixup_activate(void *addr, enum debug_obj_state state)
880 struct self_test *obj = addr;
883 case ODEBUG_STATE_NOTAVAILABLE:
885 case ODEBUG_STATE_ACTIVE:
886 debug_object_deactivate(obj, &descr_type_test);
887 debug_object_activate(obj, &descr_type_test);
896 * fixup_destroy is called when:
897 * - an active object is destroyed
899 static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
901 struct self_test *obj = addr;
904 case ODEBUG_STATE_ACTIVE:
905 debug_object_deactivate(obj, &descr_type_test);
906 debug_object_destroy(obj, &descr_type_test);
914 * fixup_free is called when:
915 * - an active object is freed
917 static bool __init fixup_free(void *addr, enum debug_obj_state state)
919 struct self_test *obj = addr;
922 case ODEBUG_STATE_ACTIVE:
923 debug_object_deactivate(obj, &descr_type_test);
924 debug_object_free(obj, &descr_type_test);
932 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
934 struct debug_bucket *db;
935 struct debug_obj *obj;
939 db = get_bucket((unsigned long) addr);
941 raw_spin_lock_irqsave(&db->lock, flags);
943 obj = lookup_object(addr, db);
944 if (!obj && state != ODEBUG_STATE_NONE) {
945 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
948 if (obj && obj->state != state) {
949 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
953 if (fixups != debug_objects_fixups) {
954 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
955 fixups, debug_objects_fixups);
958 if (warnings != debug_objects_warnings) {
959 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
960 warnings, debug_objects_warnings);
965 raw_spin_unlock_irqrestore(&db->lock, flags);
967 debug_objects_enabled = 0;
971 static __initdata struct debug_obj_descr descr_type_test = {
973 .is_static_object = is_static_object,
974 .fixup_init = fixup_init,
975 .fixup_activate = fixup_activate,
976 .fixup_destroy = fixup_destroy,
977 .fixup_free = fixup_free,
980 static __initdata struct self_test obj = { .static_init = 0 };
982 static void __init debug_objects_selftest(void)
984 int fixups, oldfixups, warnings, oldwarnings;
987 local_irq_save(flags);
989 fixups = oldfixups = debug_objects_fixups;
990 warnings = oldwarnings = debug_objects_warnings;
991 descr_test = &descr_type_test;
993 debug_object_init(&obj, &descr_type_test);
994 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
996 debug_object_activate(&obj, &descr_type_test);
997 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
999 debug_object_activate(&obj, &descr_type_test);
1000 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1002 debug_object_deactivate(&obj, &descr_type_test);
1003 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1005 debug_object_destroy(&obj, &descr_type_test);
1006 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1008 debug_object_init(&obj, &descr_type_test);
1009 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1011 debug_object_activate(&obj, &descr_type_test);
1012 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1014 debug_object_deactivate(&obj, &descr_type_test);
1015 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1017 debug_object_free(&obj, &descr_type_test);
1018 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1021 obj.static_init = 1;
1022 debug_object_activate(&obj, &descr_type_test);
1023 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1025 debug_object_init(&obj, &descr_type_test);
1026 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1028 debug_object_free(&obj, &descr_type_test);
1029 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1032 #ifdef CONFIG_DEBUG_OBJECTS_FREE
1033 debug_object_init(&obj, &descr_type_test);
1034 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1036 debug_object_activate(&obj, &descr_type_test);
1037 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1039 __debug_check_no_obj_freed(&obj, sizeof(obj));
1040 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1043 pr_info("selftest passed\n");
1046 debug_objects_fixups = oldfixups;
1047 debug_objects_warnings = oldwarnings;
1050 local_irq_restore(flags);
1053 static inline void debug_objects_selftest(void) { }
1057 * Called during early boot to initialize the hash buckets and link
1058 * the static object pool objects into the poll list. After this call
1059 * the object tracker is fully operational.
1061 void __init debug_objects_early_init(void)
1065 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1066 raw_spin_lock_init(&obj_hash[i].lock);
1068 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1069 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1073 * Convert the statically allocated objects to dynamic ones:
1075 static int __init debug_objects_replace_static_objects(void)
1077 struct debug_bucket *db = obj_hash;
1078 struct hlist_node *tmp;
1079 struct debug_obj *obj, *new;
1080 HLIST_HEAD(objects);
1083 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1084 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1087 hlist_add_head(&obj->node, &objects);
1091 * When debug_objects_mem_init() is called we know that only
1092 * one CPU is up, so disabling interrupts is enough
1093 * protection. This avoids the lockdep hell of lock ordering.
1095 local_irq_disable();
1097 /* Remove the statically allocated objects from the pool */
1098 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1099 hlist_del(&obj->node);
1100 /* Move the allocated objects to the pool */
1101 hlist_move_list(&objects, &obj_pool);
1103 /* Replace the active object references */
1104 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1105 hlist_move_list(&db->list, &objects);
1107 hlist_for_each_entry(obj, &objects, node) {
1108 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1109 hlist_del(&new->node);
1110 /* copy object data */
1112 hlist_add_head(&new->node, &db->list);
1118 pr_debug("%d of %d active objects replaced\n",
1119 cnt, obj_pool_used);
1122 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1123 hlist_del(&obj->node);
1124 kmem_cache_free(obj_cache, obj);
1130 * Called after the kmem_caches are functional to setup a dedicated
1131 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1132 * prevents that the debug code is called on kmem_cache_free() for the
1133 * debug tracker objects to avoid recursive calls.
1135 void __init debug_objects_mem_init(void)
1137 if (!debug_objects_enabled)
1140 obj_cache = kmem_cache_create("debug_objects_cache",
1141 sizeof (struct debug_obj), 0,
1142 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
1145 if (!obj_cache || debug_objects_replace_static_objects()) {
1146 debug_objects_enabled = 0;
1148 kmem_cache_destroy(obj_cache);
1149 pr_warn("out of memory.\n");
1151 debug_objects_selftest();
1154 * Increase the thresholds for allocating and freeing objects
1155 * according to the number of possible CPUs available in the system.
1157 debug_objects_pool_size += num_possible_cpus() * 32;
1158 debug_objects_pool_min_level += num_possible_cpus() * 4;