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;
284 * Don't report if lookup_object_or_alloc() by the current thread
285 * failed because lookup_object_or_alloc()/debug_objects_oom() by a
286 * concurrent thread turned off debug_objects_enabled and cleared
289 if (!debug_objects_enabled)
292 if (limit < 5 && descr != descr_test) {
293 void *hint = descr->debug_hint ?
294 descr->debug_hint(obj->object) : NULL;
296 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
297 "object type: %s hint: %pS\n",
298 msg, obj_states[obj->state], obj->astate,
301 debug_objects_warnings++;
305 * Try to repair the damage, so we have a better chance to get useful
309 debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
310 void * addr, enum debug_obj_state state)
312 if (fixup && fixup(addr, state)) {
313 debug_objects_fixups++;
319 static void debug_object_is_on_stack(void *addr, int onstack)
327 is_on_stack = object_is_on_stack(addr);
328 if (is_on_stack == onstack)
333 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
334 task_stack_page(current));
336 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
337 task_stack_page(current));
343 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
345 enum debug_obj_state state;
346 struct debug_bucket *db;
347 struct debug_obj *obj;
352 db = get_bucket((unsigned long) addr);
354 raw_spin_lock_irqsave(&db->lock, flags);
356 obj = lookup_object(addr, db);
358 obj = alloc_object(addr, db, descr);
360 debug_objects_enabled = 0;
361 raw_spin_unlock_irqrestore(&db->lock, flags);
365 debug_object_is_on_stack(addr, onstack);
368 switch (obj->state) {
369 case ODEBUG_STATE_NONE:
370 case ODEBUG_STATE_INIT:
371 case ODEBUG_STATE_INACTIVE:
372 obj->state = ODEBUG_STATE_INIT;
375 case ODEBUG_STATE_ACTIVE:
376 debug_print_object(obj, "init");
378 raw_spin_unlock_irqrestore(&db->lock, flags);
379 debug_object_fixup(descr->fixup_init, addr, state);
382 case ODEBUG_STATE_DESTROYED:
383 debug_print_object(obj, "init");
389 raw_spin_unlock_irqrestore(&db->lock, flags);
393 * debug_object_init - debug checks when an object is initialized
394 * @addr: address of the object
395 * @descr: pointer to an object specific debug description structure
397 void debug_object_init(void *addr, struct debug_obj_descr *descr)
399 if (!debug_objects_enabled)
402 __debug_object_init(addr, descr, 0);
404 EXPORT_SYMBOL_GPL(debug_object_init);
407 * debug_object_init_on_stack - debug checks when an object on stack is
409 * @addr: address of the object
410 * @descr: pointer to an object specific debug description structure
412 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
414 if (!debug_objects_enabled)
417 __debug_object_init(addr, descr, 1);
419 EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
422 * debug_object_activate - debug checks when an object is activated
423 * @addr: address of the object
424 * @descr: pointer to an object specific debug description structure
425 * Returns 0 for success, -EINVAL for check failed.
427 int debug_object_activate(void *addr, struct debug_obj_descr *descr)
429 enum debug_obj_state state;
430 struct debug_bucket *db;
431 struct debug_obj *obj;
434 struct debug_obj o = { .object = addr,
435 .state = ODEBUG_STATE_NOTAVAILABLE,
438 if (!debug_objects_enabled)
441 db = get_bucket((unsigned long) addr);
443 raw_spin_lock_irqsave(&db->lock, flags);
445 obj = lookup_object(addr, db);
447 switch (obj->state) {
448 case ODEBUG_STATE_INIT:
449 case ODEBUG_STATE_INACTIVE:
450 obj->state = ODEBUG_STATE_ACTIVE;
454 case ODEBUG_STATE_ACTIVE:
455 debug_print_object(obj, "activate");
457 raw_spin_unlock_irqrestore(&db->lock, flags);
458 ret = debug_object_fixup(descr->fixup_activate, addr, state);
459 return ret ? 0 : -EINVAL;
461 case ODEBUG_STATE_DESTROYED:
462 debug_print_object(obj, "activate");
469 raw_spin_unlock_irqrestore(&db->lock, flags);
473 raw_spin_unlock_irqrestore(&db->lock, flags);
475 * We are here when a static object is activated. We
476 * let the type specific code confirm whether this is
477 * true or not. if true, we just make sure that the
478 * static object is tracked in the object tracker. If
479 * not, this must be a bug, so we try to fix it up.
481 if (descr->is_static_object && descr->is_static_object(addr)) {
482 /* track this static object */
483 debug_object_init(addr, descr);
484 debug_object_activate(addr, descr);
486 debug_print_object(&o, "activate");
487 ret = debug_object_fixup(descr->fixup_activate, addr,
488 ODEBUG_STATE_NOTAVAILABLE);
489 return ret ? 0 : -EINVAL;
493 EXPORT_SYMBOL_GPL(debug_object_activate);
496 * debug_object_deactivate - debug checks when an object is deactivated
497 * @addr: address of the object
498 * @descr: pointer to an object specific debug description structure
500 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
502 struct debug_bucket *db;
503 struct debug_obj *obj;
506 if (!debug_objects_enabled)
509 db = get_bucket((unsigned long) addr);
511 raw_spin_lock_irqsave(&db->lock, flags);
513 obj = lookup_object(addr, db);
515 switch (obj->state) {
516 case ODEBUG_STATE_INIT:
517 case ODEBUG_STATE_INACTIVE:
518 case ODEBUG_STATE_ACTIVE:
520 obj->state = ODEBUG_STATE_INACTIVE;
522 debug_print_object(obj, "deactivate");
525 case ODEBUG_STATE_DESTROYED:
526 debug_print_object(obj, "deactivate");
532 struct debug_obj o = { .object = addr,
533 .state = ODEBUG_STATE_NOTAVAILABLE,
536 debug_print_object(&o, "deactivate");
539 raw_spin_unlock_irqrestore(&db->lock, flags);
541 EXPORT_SYMBOL_GPL(debug_object_deactivate);
544 * debug_object_destroy - debug checks when an object is destroyed
545 * @addr: address of the object
546 * @descr: pointer to an object specific debug description structure
548 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
550 enum debug_obj_state state;
551 struct debug_bucket *db;
552 struct debug_obj *obj;
555 if (!debug_objects_enabled)
558 db = get_bucket((unsigned long) addr);
560 raw_spin_lock_irqsave(&db->lock, flags);
562 obj = lookup_object(addr, db);
566 switch (obj->state) {
567 case ODEBUG_STATE_NONE:
568 case ODEBUG_STATE_INIT:
569 case ODEBUG_STATE_INACTIVE:
570 obj->state = ODEBUG_STATE_DESTROYED;
572 case ODEBUG_STATE_ACTIVE:
573 debug_print_object(obj, "destroy");
575 raw_spin_unlock_irqrestore(&db->lock, flags);
576 debug_object_fixup(descr->fixup_destroy, addr, state);
579 case ODEBUG_STATE_DESTROYED:
580 debug_print_object(obj, "destroy");
586 raw_spin_unlock_irqrestore(&db->lock, flags);
588 EXPORT_SYMBOL_GPL(debug_object_destroy);
591 * debug_object_free - debug checks when an object is freed
592 * @addr: address of the object
593 * @descr: pointer to an object specific debug description structure
595 void debug_object_free(void *addr, struct debug_obj_descr *descr)
597 enum debug_obj_state state;
598 struct debug_bucket *db;
599 struct debug_obj *obj;
602 if (!debug_objects_enabled)
605 db = get_bucket((unsigned long) addr);
607 raw_spin_lock_irqsave(&db->lock, flags);
609 obj = lookup_object(addr, db);
613 switch (obj->state) {
614 case ODEBUG_STATE_ACTIVE:
615 debug_print_object(obj, "free");
617 raw_spin_unlock_irqrestore(&db->lock, flags);
618 debug_object_fixup(descr->fixup_free, addr, state);
621 hlist_del(&obj->node);
622 raw_spin_unlock_irqrestore(&db->lock, flags);
627 raw_spin_unlock_irqrestore(&db->lock, flags);
629 EXPORT_SYMBOL_GPL(debug_object_free);
632 * debug_object_assert_init - debug checks when object should be init-ed
633 * @addr: address of the object
634 * @descr: pointer to an object specific debug description structure
636 void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
638 struct debug_bucket *db;
639 struct debug_obj *obj;
642 if (!debug_objects_enabled)
645 db = get_bucket((unsigned long) addr);
647 raw_spin_lock_irqsave(&db->lock, flags);
649 obj = lookup_object(addr, db);
651 struct debug_obj o = { .object = addr,
652 .state = ODEBUG_STATE_NOTAVAILABLE,
655 raw_spin_unlock_irqrestore(&db->lock, flags);
657 * Maybe the object is static, and we let the type specific
658 * code confirm. Track this static object if true, else invoke
661 if (descr->is_static_object && descr->is_static_object(addr)) {
662 /* Track this static object */
663 debug_object_init(addr, descr);
665 debug_print_object(&o, "assert_init");
666 debug_object_fixup(descr->fixup_assert_init, addr,
667 ODEBUG_STATE_NOTAVAILABLE);
672 raw_spin_unlock_irqrestore(&db->lock, flags);
674 EXPORT_SYMBOL_GPL(debug_object_assert_init);
677 * debug_object_active_state - debug checks object usage state machine
678 * @addr: address of the object
679 * @descr: pointer to an object specific debug description structure
680 * @expect: expected state
681 * @next: state to move to if expected state is found
684 debug_object_active_state(void *addr, struct debug_obj_descr *descr,
685 unsigned int expect, unsigned int next)
687 struct debug_bucket *db;
688 struct debug_obj *obj;
691 if (!debug_objects_enabled)
694 db = get_bucket((unsigned long) addr);
696 raw_spin_lock_irqsave(&db->lock, flags);
698 obj = lookup_object(addr, db);
700 switch (obj->state) {
701 case ODEBUG_STATE_ACTIVE:
702 if (obj->astate == expect)
705 debug_print_object(obj, "active_state");
709 debug_print_object(obj, "active_state");
713 struct debug_obj o = { .object = addr,
714 .state = ODEBUG_STATE_NOTAVAILABLE,
717 debug_print_object(&o, "active_state");
720 raw_spin_unlock_irqrestore(&db->lock, flags);
722 EXPORT_SYMBOL_GPL(debug_object_active_state);
724 #ifdef CONFIG_DEBUG_OBJECTS_FREE
725 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
727 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
728 struct hlist_node *tmp;
729 HLIST_HEAD(freelist);
730 struct debug_obj_descr *descr;
731 enum debug_obj_state state;
732 struct debug_bucket *db;
733 struct debug_obj *obj;
736 saddr = (unsigned long) address;
737 eaddr = saddr + size;
738 paddr = saddr & ODEBUG_CHUNK_MASK;
739 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
740 chunks >>= ODEBUG_CHUNK_SHIFT;
742 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
743 db = get_bucket(paddr);
747 raw_spin_lock_irqsave(&db->lock, flags);
748 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
750 oaddr = (unsigned long) obj->object;
751 if (oaddr < saddr || oaddr >= eaddr)
754 switch (obj->state) {
755 case ODEBUG_STATE_ACTIVE:
756 debug_print_object(obj, "free");
759 raw_spin_unlock_irqrestore(&db->lock, flags);
760 debug_object_fixup(descr->fixup_free,
761 (void *) oaddr, state);
764 hlist_del(&obj->node);
765 hlist_add_head(&obj->node, &freelist);
769 raw_spin_unlock_irqrestore(&db->lock, flags);
772 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
773 hlist_del(&obj->node);
777 if (cnt > debug_objects_maxchain)
778 debug_objects_maxchain = cnt;
782 void debug_check_no_obj_freed(const void *address, unsigned long size)
784 if (debug_objects_enabled)
785 __debug_check_no_obj_freed(address, size);
789 #ifdef CONFIG_DEBUG_FS
791 static int debug_stats_show(struct seq_file *m, void *v)
793 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
794 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
795 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
796 seq_printf(m, "pool_free :%d\n", obj_pool_free);
797 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
798 seq_printf(m, "pool_used :%d\n", obj_pool_used);
799 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
800 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
801 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
805 static int debug_stats_open(struct inode *inode, struct file *filp)
807 return single_open(filp, debug_stats_show, NULL);
810 static const struct file_operations debug_stats_fops = {
811 .open = debug_stats_open,
814 .release = single_release,
817 static int __init debug_objects_init_debugfs(void)
819 struct dentry *dbgdir, *dbgstats;
821 if (!debug_objects_enabled)
824 dbgdir = debugfs_create_dir("debug_objects", NULL);
828 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
836 debugfs_remove(dbgdir);
840 __initcall(debug_objects_init_debugfs);
843 static inline void debug_objects_init_debugfs(void) { }
846 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
848 /* Random data structure for the self test */
850 unsigned long dummy1[6];
852 unsigned long dummy2[3];
855 static __initdata struct debug_obj_descr descr_type_test;
857 static bool __init is_static_object(void *addr)
859 struct self_test *obj = addr;
861 return obj->static_init;
865 * fixup_init is called when:
866 * - an active object is initialized
868 static bool __init fixup_init(void *addr, enum debug_obj_state state)
870 struct self_test *obj = addr;
873 case ODEBUG_STATE_ACTIVE:
874 debug_object_deactivate(obj, &descr_type_test);
875 debug_object_init(obj, &descr_type_test);
883 * fixup_activate is called when:
884 * - an active object is activated
885 * - an unknown non-static object is activated
887 static bool __init fixup_activate(void *addr, enum debug_obj_state state)
889 struct self_test *obj = addr;
892 case ODEBUG_STATE_NOTAVAILABLE:
894 case ODEBUG_STATE_ACTIVE:
895 debug_object_deactivate(obj, &descr_type_test);
896 debug_object_activate(obj, &descr_type_test);
905 * fixup_destroy is called when:
906 * - an active object is destroyed
908 static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
910 struct self_test *obj = addr;
913 case ODEBUG_STATE_ACTIVE:
914 debug_object_deactivate(obj, &descr_type_test);
915 debug_object_destroy(obj, &descr_type_test);
923 * fixup_free is called when:
924 * - an active object is freed
926 static bool __init fixup_free(void *addr, enum debug_obj_state state)
928 struct self_test *obj = addr;
931 case ODEBUG_STATE_ACTIVE:
932 debug_object_deactivate(obj, &descr_type_test);
933 debug_object_free(obj, &descr_type_test);
941 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
943 struct debug_bucket *db;
944 struct debug_obj *obj;
948 db = get_bucket((unsigned long) addr);
950 raw_spin_lock_irqsave(&db->lock, flags);
952 obj = lookup_object(addr, db);
953 if (!obj && state != ODEBUG_STATE_NONE) {
954 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
957 if (obj && obj->state != state) {
958 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
962 if (fixups != debug_objects_fixups) {
963 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
964 fixups, debug_objects_fixups);
967 if (warnings != debug_objects_warnings) {
968 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
969 warnings, debug_objects_warnings);
974 raw_spin_unlock_irqrestore(&db->lock, flags);
976 debug_objects_enabled = 0;
980 static __initdata struct debug_obj_descr descr_type_test = {
982 .is_static_object = is_static_object,
983 .fixup_init = fixup_init,
984 .fixup_activate = fixup_activate,
985 .fixup_destroy = fixup_destroy,
986 .fixup_free = fixup_free,
989 static __initdata struct self_test obj = { .static_init = 0 };
991 static void __init debug_objects_selftest(void)
993 int fixups, oldfixups, warnings, oldwarnings;
996 local_irq_save(flags);
998 fixups = oldfixups = debug_objects_fixups;
999 warnings = oldwarnings = debug_objects_warnings;
1000 descr_test = &descr_type_test;
1002 debug_object_init(&obj, &descr_type_test);
1003 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1005 debug_object_activate(&obj, &descr_type_test);
1006 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1008 debug_object_activate(&obj, &descr_type_test);
1009 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1011 debug_object_deactivate(&obj, &descr_type_test);
1012 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1014 debug_object_destroy(&obj, &descr_type_test);
1015 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1017 debug_object_init(&obj, &descr_type_test);
1018 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1020 debug_object_activate(&obj, &descr_type_test);
1021 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1023 debug_object_deactivate(&obj, &descr_type_test);
1024 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1026 debug_object_free(&obj, &descr_type_test);
1027 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1030 obj.static_init = 1;
1031 debug_object_activate(&obj, &descr_type_test);
1032 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1034 debug_object_init(&obj, &descr_type_test);
1035 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1037 debug_object_free(&obj, &descr_type_test);
1038 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1041 #ifdef CONFIG_DEBUG_OBJECTS_FREE
1042 debug_object_init(&obj, &descr_type_test);
1043 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1045 debug_object_activate(&obj, &descr_type_test);
1046 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1048 __debug_check_no_obj_freed(&obj, sizeof(obj));
1049 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1052 pr_info("selftest passed\n");
1055 debug_objects_fixups = oldfixups;
1056 debug_objects_warnings = oldwarnings;
1059 local_irq_restore(flags);
1062 static inline void debug_objects_selftest(void) { }
1066 * Called during early boot to initialize the hash buckets and link
1067 * the static object pool objects into the poll list. After this call
1068 * the object tracker is fully operational.
1070 void __init debug_objects_early_init(void)
1074 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1075 raw_spin_lock_init(&obj_hash[i].lock);
1077 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1078 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1082 * Convert the statically allocated objects to dynamic ones:
1084 static int __init debug_objects_replace_static_objects(void)
1086 struct debug_bucket *db = obj_hash;
1087 struct hlist_node *tmp;
1088 struct debug_obj *obj, *new;
1089 HLIST_HEAD(objects);
1092 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1093 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1096 hlist_add_head(&obj->node, &objects);
1100 * When debug_objects_mem_init() is called we know that only
1101 * one CPU is up, so disabling interrupts is enough
1102 * protection. This avoids the lockdep hell of lock ordering.
1104 local_irq_disable();
1106 /* Remove the statically allocated objects from the pool */
1107 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1108 hlist_del(&obj->node);
1109 /* Move the allocated objects to the pool */
1110 hlist_move_list(&objects, &obj_pool);
1112 /* Replace the active object references */
1113 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1114 hlist_move_list(&db->list, &objects);
1116 hlist_for_each_entry(obj, &objects, node) {
1117 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1118 hlist_del(&new->node);
1119 /* copy object data */
1121 hlist_add_head(&new->node, &db->list);
1127 pr_debug("%d of %d active objects replaced\n",
1128 cnt, obj_pool_used);
1131 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1132 hlist_del(&obj->node);
1133 kmem_cache_free(obj_cache, obj);
1139 * Called after the kmem_caches are functional to setup a dedicated
1140 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1141 * prevents that the debug code is called on kmem_cache_free() for the
1142 * debug tracker objects to avoid recursive calls.
1144 void __init debug_objects_mem_init(void)
1146 if (!debug_objects_enabled)
1149 obj_cache = kmem_cache_create("debug_objects_cache",
1150 sizeof (struct debug_obj), 0,
1151 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
1154 if (!obj_cache || debug_objects_replace_static_objects()) {
1155 debug_objects_enabled = 0;
1157 kmem_cache_destroy(obj_cache);
1158 pr_warn("out of memory.\n");
1160 debug_objects_selftest();
1163 * Increase the thresholds for allocating and freeing objects
1164 * according to the number of possible CPUs available in the system.
1166 debug_objects_pool_size += num_possible_cpus() * 32;
1167 debug_objects_pool_min_level += num_possible_cpus() * 4;