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
10 #include <linux/debugobjects.h>
11 #include <linux/interrupt.h>
12 #include <linux/sched.h>
13 #include <linux/seq_file.h>
14 #include <linux/debugfs.h>
15 #include <linux/slab.h>
16 #include <linux/hash.h>
18 #define ODEBUG_HASH_BITS 14
19 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
21 #define ODEBUG_POOL_SIZE 512
22 #define ODEBUG_POOL_MIN_LEVEL 256
24 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
25 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
26 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
29 struct hlist_head list;
33 static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
35 static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
37 static DEFINE_RAW_SPINLOCK(pool_lock);
39 static HLIST_HEAD(obj_pool);
41 static int obj_pool_min_free = ODEBUG_POOL_SIZE;
42 static int obj_pool_free = ODEBUG_POOL_SIZE;
43 static int obj_pool_used;
44 static int obj_pool_max_used;
45 static struct kmem_cache *obj_cache;
47 static int debug_objects_maxchain __read_mostly;
48 static int debug_objects_fixups __read_mostly;
49 static int debug_objects_warnings __read_mostly;
50 static int debug_objects_enabled __read_mostly
51 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
53 static struct debug_obj_descr *descr_test __read_mostly;
55 static void free_obj_work(struct work_struct *work);
56 static DECLARE_WORK(debug_obj_work, free_obj_work);
58 static int __init enable_object_debug(char *str)
60 debug_objects_enabled = 1;
64 static int __init disable_object_debug(char *str)
66 debug_objects_enabled = 0;
70 early_param("debug_objects", enable_object_debug);
71 early_param("no_debug_objects", disable_object_debug);
73 static const char *obj_states[ODEBUG_STATE_MAX] = {
74 [ODEBUG_STATE_NONE] = "none",
75 [ODEBUG_STATE_INIT] = "initialized",
76 [ODEBUG_STATE_INACTIVE] = "inactive",
77 [ODEBUG_STATE_ACTIVE] = "active",
78 [ODEBUG_STATE_DESTROYED] = "destroyed",
79 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
82 static int fill_pool(void)
84 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
85 struct debug_obj *new;
88 if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
91 if (unlikely(!obj_cache))
94 while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
96 new = kmem_cache_zalloc(obj_cache, gfp);
100 raw_spin_lock_irqsave(&pool_lock, flags);
101 hlist_add_head(&new->node, &obj_pool);
103 raw_spin_unlock_irqrestore(&pool_lock, flags);
105 return obj_pool_free;
109 * Lookup an object in the hash bucket.
111 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
113 struct hlist_node *node;
114 struct debug_obj *obj;
117 hlist_for_each_entry(obj, node, &b->list, node) {
119 if (obj->object == addr)
122 if (cnt > debug_objects_maxchain)
123 debug_objects_maxchain = cnt;
129 * Allocate a new object. If the pool is empty, switch off the debugger.
130 * Must be called with interrupts disabled.
132 static struct debug_obj *
133 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
135 struct debug_obj *obj = NULL;
137 raw_spin_lock(&pool_lock);
138 if (obj_pool.first) {
139 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
143 obj->state = ODEBUG_STATE_NONE;
145 hlist_del(&obj->node);
147 hlist_add_head(&obj->node, &b->list);
150 if (obj_pool_used > obj_pool_max_used)
151 obj_pool_max_used = obj_pool_used;
154 if (obj_pool_free < obj_pool_min_free)
155 obj_pool_min_free = obj_pool_free;
157 raw_spin_unlock(&pool_lock);
163 * workqueue function to free objects.
165 static void free_obj_work(struct work_struct *work)
167 struct debug_obj *obj;
170 raw_spin_lock_irqsave(&pool_lock, flags);
171 while (obj_pool_free > ODEBUG_POOL_SIZE) {
172 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
173 hlist_del(&obj->node);
176 * We release pool_lock across kmem_cache_free() to
177 * avoid contention on pool_lock.
179 raw_spin_unlock_irqrestore(&pool_lock, flags);
180 kmem_cache_free(obj_cache, obj);
181 raw_spin_lock_irqsave(&pool_lock, flags);
183 raw_spin_unlock_irqrestore(&pool_lock, flags);
187 * Put the object back into the pool and schedule work to free objects
190 static void free_object(struct debug_obj *obj)
195 raw_spin_lock_irqsave(&pool_lock, flags);
197 * schedule work when the pool is filled and the cache is
200 if (obj_pool_free > ODEBUG_POOL_SIZE && obj_cache)
201 sched = !work_pending(&debug_obj_work);
202 hlist_add_head(&obj->node, &obj_pool);
205 raw_spin_unlock_irqrestore(&pool_lock, flags);
207 schedule_work(&debug_obj_work);
211 * We run out of memory. That means we probably have tons of objects
214 static void debug_objects_oom(void)
216 struct debug_bucket *db = obj_hash;
217 struct hlist_node *node, *tmp;
218 HLIST_HEAD(freelist);
219 struct debug_obj *obj;
223 printk(KERN_WARNING "ODEBUG: Out of memory. ODEBUG disabled\n");
225 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
226 raw_spin_lock_irqsave(&db->lock, flags);
227 hlist_move_list(&db->list, &freelist);
228 raw_spin_unlock_irqrestore(&db->lock, flags);
231 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
232 hlist_del(&obj->node);
239 * We use the pfn of the address for the hash. That way we can check
240 * for freed objects simply by checking the affected bucket.
242 static struct debug_bucket *get_bucket(unsigned long addr)
246 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
247 return &obj_hash[hash];
250 static void debug_print_object(struct debug_obj *obj, char *msg)
254 if (limit < 5 && obj->descr != descr_test) {
256 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
258 msg, obj_states[obj->state], obj->astate,
261 debug_objects_warnings++;
265 * Try to repair the damage, so we have a better chance to get useful
269 debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state),
270 void * addr, enum debug_obj_state state)
273 debug_objects_fixups += fixup(addr, state);
276 static void debug_object_is_on_stack(void *addr, int onstack)
284 is_on_stack = object_is_on_stack(addr);
285 if (is_on_stack == onstack)
291 "ODEBUG: object is on stack, but not annotated\n");
294 "ODEBUG: object is not on stack, but annotated\n");
299 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
301 enum debug_obj_state state;
302 struct debug_bucket *db;
303 struct debug_obj *obj;
308 db = get_bucket((unsigned long) addr);
310 raw_spin_lock_irqsave(&db->lock, flags);
312 obj = lookup_object(addr, db);
314 obj = alloc_object(addr, db, descr);
316 debug_objects_enabled = 0;
317 raw_spin_unlock_irqrestore(&db->lock, flags);
321 debug_object_is_on_stack(addr, onstack);
324 switch (obj->state) {
325 case ODEBUG_STATE_NONE:
326 case ODEBUG_STATE_INIT:
327 case ODEBUG_STATE_INACTIVE:
328 obj->state = ODEBUG_STATE_INIT;
331 case ODEBUG_STATE_ACTIVE:
332 debug_print_object(obj, "init");
334 raw_spin_unlock_irqrestore(&db->lock, flags);
335 debug_object_fixup(descr->fixup_init, addr, state);
338 case ODEBUG_STATE_DESTROYED:
339 debug_print_object(obj, "init");
345 raw_spin_unlock_irqrestore(&db->lock, flags);
349 * debug_object_init - debug checks when an object is initialized
350 * @addr: address of the object
351 * @descr: pointer to an object specific debug description structure
353 void debug_object_init(void *addr, struct debug_obj_descr *descr)
355 if (!debug_objects_enabled)
358 __debug_object_init(addr, descr, 0);
362 * debug_object_init_on_stack - debug checks when an object on stack is
364 * @addr: address of the object
365 * @descr: pointer to an object specific debug description structure
367 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
369 if (!debug_objects_enabled)
372 __debug_object_init(addr, descr, 1);
376 * debug_object_activate - debug checks when an object is activated
377 * @addr: address of the object
378 * @descr: pointer to an object specific debug description structure
380 void debug_object_activate(void *addr, struct debug_obj_descr *descr)
382 enum debug_obj_state state;
383 struct debug_bucket *db;
384 struct debug_obj *obj;
387 if (!debug_objects_enabled)
390 db = get_bucket((unsigned long) addr);
392 raw_spin_lock_irqsave(&db->lock, flags);
394 obj = lookup_object(addr, db);
396 switch (obj->state) {
397 case ODEBUG_STATE_INIT:
398 case ODEBUG_STATE_INACTIVE:
399 obj->state = ODEBUG_STATE_ACTIVE;
402 case ODEBUG_STATE_ACTIVE:
403 debug_print_object(obj, "activate");
405 raw_spin_unlock_irqrestore(&db->lock, flags);
406 debug_object_fixup(descr->fixup_activate, addr, state);
409 case ODEBUG_STATE_DESTROYED:
410 debug_print_object(obj, "activate");
415 raw_spin_unlock_irqrestore(&db->lock, flags);
419 raw_spin_unlock_irqrestore(&db->lock, flags);
421 * This happens when a static object is activated. We
422 * let the type specific code decide whether this is
425 debug_object_fixup(descr->fixup_activate, addr,
426 ODEBUG_STATE_NOTAVAILABLE);
430 * debug_object_deactivate - debug checks when an object is deactivated
431 * @addr: address of the object
432 * @descr: pointer to an object specific debug description structure
434 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
436 struct debug_bucket *db;
437 struct debug_obj *obj;
440 if (!debug_objects_enabled)
443 db = get_bucket((unsigned long) addr);
445 raw_spin_lock_irqsave(&db->lock, flags);
447 obj = lookup_object(addr, db);
449 switch (obj->state) {
450 case ODEBUG_STATE_INIT:
451 case ODEBUG_STATE_INACTIVE:
452 case ODEBUG_STATE_ACTIVE:
454 obj->state = ODEBUG_STATE_INACTIVE;
456 debug_print_object(obj, "deactivate");
459 case ODEBUG_STATE_DESTROYED:
460 debug_print_object(obj, "deactivate");
466 struct debug_obj o = { .object = addr,
467 .state = ODEBUG_STATE_NOTAVAILABLE,
470 debug_print_object(&o, "deactivate");
473 raw_spin_unlock_irqrestore(&db->lock, flags);
477 * debug_object_destroy - debug checks when an object is destroyed
478 * @addr: address of the object
479 * @descr: pointer to an object specific debug description structure
481 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
483 enum debug_obj_state state;
484 struct debug_bucket *db;
485 struct debug_obj *obj;
488 if (!debug_objects_enabled)
491 db = get_bucket((unsigned long) addr);
493 raw_spin_lock_irqsave(&db->lock, flags);
495 obj = lookup_object(addr, db);
499 switch (obj->state) {
500 case ODEBUG_STATE_NONE:
501 case ODEBUG_STATE_INIT:
502 case ODEBUG_STATE_INACTIVE:
503 obj->state = ODEBUG_STATE_DESTROYED;
505 case ODEBUG_STATE_ACTIVE:
506 debug_print_object(obj, "destroy");
508 raw_spin_unlock_irqrestore(&db->lock, flags);
509 debug_object_fixup(descr->fixup_destroy, addr, state);
512 case ODEBUG_STATE_DESTROYED:
513 debug_print_object(obj, "destroy");
519 raw_spin_unlock_irqrestore(&db->lock, flags);
523 * debug_object_free - debug checks when an object is freed
524 * @addr: address of the object
525 * @descr: pointer to an object specific debug description structure
527 void debug_object_free(void *addr, struct debug_obj_descr *descr)
529 enum debug_obj_state state;
530 struct debug_bucket *db;
531 struct debug_obj *obj;
534 if (!debug_objects_enabled)
537 db = get_bucket((unsigned long) addr);
539 raw_spin_lock_irqsave(&db->lock, flags);
541 obj = lookup_object(addr, db);
545 switch (obj->state) {
546 case ODEBUG_STATE_ACTIVE:
547 debug_print_object(obj, "free");
549 raw_spin_unlock_irqrestore(&db->lock, flags);
550 debug_object_fixup(descr->fixup_free, addr, state);
553 hlist_del(&obj->node);
554 raw_spin_unlock_irqrestore(&db->lock, flags);
559 raw_spin_unlock_irqrestore(&db->lock, flags);
563 * debug_object_active_state - debug checks object usage state machine
564 * @addr: address of the object
565 * @descr: pointer to an object specific debug description structure
566 * @expect: expected state
567 * @next: state to move to if expected state is found
570 debug_object_active_state(void *addr, struct debug_obj_descr *descr,
571 unsigned int expect, unsigned int next)
573 struct debug_bucket *db;
574 struct debug_obj *obj;
577 if (!debug_objects_enabled)
580 db = get_bucket((unsigned long) addr);
582 raw_spin_lock_irqsave(&db->lock, flags);
584 obj = lookup_object(addr, db);
586 switch (obj->state) {
587 case ODEBUG_STATE_ACTIVE:
588 if (obj->astate == expect)
591 debug_print_object(obj, "active_state");
595 debug_print_object(obj, "active_state");
599 struct debug_obj o = { .object = addr,
600 .state = ODEBUG_STATE_NOTAVAILABLE,
603 debug_print_object(&o, "active_state");
606 raw_spin_unlock_irqrestore(&db->lock, flags);
609 #ifdef CONFIG_DEBUG_OBJECTS_FREE
610 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
612 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
613 struct hlist_node *node, *tmp;
614 HLIST_HEAD(freelist);
615 struct debug_obj_descr *descr;
616 enum debug_obj_state state;
617 struct debug_bucket *db;
618 struct debug_obj *obj;
621 saddr = (unsigned long) address;
622 eaddr = saddr + size;
623 paddr = saddr & ODEBUG_CHUNK_MASK;
624 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
625 chunks >>= ODEBUG_CHUNK_SHIFT;
627 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
628 db = get_bucket(paddr);
632 raw_spin_lock_irqsave(&db->lock, flags);
633 hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
635 oaddr = (unsigned long) obj->object;
636 if (oaddr < saddr || oaddr >= eaddr)
639 switch (obj->state) {
640 case ODEBUG_STATE_ACTIVE:
641 debug_print_object(obj, "free");
644 raw_spin_unlock_irqrestore(&db->lock, flags);
645 debug_object_fixup(descr->fixup_free,
646 (void *) oaddr, state);
649 hlist_del(&obj->node);
650 hlist_add_head(&obj->node, &freelist);
654 raw_spin_unlock_irqrestore(&db->lock, flags);
657 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
658 hlist_del(&obj->node);
662 if (cnt > debug_objects_maxchain)
663 debug_objects_maxchain = cnt;
667 void debug_check_no_obj_freed(const void *address, unsigned long size)
669 if (debug_objects_enabled)
670 __debug_check_no_obj_freed(address, size);
674 #ifdef CONFIG_DEBUG_FS
676 static int debug_stats_show(struct seq_file *m, void *v)
678 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
679 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
680 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
681 seq_printf(m, "pool_free :%d\n", obj_pool_free);
682 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
683 seq_printf(m, "pool_used :%d\n", obj_pool_used);
684 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
688 static int debug_stats_open(struct inode *inode, struct file *filp)
690 return single_open(filp, debug_stats_show, NULL);
693 static const struct file_operations debug_stats_fops = {
694 .open = debug_stats_open,
697 .release = single_release,
700 static int __init debug_objects_init_debugfs(void)
702 struct dentry *dbgdir, *dbgstats;
704 if (!debug_objects_enabled)
707 dbgdir = debugfs_create_dir("debug_objects", NULL);
711 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
719 debugfs_remove(dbgdir);
723 __initcall(debug_objects_init_debugfs);
726 static inline void debug_objects_init_debugfs(void) { }
729 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
731 /* Random data structure for the self test */
733 unsigned long dummy1[6];
735 unsigned long dummy2[3];
738 static __initdata struct debug_obj_descr descr_type_test;
741 * fixup_init is called when:
742 * - an active object is initialized
744 static int __init fixup_init(void *addr, enum debug_obj_state state)
746 struct self_test *obj = addr;
749 case ODEBUG_STATE_ACTIVE:
750 debug_object_deactivate(obj, &descr_type_test);
751 debug_object_init(obj, &descr_type_test);
759 * fixup_activate is called when:
760 * - an active object is activated
761 * - an unknown object is activated (might be a statically initialized object)
763 static int __init fixup_activate(void *addr, enum debug_obj_state state)
765 struct self_test *obj = addr;
768 case ODEBUG_STATE_NOTAVAILABLE:
769 if (obj->static_init == 1) {
770 debug_object_init(obj, &descr_type_test);
771 debug_object_activate(obj, &descr_type_test);
773 * Real code should return 0 here ! This is
774 * not a fixup of some bad behaviour. We
775 * merily call the debug_init function to keep
776 * track of the object.
780 /* Real code needs to emit a warning here */
784 case ODEBUG_STATE_ACTIVE:
785 debug_object_deactivate(obj, &descr_type_test);
786 debug_object_activate(obj, &descr_type_test);
795 * fixup_destroy is called when:
796 * - an active object is destroyed
798 static int __init fixup_destroy(void *addr, enum debug_obj_state state)
800 struct self_test *obj = addr;
803 case ODEBUG_STATE_ACTIVE:
804 debug_object_deactivate(obj, &descr_type_test);
805 debug_object_destroy(obj, &descr_type_test);
813 * fixup_free is called when:
814 * - an active object is freed
816 static int __init fixup_free(void *addr, enum debug_obj_state state)
818 struct self_test *obj = addr;
821 case ODEBUG_STATE_ACTIVE:
822 debug_object_deactivate(obj, &descr_type_test);
823 debug_object_free(obj, &descr_type_test);
831 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
833 struct debug_bucket *db;
834 struct debug_obj *obj;
838 db = get_bucket((unsigned long) addr);
840 raw_spin_lock_irqsave(&db->lock, flags);
842 obj = lookup_object(addr, db);
843 if (!obj && state != ODEBUG_STATE_NONE) {
844 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
847 if (obj && obj->state != state) {
848 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
852 if (fixups != debug_objects_fixups) {
853 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
854 fixups, debug_objects_fixups);
857 if (warnings != debug_objects_warnings) {
858 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
859 warnings, debug_objects_warnings);
864 raw_spin_unlock_irqrestore(&db->lock, flags);
866 debug_objects_enabled = 0;
870 static __initdata struct debug_obj_descr descr_type_test = {
872 .fixup_init = fixup_init,
873 .fixup_activate = fixup_activate,
874 .fixup_destroy = fixup_destroy,
875 .fixup_free = fixup_free,
878 static __initdata struct self_test obj = { .static_init = 0 };
880 static void __init debug_objects_selftest(void)
882 int fixups, oldfixups, warnings, oldwarnings;
885 local_irq_save(flags);
887 fixups = oldfixups = debug_objects_fixups;
888 warnings = oldwarnings = debug_objects_warnings;
889 descr_test = &descr_type_test;
891 debug_object_init(&obj, &descr_type_test);
892 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
894 debug_object_activate(&obj, &descr_type_test);
895 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
897 debug_object_activate(&obj, &descr_type_test);
898 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
900 debug_object_deactivate(&obj, &descr_type_test);
901 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
903 debug_object_destroy(&obj, &descr_type_test);
904 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
906 debug_object_init(&obj, &descr_type_test);
907 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
909 debug_object_activate(&obj, &descr_type_test);
910 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
912 debug_object_deactivate(&obj, &descr_type_test);
913 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
915 debug_object_free(&obj, &descr_type_test);
916 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
920 debug_object_activate(&obj, &descr_type_test);
921 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
923 debug_object_init(&obj, &descr_type_test);
924 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
926 debug_object_free(&obj, &descr_type_test);
927 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
930 #ifdef CONFIG_DEBUG_OBJECTS_FREE
931 debug_object_init(&obj, &descr_type_test);
932 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
934 debug_object_activate(&obj, &descr_type_test);
935 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
937 __debug_check_no_obj_freed(&obj, sizeof(obj));
938 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
941 printk(KERN_INFO "ODEBUG: selftest passed\n");
944 debug_objects_fixups = oldfixups;
945 debug_objects_warnings = oldwarnings;
948 local_irq_restore(flags);
951 static inline void debug_objects_selftest(void) { }
955 * Called during early boot to initialize the hash buckets and link
956 * the static object pool objects into the poll list. After this call
957 * the object tracker is fully operational.
959 void __init debug_objects_early_init(void)
963 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
964 raw_spin_lock_init(&obj_hash[i].lock);
966 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
967 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
971 * Convert the statically allocated objects to dynamic ones:
973 static int __init debug_objects_replace_static_objects(void)
975 struct debug_bucket *db = obj_hash;
976 struct hlist_node *node, *tmp;
977 struct debug_obj *obj, *new;
981 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
982 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
985 hlist_add_head(&obj->node, &objects);
989 * When debug_objects_mem_init() is called we know that only
990 * one CPU is up, so disabling interrupts is enough
991 * protection. This avoids the lockdep hell of lock ordering.
995 /* Remove the statically allocated objects from the pool */
996 hlist_for_each_entry_safe(obj, node, tmp, &obj_pool, node)
997 hlist_del(&obj->node);
998 /* Move the allocated objects to the pool */
999 hlist_move_list(&objects, &obj_pool);
1001 /* Replace the active object references */
1002 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1003 hlist_move_list(&db->list, &objects);
1005 hlist_for_each_entry(obj, node, &objects, node) {
1006 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1007 hlist_del(&new->node);
1008 /* copy object data */
1010 hlist_add_head(&new->node, &db->list);
1015 printk(KERN_DEBUG "ODEBUG: %d of %d active objects replaced\n", cnt,
1020 hlist_for_each_entry_safe(obj, node, tmp, &objects, node) {
1021 hlist_del(&obj->node);
1022 kmem_cache_free(obj_cache, obj);
1028 * Called after the kmem_caches are functional to setup a dedicated
1029 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1030 * prevents that the debug code is called on kmem_cache_free() for the
1031 * debug tracker objects to avoid recursive calls.
1033 void __init debug_objects_mem_init(void)
1035 if (!debug_objects_enabled)
1038 obj_cache = kmem_cache_create("debug_objects_cache",
1039 sizeof (struct debug_obj), 0,
1040 SLAB_DEBUG_OBJECTS, NULL);
1042 if (!obj_cache || debug_objects_replace_static_objects()) {
1043 debug_objects_enabled = 0;
1045 kmem_cache_destroy(obj_cache);
1046 printk(KERN_WARNING "ODEBUG: out of memory.\n");
1048 debug_objects_selftest();