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 kmemleak_ignore(new);
115 raw_spin_lock_irqsave(&pool_lock, flags);
116 hlist_add_head(&new->node, &obj_pool);
117 debug_objects_allocated++;
119 raw_spin_unlock_irqrestore(&pool_lock, flags);
124 * Lookup an object in the hash bucket.
126 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
128 struct debug_obj *obj;
131 hlist_for_each_entry(obj, &b->list, node) {
133 if (obj->object == addr)
136 if (cnt > debug_objects_maxchain)
137 debug_objects_maxchain = cnt;
143 * Allocate a new object. If the pool is empty, switch off the debugger.
144 * Must be called with interrupts disabled.
146 static struct debug_obj *
147 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
149 struct debug_obj *obj = NULL;
151 raw_spin_lock(&pool_lock);
152 if (obj_pool.first) {
153 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
157 obj->state = ODEBUG_STATE_NONE;
159 hlist_del(&obj->node);
161 hlist_add_head(&obj->node, &b->list);
164 if (obj_pool_used > obj_pool_max_used)
165 obj_pool_max_used = obj_pool_used;
168 if (obj_pool_free < obj_pool_min_free)
169 obj_pool_min_free = obj_pool_free;
171 raw_spin_unlock(&pool_lock);
177 * workqueue function to free objects.
179 * To reduce contention on the global pool_lock, the actual freeing of
180 * debug objects will be delayed if the pool_lock is busy. We also free
181 * the objects in a batch of 4 for each lock/unlock cycle.
183 #define ODEBUG_FREE_BATCH 4
185 static void free_obj_work(struct work_struct *work)
187 struct debug_obj *objs[ODEBUG_FREE_BATCH];
191 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
193 while (obj_pool_free >= debug_objects_pool_size + ODEBUG_FREE_BATCH) {
194 for (i = 0; i < ODEBUG_FREE_BATCH; i++) {
195 objs[i] = hlist_entry(obj_pool.first,
196 typeof(*objs[0]), node);
197 hlist_del(&objs[i]->node);
200 obj_pool_free -= ODEBUG_FREE_BATCH;
201 debug_objects_freed += ODEBUG_FREE_BATCH;
203 * We release pool_lock across kmem_cache_free() to
204 * avoid contention on pool_lock.
206 raw_spin_unlock_irqrestore(&pool_lock, flags);
207 for (i = 0; i < ODEBUG_FREE_BATCH; i++)
208 kmem_cache_free(obj_cache, objs[i]);
209 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
212 raw_spin_unlock_irqrestore(&pool_lock, flags);
216 * Put the object back into the pool and schedule work to free objects
219 static void free_object(struct debug_obj *obj)
224 raw_spin_lock_irqsave(&pool_lock, flags);
226 * schedule work when the pool is filled and the cache is
229 if (obj_pool_free > debug_objects_pool_size && obj_cache)
231 hlist_add_head(&obj->node, &obj_pool);
234 raw_spin_unlock_irqrestore(&pool_lock, flags);
236 schedule_work(&debug_obj_work);
240 * We run out of memory. That means we probably have tons of objects
243 static void debug_objects_oom(void)
245 struct debug_bucket *db = obj_hash;
246 struct hlist_node *tmp;
247 HLIST_HEAD(freelist);
248 struct debug_obj *obj;
252 pr_warn("Out of memory. ODEBUG disabled\n");
254 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
255 raw_spin_lock_irqsave(&db->lock, flags);
256 hlist_move_list(&db->list, &freelist);
257 raw_spin_unlock_irqrestore(&db->lock, flags);
260 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
261 hlist_del(&obj->node);
268 * We use the pfn of the address for the hash. That way we can check
269 * for freed objects simply by checking the affected bucket.
271 static struct debug_bucket *get_bucket(unsigned long addr)
275 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
276 return &obj_hash[hash];
279 static void debug_print_object(struct debug_obj *obj, char *msg)
281 struct debug_obj_descr *descr = obj->descr;
284 if (limit < 5 && descr != descr_test) {
285 void *hint = descr->debug_hint ?
286 descr->debug_hint(obj->object) : NULL;
288 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
289 "object type: %s hint: %pS\n",
290 msg, obj_states[obj->state], obj->astate,
293 debug_objects_warnings++;
297 * Try to repair the damage, so we have a better chance to get useful
301 debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
302 void * addr, enum debug_obj_state state)
304 if (fixup && fixup(addr, state)) {
305 debug_objects_fixups++;
311 static void debug_object_is_on_stack(void *addr, int onstack)
319 is_on_stack = object_is_on_stack(addr);
320 if (is_on_stack == onstack)
325 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
326 task_stack_page(current));
328 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
329 task_stack_page(current));
335 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
337 enum debug_obj_state state;
338 struct debug_bucket *db;
339 struct debug_obj *obj;
344 db = get_bucket((unsigned long) addr);
346 raw_spin_lock_irqsave(&db->lock, flags);
348 obj = lookup_object(addr, db);
350 obj = alloc_object(addr, db, descr);
352 debug_objects_enabled = 0;
353 raw_spin_unlock_irqrestore(&db->lock, flags);
357 debug_object_is_on_stack(addr, onstack);
360 switch (obj->state) {
361 case ODEBUG_STATE_NONE:
362 case ODEBUG_STATE_INIT:
363 case ODEBUG_STATE_INACTIVE:
364 obj->state = ODEBUG_STATE_INIT;
367 case ODEBUG_STATE_ACTIVE:
368 debug_print_object(obj, "init");
370 raw_spin_unlock_irqrestore(&db->lock, flags);
371 debug_object_fixup(descr->fixup_init, addr, state);
374 case ODEBUG_STATE_DESTROYED:
375 debug_print_object(obj, "init");
381 raw_spin_unlock_irqrestore(&db->lock, flags);
385 * debug_object_init - debug checks when an object is initialized
386 * @addr: address of the object
387 * @descr: pointer to an object specific debug description structure
389 void debug_object_init(void *addr, struct debug_obj_descr *descr)
391 if (!debug_objects_enabled)
394 __debug_object_init(addr, descr, 0);
396 EXPORT_SYMBOL_GPL(debug_object_init);
399 * debug_object_init_on_stack - debug checks when an object on stack is
401 * @addr: address of the object
402 * @descr: pointer to an object specific debug description structure
404 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
406 if (!debug_objects_enabled)
409 __debug_object_init(addr, descr, 1);
411 EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
414 * debug_object_activate - debug checks when an object is activated
415 * @addr: address of the object
416 * @descr: pointer to an object specific debug description structure
417 * Returns 0 for success, -EINVAL for check failed.
419 int debug_object_activate(void *addr, struct debug_obj_descr *descr)
421 enum debug_obj_state state;
422 struct debug_bucket *db;
423 struct debug_obj *obj;
426 struct debug_obj o = { .object = addr,
427 .state = ODEBUG_STATE_NOTAVAILABLE,
430 if (!debug_objects_enabled)
433 db = get_bucket((unsigned long) addr);
435 raw_spin_lock_irqsave(&db->lock, flags);
437 obj = lookup_object(addr, db);
439 switch (obj->state) {
440 case ODEBUG_STATE_INIT:
441 case ODEBUG_STATE_INACTIVE:
442 obj->state = ODEBUG_STATE_ACTIVE;
446 case ODEBUG_STATE_ACTIVE:
447 debug_print_object(obj, "activate");
449 raw_spin_unlock_irqrestore(&db->lock, flags);
450 ret = debug_object_fixup(descr->fixup_activate, addr, state);
451 return ret ? 0 : -EINVAL;
453 case ODEBUG_STATE_DESTROYED:
454 debug_print_object(obj, "activate");
461 raw_spin_unlock_irqrestore(&db->lock, flags);
465 raw_spin_unlock_irqrestore(&db->lock, flags);
467 * We are here when a static object is activated. We
468 * let the type specific code confirm whether this is
469 * true or not. if true, we just make sure that the
470 * static object is tracked in the object tracker. If
471 * not, this must be a bug, so we try to fix it up.
473 if (descr->is_static_object && descr->is_static_object(addr)) {
474 /* track this static object */
475 debug_object_init(addr, descr);
476 debug_object_activate(addr, descr);
478 debug_print_object(&o, "activate");
479 ret = debug_object_fixup(descr->fixup_activate, addr,
480 ODEBUG_STATE_NOTAVAILABLE);
481 return ret ? 0 : -EINVAL;
485 EXPORT_SYMBOL_GPL(debug_object_activate);
488 * debug_object_deactivate - debug checks when an object is deactivated
489 * @addr: address of the object
490 * @descr: pointer to an object specific debug description structure
492 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
494 struct debug_bucket *db;
495 struct debug_obj *obj;
498 if (!debug_objects_enabled)
501 db = get_bucket((unsigned long) addr);
503 raw_spin_lock_irqsave(&db->lock, flags);
505 obj = lookup_object(addr, db);
507 switch (obj->state) {
508 case ODEBUG_STATE_INIT:
509 case ODEBUG_STATE_INACTIVE:
510 case ODEBUG_STATE_ACTIVE:
512 obj->state = ODEBUG_STATE_INACTIVE;
514 debug_print_object(obj, "deactivate");
517 case ODEBUG_STATE_DESTROYED:
518 debug_print_object(obj, "deactivate");
524 struct debug_obj o = { .object = addr,
525 .state = ODEBUG_STATE_NOTAVAILABLE,
528 debug_print_object(&o, "deactivate");
531 raw_spin_unlock_irqrestore(&db->lock, flags);
533 EXPORT_SYMBOL_GPL(debug_object_deactivate);
536 * debug_object_destroy - debug checks when an object is destroyed
537 * @addr: address of the object
538 * @descr: pointer to an object specific debug description structure
540 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
542 enum debug_obj_state state;
543 struct debug_bucket *db;
544 struct debug_obj *obj;
547 if (!debug_objects_enabled)
550 db = get_bucket((unsigned long) addr);
552 raw_spin_lock_irqsave(&db->lock, flags);
554 obj = lookup_object(addr, db);
558 switch (obj->state) {
559 case ODEBUG_STATE_NONE:
560 case ODEBUG_STATE_INIT:
561 case ODEBUG_STATE_INACTIVE:
562 obj->state = ODEBUG_STATE_DESTROYED;
564 case ODEBUG_STATE_ACTIVE:
565 debug_print_object(obj, "destroy");
567 raw_spin_unlock_irqrestore(&db->lock, flags);
568 debug_object_fixup(descr->fixup_destroy, addr, state);
571 case ODEBUG_STATE_DESTROYED:
572 debug_print_object(obj, "destroy");
578 raw_spin_unlock_irqrestore(&db->lock, flags);
580 EXPORT_SYMBOL_GPL(debug_object_destroy);
583 * debug_object_free - debug checks when an object is freed
584 * @addr: address of the object
585 * @descr: pointer to an object specific debug description structure
587 void debug_object_free(void *addr, struct debug_obj_descr *descr)
589 enum debug_obj_state state;
590 struct debug_bucket *db;
591 struct debug_obj *obj;
594 if (!debug_objects_enabled)
597 db = get_bucket((unsigned long) addr);
599 raw_spin_lock_irqsave(&db->lock, flags);
601 obj = lookup_object(addr, db);
605 switch (obj->state) {
606 case ODEBUG_STATE_ACTIVE:
607 debug_print_object(obj, "free");
609 raw_spin_unlock_irqrestore(&db->lock, flags);
610 debug_object_fixup(descr->fixup_free, addr, state);
613 hlist_del(&obj->node);
614 raw_spin_unlock_irqrestore(&db->lock, flags);
619 raw_spin_unlock_irqrestore(&db->lock, flags);
621 EXPORT_SYMBOL_GPL(debug_object_free);
624 * debug_object_assert_init - debug checks when object should be init-ed
625 * @addr: address of the object
626 * @descr: pointer to an object specific debug description structure
628 void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
630 struct debug_bucket *db;
631 struct debug_obj *obj;
634 if (!debug_objects_enabled)
637 db = get_bucket((unsigned long) addr);
639 raw_spin_lock_irqsave(&db->lock, flags);
641 obj = lookup_object(addr, db);
643 struct debug_obj o = { .object = addr,
644 .state = ODEBUG_STATE_NOTAVAILABLE,
647 raw_spin_unlock_irqrestore(&db->lock, flags);
649 * Maybe the object is static, and we let the type specific
650 * code confirm. Track this static object if true, else invoke
653 if (descr->is_static_object && descr->is_static_object(addr)) {
654 /* Track this static object */
655 debug_object_init(addr, descr);
657 debug_print_object(&o, "assert_init");
658 debug_object_fixup(descr->fixup_assert_init, addr,
659 ODEBUG_STATE_NOTAVAILABLE);
664 raw_spin_unlock_irqrestore(&db->lock, flags);
666 EXPORT_SYMBOL_GPL(debug_object_assert_init);
669 * debug_object_active_state - debug checks object usage state machine
670 * @addr: address of the object
671 * @descr: pointer to an object specific debug description structure
672 * @expect: expected state
673 * @next: state to move to if expected state is found
676 debug_object_active_state(void *addr, struct debug_obj_descr *descr,
677 unsigned int expect, unsigned int next)
679 struct debug_bucket *db;
680 struct debug_obj *obj;
683 if (!debug_objects_enabled)
686 db = get_bucket((unsigned long) addr);
688 raw_spin_lock_irqsave(&db->lock, flags);
690 obj = lookup_object(addr, db);
692 switch (obj->state) {
693 case ODEBUG_STATE_ACTIVE:
694 if (obj->astate == expect)
697 debug_print_object(obj, "active_state");
701 debug_print_object(obj, "active_state");
705 struct debug_obj o = { .object = addr,
706 .state = ODEBUG_STATE_NOTAVAILABLE,
709 debug_print_object(&o, "active_state");
712 raw_spin_unlock_irqrestore(&db->lock, flags);
714 EXPORT_SYMBOL_GPL(debug_object_active_state);
716 #ifdef CONFIG_DEBUG_OBJECTS_FREE
717 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
719 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
720 struct hlist_node *tmp;
721 HLIST_HEAD(freelist);
722 struct debug_obj_descr *descr;
723 enum debug_obj_state state;
724 struct debug_bucket *db;
725 struct debug_obj *obj;
728 saddr = (unsigned long) address;
729 eaddr = saddr + size;
730 paddr = saddr & ODEBUG_CHUNK_MASK;
731 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
732 chunks >>= ODEBUG_CHUNK_SHIFT;
734 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
735 db = get_bucket(paddr);
739 raw_spin_lock_irqsave(&db->lock, flags);
740 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
742 oaddr = (unsigned long) obj->object;
743 if (oaddr < saddr || oaddr >= eaddr)
746 switch (obj->state) {
747 case ODEBUG_STATE_ACTIVE:
748 debug_print_object(obj, "free");
751 raw_spin_unlock_irqrestore(&db->lock, flags);
752 debug_object_fixup(descr->fixup_free,
753 (void *) oaddr, state);
756 hlist_del(&obj->node);
757 hlist_add_head(&obj->node, &freelist);
761 raw_spin_unlock_irqrestore(&db->lock, flags);
764 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
765 hlist_del(&obj->node);
769 if (cnt > debug_objects_maxchain)
770 debug_objects_maxchain = cnt;
774 void debug_check_no_obj_freed(const void *address, unsigned long size)
776 if (debug_objects_enabled)
777 __debug_check_no_obj_freed(address, size);
781 #ifdef CONFIG_DEBUG_FS
783 static int debug_stats_show(struct seq_file *m, void *v)
785 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
786 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
787 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
788 seq_printf(m, "pool_free :%d\n", obj_pool_free);
789 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
790 seq_printf(m, "pool_used :%d\n", obj_pool_used);
791 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
792 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
793 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
797 static int debug_stats_open(struct inode *inode, struct file *filp)
799 return single_open(filp, debug_stats_show, NULL);
802 static const struct file_operations debug_stats_fops = {
803 .open = debug_stats_open,
806 .release = single_release,
809 static int __init debug_objects_init_debugfs(void)
811 struct dentry *dbgdir, *dbgstats;
813 if (!debug_objects_enabled)
816 dbgdir = debugfs_create_dir("debug_objects", NULL);
820 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
828 debugfs_remove(dbgdir);
832 __initcall(debug_objects_init_debugfs);
835 static inline void debug_objects_init_debugfs(void) { }
838 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
840 /* Random data structure for the self test */
842 unsigned long dummy1[6];
844 unsigned long dummy2[3];
847 static __initdata struct debug_obj_descr descr_type_test;
849 static bool __init is_static_object(void *addr)
851 struct self_test *obj = addr;
853 return obj->static_init;
857 * fixup_init is called when:
858 * - an active object is initialized
860 static bool __init fixup_init(void *addr, enum debug_obj_state state)
862 struct self_test *obj = addr;
865 case ODEBUG_STATE_ACTIVE:
866 debug_object_deactivate(obj, &descr_type_test);
867 debug_object_init(obj, &descr_type_test);
875 * fixup_activate is called when:
876 * - an active object is activated
877 * - an unknown non-static object is activated
879 static bool __init fixup_activate(void *addr, enum debug_obj_state state)
881 struct self_test *obj = addr;
884 case ODEBUG_STATE_NOTAVAILABLE:
886 case ODEBUG_STATE_ACTIVE:
887 debug_object_deactivate(obj, &descr_type_test);
888 debug_object_activate(obj, &descr_type_test);
897 * fixup_destroy is called when:
898 * - an active object is destroyed
900 static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
902 struct self_test *obj = addr;
905 case ODEBUG_STATE_ACTIVE:
906 debug_object_deactivate(obj, &descr_type_test);
907 debug_object_destroy(obj, &descr_type_test);
915 * fixup_free is called when:
916 * - an active object is freed
918 static bool __init fixup_free(void *addr, enum debug_obj_state state)
920 struct self_test *obj = addr;
923 case ODEBUG_STATE_ACTIVE:
924 debug_object_deactivate(obj, &descr_type_test);
925 debug_object_free(obj, &descr_type_test);
933 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
935 struct debug_bucket *db;
936 struct debug_obj *obj;
940 db = get_bucket((unsigned long) addr);
942 raw_spin_lock_irqsave(&db->lock, flags);
944 obj = lookup_object(addr, db);
945 if (!obj && state != ODEBUG_STATE_NONE) {
946 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
949 if (obj && obj->state != state) {
950 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
954 if (fixups != debug_objects_fixups) {
955 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
956 fixups, debug_objects_fixups);
959 if (warnings != debug_objects_warnings) {
960 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
961 warnings, debug_objects_warnings);
966 raw_spin_unlock_irqrestore(&db->lock, flags);
968 debug_objects_enabled = 0;
972 static __initdata struct debug_obj_descr descr_type_test = {
974 .is_static_object = is_static_object,
975 .fixup_init = fixup_init,
976 .fixup_activate = fixup_activate,
977 .fixup_destroy = fixup_destroy,
978 .fixup_free = fixup_free,
981 static __initdata struct self_test obj = { .static_init = 0 };
983 static void __init debug_objects_selftest(void)
985 int fixups, oldfixups, warnings, oldwarnings;
988 local_irq_save(flags);
990 fixups = oldfixups = debug_objects_fixups;
991 warnings = oldwarnings = debug_objects_warnings;
992 descr_test = &descr_type_test;
994 debug_object_init(&obj, &descr_type_test);
995 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
997 debug_object_activate(&obj, &descr_type_test);
998 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1000 debug_object_activate(&obj, &descr_type_test);
1001 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1003 debug_object_deactivate(&obj, &descr_type_test);
1004 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1006 debug_object_destroy(&obj, &descr_type_test);
1007 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1009 debug_object_init(&obj, &descr_type_test);
1010 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1012 debug_object_activate(&obj, &descr_type_test);
1013 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1015 debug_object_deactivate(&obj, &descr_type_test);
1016 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1018 debug_object_free(&obj, &descr_type_test);
1019 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1022 obj.static_init = 1;
1023 debug_object_activate(&obj, &descr_type_test);
1024 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1026 debug_object_init(&obj, &descr_type_test);
1027 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1029 debug_object_free(&obj, &descr_type_test);
1030 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1033 #ifdef CONFIG_DEBUG_OBJECTS_FREE
1034 debug_object_init(&obj, &descr_type_test);
1035 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1037 debug_object_activate(&obj, &descr_type_test);
1038 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1040 __debug_check_no_obj_freed(&obj, sizeof(obj));
1041 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1044 pr_info("selftest passed\n");
1047 debug_objects_fixups = oldfixups;
1048 debug_objects_warnings = oldwarnings;
1051 local_irq_restore(flags);
1054 static inline void debug_objects_selftest(void) { }
1058 * Called during early boot to initialize the hash buckets and link
1059 * the static object pool objects into the poll list. After this call
1060 * the object tracker is fully operational.
1062 void __init debug_objects_early_init(void)
1066 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1067 raw_spin_lock_init(&obj_hash[i].lock);
1069 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1070 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1074 * Convert the statically allocated objects to dynamic ones:
1076 static int __init debug_objects_replace_static_objects(void)
1078 struct debug_bucket *db = obj_hash;
1079 struct hlist_node *tmp;
1080 struct debug_obj *obj, *new;
1081 HLIST_HEAD(objects);
1084 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1085 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1088 kmemleak_ignore(obj);
1089 hlist_add_head(&obj->node, &objects);
1093 * When debug_objects_mem_init() is called we know that only
1094 * one CPU is up, so disabling interrupts is enough
1095 * protection. This avoids the lockdep hell of lock ordering.
1097 local_irq_disable();
1099 /* Remove the statically allocated objects from the pool */
1100 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1101 hlist_del(&obj->node);
1102 /* Move the allocated objects to the pool */
1103 hlist_move_list(&objects, &obj_pool);
1105 /* Replace the active object references */
1106 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1107 hlist_move_list(&db->list, &objects);
1109 hlist_for_each_entry(obj, &objects, node) {
1110 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1111 hlist_del(&new->node);
1112 /* copy object data */
1114 hlist_add_head(&new->node, &db->list);
1120 pr_debug("%d of %d active objects replaced\n",
1121 cnt, obj_pool_used);
1124 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1125 hlist_del(&obj->node);
1126 kmem_cache_free(obj_cache, obj);
1132 * Called after the kmem_caches are functional to setup a dedicated
1133 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1134 * prevents that the debug code is called on kmem_cache_free() for the
1135 * debug tracker objects to avoid recursive calls.
1137 void __init debug_objects_mem_init(void)
1139 if (!debug_objects_enabled)
1142 obj_cache = kmem_cache_create("debug_objects_cache",
1143 sizeof (struct debug_obj), 0,
1144 SLAB_DEBUG_OBJECTS, NULL);
1146 if (!obj_cache || debug_objects_replace_static_objects()) {
1147 debug_objects_enabled = 0;
1149 kmem_cache_destroy(obj_cache);
1150 pr_warn("out of memory.\n");
1152 debug_objects_selftest();
1155 * Increase the thresholds for allocating and freeing objects
1156 * according to the number of possible CPUs available in the system.
1158 debug_objects_pool_size += num_possible_cpus() * 32;
1159 debug_objects_pool_min_level += num_possible_cpus() * 4;