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/seq_file.h>
13 #include <linux/debugfs.h>
14 #include <linux/hash.h>
16 #define ODEBUG_HASH_BITS 14
17 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
19 #define ODEBUG_POOL_SIZE 512
20 #define ODEBUG_POOL_MIN_LEVEL 256
22 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
23 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
24 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
27 struct hlist_head list
;
31 static struct debug_bucket obj_hash
[ODEBUG_HASH_SIZE
];
33 static struct debug_obj obj_static_pool
[ODEBUG_POOL_SIZE
] __initdata
;
35 static DEFINE_SPINLOCK(pool_lock
);
37 static HLIST_HEAD(obj_pool
);
39 static int obj_pool_min_free
= ODEBUG_POOL_SIZE
;
40 static int obj_pool_free
= ODEBUG_POOL_SIZE
;
41 static int obj_pool_used
;
42 static int obj_pool_max_used
;
43 static struct kmem_cache
*obj_cache
;
45 static int debug_objects_maxchain __read_mostly
;
46 static int debug_objects_fixups __read_mostly
;
47 static int debug_objects_warnings __read_mostly
;
48 static int debug_objects_enabled __read_mostly
49 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT
;
51 static struct debug_obj_descr
*descr_test __read_mostly
;
53 static void free_obj_work(struct work_struct
*work
);
54 static DECLARE_WORK(debug_obj_work
, free_obj_work
);
56 static int __init
enable_object_debug(char *str
)
58 debug_objects_enabled
= 1;
62 static int __init
disable_object_debug(char *str
)
64 debug_objects_enabled
= 0;
68 early_param("debug_objects", enable_object_debug
);
69 early_param("no_debug_objects", disable_object_debug
);
71 static const char *obj_states
[ODEBUG_STATE_MAX
] = {
72 [ODEBUG_STATE_NONE
] = "none",
73 [ODEBUG_STATE_INIT
] = "initialized",
74 [ODEBUG_STATE_INACTIVE
] = "inactive",
75 [ODEBUG_STATE_ACTIVE
] = "active",
76 [ODEBUG_STATE_DESTROYED
] = "destroyed",
77 [ODEBUG_STATE_NOTAVAILABLE
] = "not available",
80 static int fill_pool(void)
82 gfp_t gfp
= GFP_ATOMIC
| __GFP_NORETRY
| __GFP_NOWARN
;
83 struct debug_obj
*new;
86 if (likely(obj_pool_free
>= ODEBUG_POOL_MIN_LEVEL
))
89 if (unlikely(!obj_cache
))
92 while (obj_pool_free
< ODEBUG_POOL_MIN_LEVEL
) {
94 new = kmem_cache_zalloc(obj_cache
, gfp
);
98 spin_lock_irqsave(&pool_lock
, flags
);
99 hlist_add_head(&new->node
, &obj_pool
);
101 spin_unlock_irqrestore(&pool_lock
, flags
);
103 return obj_pool_free
;
107 * Lookup an object in the hash bucket.
109 static struct debug_obj
*lookup_object(void *addr
, struct debug_bucket
*b
)
111 struct hlist_node
*node
;
112 struct debug_obj
*obj
;
115 hlist_for_each_entry(obj
, node
, &b
->list
, node
) {
117 if (obj
->object
== addr
)
120 if (cnt
> debug_objects_maxchain
)
121 debug_objects_maxchain
= cnt
;
127 * Allocate a new object. If the pool is empty, switch off the debugger.
128 * Must be called with interrupts disabled.
130 static struct debug_obj
*
131 alloc_object(void *addr
, struct debug_bucket
*b
, struct debug_obj_descr
*descr
)
133 struct debug_obj
*obj
= NULL
;
135 spin_lock(&pool_lock
);
136 if (obj_pool
.first
) {
137 obj
= hlist_entry(obj_pool
.first
, typeof(*obj
), node
);
141 obj
->state
= ODEBUG_STATE_NONE
;
142 hlist_del(&obj
->node
);
144 hlist_add_head(&obj
->node
, &b
->list
);
147 if (obj_pool_used
> obj_pool_max_used
)
148 obj_pool_max_used
= obj_pool_used
;
151 if (obj_pool_free
< obj_pool_min_free
)
152 obj_pool_min_free
= obj_pool_free
;
154 spin_unlock(&pool_lock
);
160 * workqueue function to free objects.
162 static void free_obj_work(struct work_struct
*work
)
164 struct debug_obj
*obj
;
167 spin_lock_irqsave(&pool_lock
, flags
);
168 while (obj_pool_free
> ODEBUG_POOL_SIZE
) {
169 obj
= hlist_entry(obj_pool
.first
, typeof(*obj
), node
);
170 hlist_del(&obj
->node
);
173 * We release pool_lock across kmem_cache_free() to
174 * avoid contention on pool_lock.
176 spin_unlock_irqrestore(&pool_lock
, flags
);
177 kmem_cache_free(obj_cache
, obj
);
178 spin_lock_irqsave(&pool_lock
, flags
);
180 spin_unlock_irqrestore(&pool_lock
, flags
);
184 * Put the object back into the pool and schedule work to free objects
187 static void free_object(struct debug_obj
*obj
)
192 spin_lock_irqsave(&pool_lock
, flags
);
194 * schedule work when the pool is filled and the cache is
197 if (obj_pool_free
> ODEBUG_POOL_SIZE
&& obj_cache
)
198 sched
= !work_pending(&debug_obj_work
);
199 hlist_add_head(&obj
->node
, &obj_pool
);
202 spin_unlock_irqrestore(&pool_lock
, flags
);
204 schedule_work(&debug_obj_work
);
208 * We run out of memory. That means we probably have tons of objects
211 static void debug_objects_oom(void)
213 struct debug_bucket
*db
= obj_hash
;
214 struct hlist_node
*node
, *tmp
;
215 HLIST_HEAD(freelist
);
216 struct debug_obj
*obj
;
220 printk(KERN_WARNING
"ODEBUG: Out of memory. ODEBUG disabled\n");
222 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++, db
++) {
223 spin_lock_irqsave(&db
->lock
, flags
);
224 hlist_move_list(&db
->list
, &freelist
);
225 spin_unlock_irqrestore(&db
->lock
, flags
);
228 hlist_for_each_entry_safe(obj
, node
, tmp
, &freelist
, node
) {
229 hlist_del(&obj
->node
);
236 * We use the pfn of the address for the hash. That way we can check
237 * for freed objects simply by checking the affected bucket.
239 static struct debug_bucket
*get_bucket(unsigned long addr
)
243 hash
= hash_long((addr
>> ODEBUG_CHUNK_SHIFT
), ODEBUG_HASH_BITS
);
244 return &obj_hash
[hash
];
247 static void debug_print_object(struct debug_obj
*obj
, char *msg
)
251 if (limit
< 5 && obj
->descr
!= descr_test
) {
253 WARN(1, KERN_ERR
"ODEBUG: %s %s object type: %s\n", msg
,
254 obj_states
[obj
->state
], obj
->descr
->name
);
256 debug_objects_warnings
++;
260 * Try to repair the damage, so we have a better chance to get useful
264 debug_object_fixup(int (*fixup
)(void *addr
, enum debug_obj_state state
),
265 void * addr
, enum debug_obj_state state
)
268 debug_objects_fixups
+= fixup(addr
, state
);
271 static void debug_object_is_on_stack(void *addr
, int onstack
)
279 is_on_stack
= object_is_on_stack(addr
);
280 if (is_on_stack
== onstack
)
286 "ODEBUG: object is on stack, but not annotated\n");
289 "ODEBUG: object is not on stack, but annotated\n");
294 __debug_object_init(void *addr
, struct debug_obj_descr
*descr
, int onstack
)
296 enum debug_obj_state state
;
297 struct debug_bucket
*db
;
298 struct debug_obj
*obj
;
303 db
= get_bucket((unsigned long) addr
);
305 spin_lock_irqsave(&db
->lock
, flags
);
307 obj
= lookup_object(addr
, db
);
309 obj
= alloc_object(addr
, db
, descr
);
311 debug_objects_enabled
= 0;
312 spin_unlock_irqrestore(&db
->lock
, flags
);
316 debug_object_is_on_stack(addr
, onstack
);
319 switch (obj
->state
) {
320 case ODEBUG_STATE_NONE
:
321 case ODEBUG_STATE_INIT
:
322 case ODEBUG_STATE_INACTIVE
:
323 obj
->state
= ODEBUG_STATE_INIT
;
326 case ODEBUG_STATE_ACTIVE
:
327 debug_print_object(obj
, "init");
329 spin_unlock_irqrestore(&db
->lock
, flags
);
330 debug_object_fixup(descr
->fixup_init
, addr
, state
);
333 case ODEBUG_STATE_DESTROYED
:
334 debug_print_object(obj
, "init");
340 spin_unlock_irqrestore(&db
->lock
, flags
);
344 * debug_object_init - debug checks when an object is initialized
345 * @addr: address of the object
346 * @descr: pointer to an object specific debug description structure
348 void debug_object_init(void *addr
, struct debug_obj_descr
*descr
)
350 if (!debug_objects_enabled
)
353 __debug_object_init(addr
, descr
, 0);
357 * debug_object_init_on_stack - debug checks when an object on stack is
359 * @addr: address of the object
360 * @descr: pointer to an object specific debug description structure
362 void debug_object_init_on_stack(void *addr
, struct debug_obj_descr
*descr
)
364 if (!debug_objects_enabled
)
367 __debug_object_init(addr
, descr
, 1);
371 * debug_object_activate - debug checks when an object is activated
372 * @addr: address of the object
373 * @descr: pointer to an object specific debug description structure
375 void debug_object_activate(void *addr
, struct debug_obj_descr
*descr
)
377 enum debug_obj_state state
;
378 struct debug_bucket
*db
;
379 struct debug_obj
*obj
;
382 if (!debug_objects_enabled
)
385 db
= get_bucket((unsigned long) addr
);
387 spin_lock_irqsave(&db
->lock
, flags
);
389 obj
= lookup_object(addr
, db
);
391 switch (obj
->state
) {
392 case ODEBUG_STATE_INIT
:
393 case ODEBUG_STATE_INACTIVE
:
394 obj
->state
= ODEBUG_STATE_ACTIVE
;
397 case ODEBUG_STATE_ACTIVE
:
398 debug_print_object(obj
, "activate");
400 spin_unlock_irqrestore(&db
->lock
, flags
);
401 debug_object_fixup(descr
->fixup_activate
, addr
, state
);
404 case ODEBUG_STATE_DESTROYED
:
405 debug_print_object(obj
, "activate");
410 spin_unlock_irqrestore(&db
->lock
, flags
);
414 spin_unlock_irqrestore(&db
->lock
, flags
);
416 * This happens when a static object is activated. We
417 * let the type specific code decide whether this is
420 debug_object_fixup(descr
->fixup_activate
, addr
,
421 ODEBUG_STATE_NOTAVAILABLE
);
425 * debug_object_deactivate - debug checks when an object is deactivated
426 * @addr: address of the object
427 * @descr: pointer to an object specific debug description structure
429 void debug_object_deactivate(void *addr
, struct debug_obj_descr
*descr
)
431 struct debug_bucket
*db
;
432 struct debug_obj
*obj
;
435 if (!debug_objects_enabled
)
438 db
= get_bucket((unsigned long) addr
);
440 spin_lock_irqsave(&db
->lock
, flags
);
442 obj
= lookup_object(addr
, db
);
444 switch (obj
->state
) {
445 case ODEBUG_STATE_INIT
:
446 case ODEBUG_STATE_INACTIVE
:
447 case ODEBUG_STATE_ACTIVE
:
448 obj
->state
= ODEBUG_STATE_INACTIVE
;
451 case ODEBUG_STATE_DESTROYED
:
452 debug_print_object(obj
, "deactivate");
458 struct debug_obj o
= { .object
= addr
,
459 .state
= ODEBUG_STATE_NOTAVAILABLE
,
462 debug_print_object(&o
, "deactivate");
465 spin_unlock_irqrestore(&db
->lock
, flags
);
469 * debug_object_destroy - debug checks when an object is destroyed
470 * @addr: address of the object
471 * @descr: pointer to an object specific debug description structure
473 void debug_object_destroy(void *addr
, struct debug_obj_descr
*descr
)
475 enum debug_obj_state state
;
476 struct debug_bucket
*db
;
477 struct debug_obj
*obj
;
480 if (!debug_objects_enabled
)
483 db
= get_bucket((unsigned long) addr
);
485 spin_lock_irqsave(&db
->lock
, flags
);
487 obj
= lookup_object(addr
, db
);
491 switch (obj
->state
) {
492 case ODEBUG_STATE_NONE
:
493 case ODEBUG_STATE_INIT
:
494 case ODEBUG_STATE_INACTIVE
:
495 obj
->state
= ODEBUG_STATE_DESTROYED
;
497 case ODEBUG_STATE_ACTIVE
:
498 debug_print_object(obj
, "destroy");
500 spin_unlock_irqrestore(&db
->lock
, flags
);
501 debug_object_fixup(descr
->fixup_destroy
, addr
, state
);
504 case ODEBUG_STATE_DESTROYED
:
505 debug_print_object(obj
, "destroy");
511 spin_unlock_irqrestore(&db
->lock
, flags
);
515 * debug_object_free - debug checks when an object is freed
516 * @addr: address of the object
517 * @descr: pointer to an object specific debug description structure
519 void debug_object_free(void *addr
, struct debug_obj_descr
*descr
)
521 enum debug_obj_state state
;
522 struct debug_bucket
*db
;
523 struct debug_obj
*obj
;
526 if (!debug_objects_enabled
)
529 db
= get_bucket((unsigned long) addr
);
531 spin_lock_irqsave(&db
->lock
, flags
);
533 obj
= lookup_object(addr
, db
);
537 switch (obj
->state
) {
538 case ODEBUG_STATE_ACTIVE
:
539 debug_print_object(obj
, "free");
541 spin_unlock_irqrestore(&db
->lock
, flags
);
542 debug_object_fixup(descr
->fixup_free
, addr
, state
);
545 hlist_del(&obj
->node
);
546 spin_unlock_irqrestore(&db
->lock
, flags
);
551 spin_unlock_irqrestore(&db
->lock
, flags
);
554 #ifdef CONFIG_DEBUG_OBJECTS_FREE
555 static void __debug_check_no_obj_freed(const void *address
, unsigned long size
)
557 unsigned long flags
, oaddr
, saddr
, eaddr
, paddr
, chunks
;
558 struct hlist_node
*node
, *tmp
;
559 HLIST_HEAD(freelist
);
560 struct debug_obj_descr
*descr
;
561 enum debug_obj_state state
;
562 struct debug_bucket
*db
;
563 struct debug_obj
*obj
;
566 saddr
= (unsigned long) address
;
567 eaddr
= saddr
+ size
;
568 paddr
= saddr
& ODEBUG_CHUNK_MASK
;
569 chunks
= ((eaddr
- paddr
) + (ODEBUG_CHUNK_SIZE
- 1));
570 chunks
>>= ODEBUG_CHUNK_SHIFT
;
572 for (;chunks
> 0; chunks
--, paddr
+= ODEBUG_CHUNK_SIZE
) {
573 db
= get_bucket(paddr
);
577 spin_lock_irqsave(&db
->lock
, flags
);
578 hlist_for_each_entry_safe(obj
, node
, tmp
, &db
->list
, node
) {
580 oaddr
= (unsigned long) obj
->object
;
581 if (oaddr
< saddr
|| oaddr
>= eaddr
)
584 switch (obj
->state
) {
585 case ODEBUG_STATE_ACTIVE
:
586 debug_print_object(obj
, "free");
589 spin_unlock_irqrestore(&db
->lock
, flags
);
590 debug_object_fixup(descr
->fixup_free
,
591 (void *) oaddr
, state
);
594 hlist_del(&obj
->node
);
595 hlist_add_head(&obj
->node
, &freelist
);
599 spin_unlock_irqrestore(&db
->lock
, flags
);
602 hlist_for_each_entry_safe(obj
, node
, tmp
, &freelist
, node
) {
603 hlist_del(&obj
->node
);
607 if (cnt
> debug_objects_maxchain
)
608 debug_objects_maxchain
= cnt
;
612 void debug_check_no_obj_freed(const void *address
, unsigned long size
)
614 if (debug_objects_enabled
)
615 __debug_check_no_obj_freed(address
, size
);
619 #ifdef CONFIG_DEBUG_FS
621 static int debug_stats_show(struct seq_file
*m
, void *v
)
623 seq_printf(m
, "max_chain :%d\n", debug_objects_maxchain
);
624 seq_printf(m
, "warnings :%d\n", debug_objects_warnings
);
625 seq_printf(m
, "fixups :%d\n", debug_objects_fixups
);
626 seq_printf(m
, "pool_free :%d\n", obj_pool_free
);
627 seq_printf(m
, "pool_min_free :%d\n", obj_pool_min_free
);
628 seq_printf(m
, "pool_used :%d\n", obj_pool_used
);
629 seq_printf(m
, "pool_max_used :%d\n", obj_pool_max_used
);
633 static int debug_stats_open(struct inode
*inode
, struct file
*filp
)
635 return single_open(filp
, debug_stats_show
, NULL
);
638 static const struct file_operations debug_stats_fops
= {
639 .open
= debug_stats_open
,
642 .release
= single_release
,
645 static int __init
debug_objects_init_debugfs(void)
647 struct dentry
*dbgdir
, *dbgstats
;
649 if (!debug_objects_enabled
)
652 dbgdir
= debugfs_create_dir("debug_objects", NULL
);
656 dbgstats
= debugfs_create_file("stats", 0444, dbgdir
, NULL
,
664 debugfs_remove(dbgdir
);
668 __initcall(debug_objects_init_debugfs
);
671 static inline void debug_objects_init_debugfs(void) { }
674 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
676 /* Random data structure for the self test */
678 unsigned long dummy1
[6];
680 unsigned long dummy2
[3];
683 static __initdata
struct debug_obj_descr descr_type_test
;
686 * fixup_init is called when:
687 * - an active object is initialized
689 static int __init
fixup_init(void *addr
, enum debug_obj_state state
)
691 struct self_test
*obj
= addr
;
694 case ODEBUG_STATE_ACTIVE
:
695 debug_object_deactivate(obj
, &descr_type_test
);
696 debug_object_init(obj
, &descr_type_test
);
704 * fixup_activate is called when:
705 * - an active object is activated
706 * - an unknown object is activated (might be a statically initialized object)
708 static int __init
fixup_activate(void *addr
, enum debug_obj_state state
)
710 struct self_test
*obj
= addr
;
713 case ODEBUG_STATE_NOTAVAILABLE
:
714 if (obj
->static_init
== 1) {
715 debug_object_init(obj
, &descr_type_test
);
716 debug_object_activate(obj
, &descr_type_test
);
718 * Real code should return 0 here ! This is
719 * not a fixup of some bad behaviour. We
720 * merily call the debug_init function to keep
721 * track of the object.
725 /* Real code needs to emit a warning here */
729 case ODEBUG_STATE_ACTIVE
:
730 debug_object_deactivate(obj
, &descr_type_test
);
731 debug_object_activate(obj
, &descr_type_test
);
740 * fixup_destroy is called when:
741 * - an active object is destroyed
743 static int __init
fixup_destroy(void *addr
, enum debug_obj_state state
)
745 struct self_test
*obj
= addr
;
748 case ODEBUG_STATE_ACTIVE
:
749 debug_object_deactivate(obj
, &descr_type_test
);
750 debug_object_destroy(obj
, &descr_type_test
);
758 * fixup_free is called when:
759 * - an active object is freed
761 static int __init
fixup_free(void *addr
, enum debug_obj_state state
)
763 struct self_test
*obj
= addr
;
766 case ODEBUG_STATE_ACTIVE
:
767 debug_object_deactivate(obj
, &descr_type_test
);
768 debug_object_free(obj
, &descr_type_test
);
776 check_results(void *addr
, enum debug_obj_state state
, int fixups
, int warnings
)
778 struct debug_bucket
*db
;
779 struct debug_obj
*obj
;
783 db
= get_bucket((unsigned long) addr
);
785 spin_lock_irqsave(&db
->lock
, flags
);
787 obj
= lookup_object(addr
, db
);
788 if (!obj
&& state
!= ODEBUG_STATE_NONE
) {
789 WARN(1, KERN_ERR
"ODEBUG: selftest object not found\n");
792 if (obj
&& obj
->state
!= state
) {
793 WARN(1, KERN_ERR
"ODEBUG: selftest wrong state: %d != %d\n",
797 if (fixups
!= debug_objects_fixups
) {
798 WARN(1, KERN_ERR
"ODEBUG: selftest fixups failed %d != %d\n",
799 fixups
, debug_objects_fixups
);
802 if (warnings
!= debug_objects_warnings
) {
803 WARN(1, KERN_ERR
"ODEBUG: selftest warnings failed %d != %d\n",
804 warnings
, debug_objects_warnings
);
809 spin_unlock_irqrestore(&db
->lock
, flags
);
811 debug_objects_enabled
= 0;
815 static __initdata
struct debug_obj_descr descr_type_test
= {
817 .fixup_init
= fixup_init
,
818 .fixup_activate
= fixup_activate
,
819 .fixup_destroy
= fixup_destroy
,
820 .fixup_free
= fixup_free
,
823 static __initdata
struct self_test obj
= { .static_init
= 0 };
825 static void __init
debug_objects_selftest(void)
827 int fixups
, oldfixups
, warnings
, oldwarnings
;
830 local_irq_save(flags
);
832 fixups
= oldfixups
= debug_objects_fixups
;
833 warnings
= oldwarnings
= debug_objects_warnings
;
834 descr_test
= &descr_type_test
;
836 debug_object_init(&obj
, &descr_type_test
);
837 if (check_results(&obj
, ODEBUG_STATE_INIT
, fixups
, warnings
))
839 debug_object_activate(&obj
, &descr_type_test
);
840 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, fixups
, warnings
))
842 debug_object_activate(&obj
, &descr_type_test
);
843 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, ++fixups
, ++warnings
))
845 debug_object_deactivate(&obj
, &descr_type_test
);
846 if (check_results(&obj
, ODEBUG_STATE_INACTIVE
, fixups
, warnings
))
848 debug_object_destroy(&obj
, &descr_type_test
);
849 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, warnings
))
851 debug_object_init(&obj
, &descr_type_test
);
852 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
854 debug_object_activate(&obj
, &descr_type_test
);
855 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
857 debug_object_deactivate(&obj
, &descr_type_test
);
858 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
860 debug_object_free(&obj
, &descr_type_test
);
861 if (check_results(&obj
, ODEBUG_STATE_NONE
, fixups
, warnings
))
865 debug_object_activate(&obj
, &descr_type_test
);
866 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, ++fixups
, warnings
))
868 debug_object_init(&obj
, &descr_type_test
);
869 if (check_results(&obj
, ODEBUG_STATE_INIT
, ++fixups
, ++warnings
))
871 debug_object_free(&obj
, &descr_type_test
);
872 if (check_results(&obj
, ODEBUG_STATE_NONE
, fixups
, warnings
))
875 #ifdef CONFIG_DEBUG_OBJECTS_FREE
876 debug_object_init(&obj
, &descr_type_test
);
877 if (check_results(&obj
, ODEBUG_STATE_INIT
, fixups
, warnings
))
879 debug_object_activate(&obj
, &descr_type_test
);
880 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, fixups
, warnings
))
882 __debug_check_no_obj_freed(&obj
, sizeof(obj
));
883 if (check_results(&obj
, ODEBUG_STATE_NONE
, ++fixups
, ++warnings
))
886 printk(KERN_INFO
"ODEBUG: selftest passed\n");
889 debug_objects_fixups
= oldfixups
;
890 debug_objects_warnings
= oldwarnings
;
893 local_irq_restore(flags
);
896 static inline void debug_objects_selftest(void) { }
900 * Called during early boot to initialize the hash buckets and link
901 * the static object pool objects into the poll list. After this call
902 * the object tracker is fully operational.
904 void __init
debug_objects_early_init(void)
908 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++)
909 spin_lock_init(&obj_hash
[i
].lock
);
911 for (i
= 0; i
< ODEBUG_POOL_SIZE
; i
++)
912 hlist_add_head(&obj_static_pool
[i
].node
, &obj_pool
);
916 * Convert the statically allocated objects to dynamic ones:
918 static int debug_objects_replace_static_objects(void)
920 struct debug_bucket
*db
= obj_hash
;
921 struct hlist_node
*node
, *tmp
;
922 struct debug_obj
*obj
, *new;
926 for (i
= 0; i
< ODEBUG_POOL_SIZE
; i
++) {
927 obj
= kmem_cache_zalloc(obj_cache
, GFP_KERNEL
);
930 hlist_add_head(&obj
->node
, &objects
);
934 * When debug_objects_mem_init() is called we know that only
935 * one CPU is up, so disabling interrupts is enough
936 * protection. This avoids the lockdep hell of lock ordering.
940 /* Remove the statically allocated objects from the pool */
941 hlist_for_each_entry_safe(obj
, node
, tmp
, &obj_pool
, node
)
942 hlist_del(&obj
->node
);
943 /* Move the allocated objects to the pool */
944 hlist_move_list(&objects
, &obj_pool
);
946 /* Replace the active object references */
947 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++, db
++) {
948 hlist_move_list(&db
->list
, &objects
);
950 hlist_for_each_entry(obj
, node
, &objects
, node
) {
951 new = hlist_entry(obj_pool
.first
, typeof(*obj
), node
);
952 hlist_del(&new->node
);
953 /* copy object data */
955 hlist_add_head(&new->node
, &db
->list
);
960 printk(KERN_DEBUG
"ODEBUG: %d of %d active objects replaced\n", cnt
,
965 hlist_for_each_entry_safe(obj
, node
, tmp
, &objects
, node
) {
966 hlist_del(&obj
->node
);
967 kmem_cache_free(obj_cache
, obj
);
973 * Called after the kmem_caches are functional to setup a dedicated
974 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
975 * prevents that the debug code is called on kmem_cache_free() for the
976 * debug tracker objects to avoid recursive calls.
978 void __init
debug_objects_mem_init(void)
980 if (!debug_objects_enabled
)
983 obj_cache
= kmem_cache_create("debug_objects_cache",
984 sizeof (struct debug_obj
), 0,
985 SLAB_DEBUG_OBJECTS
, NULL
);
987 if (!obj_cache
|| debug_objects_replace_static_objects()) {
988 debug_objects_enabled
= 0;
990 kmem_cache_destroy(obj_cache
);
991 printk(KERN_WARNING
"ODEBUG: out of memory.\n");
993 debug_objects_selftest();