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
;
144 hlist_del(&obj
->node
);
146 hlist_add_head(&obj
->node
, &b
->list
);
149 if (obj_pool_used
> obj_pool_max_used
)
150 obj_pool_max_used
= obj_pool_used
;
153 if (obj_pool_free
< obj_pool_min_free
)
154 obj_pool_min_free
= obj_pool_free
;
156 raw_spin_unlock(&pool_lock
);
162 * workqueue function to free objects.
164 static void free_obj_work(struct work_struct
*work
)
166 struct debug_obj
*obj
;
169 raw_spin_lock_irqsave(&pool_lock
, flags
);
170 while (obj_pool_free
> ODEBUG_POOL_SIZE
) {
171 obj
= hlist_entry(obj_pool
.first
, typeof(*obj
), node
);
172 hlist_del(&obj
->node
);
175 * We release pool_lock across kmem_cache_free() to
176 * avoid contention on pool_lock.
178 raw_spin_unlock_irqrestore(&pool_lock
, flags
);
179 kmem_cache_free(obj_cache
, obj
);
180 raw_spin_lock_irqsave(&pool_lock
, flags
);
182 raw_spin_unlock_irqrestore(&pool_lock
, flags
);
186 * Put the object back into the pool and schedule work to free objects
189 static void free_object(struct debug_obj
*obj
)
194 raw_spin_lock_irqsave(&pool_lock
, flags
);
196 * schedule work when the pool is filled and the cache is
199 if (obj_pool_free
> ODEBUG_POOL_SIZE
&& obj_cache
)
200 sched
= !work_pending(&debug_obj_work
);
201 hlist_add_head(&obj
->node
, &obj_pool
);
204 raw_spin_unlock_irqrestore(&pool_lock
, flags
);
206 schedule_work(&debug_obj_work
);
210 * We run out of memory. That means we probably have tons of objects
213 static void debug_objects_oom(void)
215 struct debug_bucket
*db
= obj_hash
;
216 struct hlist_node
*node
, *tmp
;
217 HLIST_HEAD(freelist
);
218 struct debug_obj
*obj
;
222 printk(KERN_WARNING
"ODEBUG: Out of memory. ODEBUG disabled\n");
224 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++, db
++) {
225 raw_spin_lock_irqsave(&db
->lock
, flags
);
226 hlist_move_list(&db
->list
, &freelist
);
227 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
230 hlist_for_each_entry_safe(obj
, node
, tmp
, &freelist
, node
) {
231 hlist_del(&obj
->node
);
238 * We use the pfn of the address for the hash. That way we can check
239 * for freed objects simply by checking the affected bucket.
241 static struct debug_bucket
*get_bucket(unsigned long addr
)
245 hash
= hash_long((addr
>> ODEBUG_CHUNK_SHIFT
), ODEBUG_HASH_BITS
);
246 return &obj_hash
[hash
];
249 static void debug_print_object(struct debug_obj
*obj
, char *msg
)
253 if (limit
< 5 && obj
->descr
!= descr_test
) {
255 WARN(1, KERN_ERR
"ODEBUG: %s %s object type: %s\n", msg
,
256 obj_states
[obj
->state
], obj
->descr
->name
);
258 debug_objects_warnings
++;
262 * Try to repair the damage, so we have a better chance to get useful
266 debug_object_fixup(int (*fixup
)(void *addr
, enum debug_obj_state state
),
267 void * addr
, enum debug_obj_state state
)
270 debug_objects_fixups
+= fixup(addr
, state
);
273 static void debug_object_is_on_stack(void *addr
, int onstack
)
281 is_on_stack
= object_is_on_stack(addr
);
282 if (is_on_stack
== onstack
)
288 "ODEBUG: object is on stack, but not annotated\n");
291 "ODEBUG: object is not on stack, but annotated\n");
296 __debug_object_init(void *addr
, struct debug_obj_descr
*descr
, int onstack
)
298 enum debug_obj_state state
;
299 struct debug_bucket
*db
;
300 struct debug_obj
*obj
;
305 db
= get_bucket((unsigned long) addr
);
307 raw_spin_lock_irqsave(&db
->lock
, flags
);
309 obj
= lookup_object(addr
, db
);
311 obj
= alloc_object(addr
, db
, descr
);
313 debug_objects_enabled
= 0;
314 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
318 debug_object_is_on_stack(addr
, onstack
);
321 switch (obj
->state
) {
322 case ODEBUG_STATE_NONE
:
323 case ODEBUG_STATE_INIT
:
324 case ODEBUG_STATE_INACTIVE
:
325 obj
->state
= ODEBUG_STATE_INIT
;
328 case ODEBUG_STATE_ACTIVE
:
329 debug_print_object(obj
, "init");
331 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
332 debug_object_fixup(descr
->fixup_init
, addr
, state
);
335 case ODEBUG_STATE_DESTROYED
:
336 debug_print_object(obj
, "init");
342 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
346 * debug_object_init - debug checks when an object is initialized
347 * @addr: address of the object
348 * @descr: pointer to an object specific debug description structure
350 void debug_object_init(void *addr
, struct debug_obj_descr
*descr
)
352 if (!debug_objects_enabled
)
355 __debug_object_init(addr
, descr
, 0);
359 * debug_object_init_on_stack - debug checks when an object on stack is
361 * @addr: address of the object
362 * @descr: pointer to an object specific debug description structure
364 void debug_object_init_on_stack(void *addr
, struct debug_obj_descr
*descr
)
366 if (!debug_objects_enabled
)
369 __debug_object_init(addr
, descr
, 1);
373 * debug_object_activate - debug checks when an object is activated
374 * @addr: address of the object
375 * @descr: pointer to an object specific debug description structure
377 void debug_object_activate(void *addr
, struct debug_obj_descr
*descr
)
379 enum debug_obj_state state
;
380 struct debug_bucket
*db
;
381 struct debug_obj
*obj
;
384 if (!debug_objects_enabled
)
387 db
= get_bucket((unsigned long) addr
);
389 raw_spin_lock_irqsave(&db
->lock
, flags
);
391 obj
= lookup_object(addr
, db
);
393 switch (obj
->state
) {
394 case ODEBUG_STATE_INIT
:
395 case ODEBUG_STATE_INACTIVE
:
396 obj
->state
= ODEBUG_STATE_ACTIVE
;
399 case ODEBUG_STATE_ACTIVE
:
400 debug_print_object(obj
, "activate");
402 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
403 debug_object_fixup(descr
->fixup_activate
, addr
, state
);
406 case ODEBUG_STATE_DESTROYED
:
407 debug_print_object(obj
, "activate");
412 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
416 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
418 * This happens when a static object is activated. We
419 * let the type specific code decide whether this is
422 debug_object_fixup(descr
->fixup_activate
, addr
,
423 ODEBUG_STATE_NOTAVAILABLE
);
427 * debug_object_deactivate - debug checks when an object is deactivated
428 * @addr: address of the object
429 * @descr: pointer to an object specific debug description structure
431 void debug_object_deactivate(void *addr
, struct debug_obj_descr
*descr
)
433 struct debug_bucket
*db
;
434 struct debug_obj
*obj
;
437 if (!debug_objects_enabled
)
440 db
= get_bucket((unsigned long) addr
);
442 raw_spin_lock_irqsave(&db
->lock
, flags
);
444 obj
= lookup_object(addr
, db
);
446 switch (obj
->state
) {
447 case ODEBUG_STATE_INIT
:
448 case ODEBUG_STATE_INACTIVE
:
449 case ODEBUG_STATE_ACTIVE
:
450 obj
->state
= ODEBUG_STATE_INACTIVE
;
453 case ODEBUG_STATE_DESTROYED
:
454 debug_print_object(obj
, "deactivate");
460 struct debug_obj o
= { .object
= addr
,
461 .state
= ODEBUG_STATE_NOTAVAILABLE
,
464 debug_print_object(&o
, "deactivate");
467 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
471 * debug_object_destroy - debug checks when an object is destroyed
472 * @addr: address of the object
473 * @descr: pointer to an object specific debug description structure
475 void debug_object_destroy(void *addr
, struct debug_obj_descr
*descr
)
477 enum debug_obj_state state
;
478 struct debug_bucket
*db
;
479 struct debug_obj
*obj
;
482 if (!debug_objects_enabled
)
485 db
= get_bucket((unsigned long) addr
);
487 raw_spin_lock_irqsave(&db
->lock
, flags
);
489 obj
= lookup_object(addr
, db
);
493 switch (obj
->state
) {
494 case ODEBUG_STATE_NONE
:
495 case ODEBUG_STATE_INIT
:
496 case ODEBUG_STATE_INACTIVE
:
497 obj
->state
= ODEBUG_STATE_DESTROYED
;
499 case ODEBUG_STATE_ACTIVE
:
500 debug_print_object(obj
, "destroy");
502 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
503 debug_object_fixup(descr
->fixup_destroy
, addr
, state
);
506 case ODEBUG_STATE_DESTROYED
:
507 debug_print_object(obj
, "destroy");
513 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
517 * debug_object_free - debug checks when an object is freed
518 * @addr: address of the object
519 * @descr: pointer to an object specific debug description structure
521 void debug_object_free(void *addr
, struct debug_obj_descr
*descr
)
523 enum debug_obj_state state
;
524 struct debug_bucket
*db
;
525 struct debug_obj
*obj
;
528 if (!debug_objects_enabled
)
531 db
= get_bucket((unsigned long) addr
);
533 raw_spin_lock_irqsave(&db
->lock
, flags
);
535 obj
= lookup_object(addr
, db
);
539 switch (obj
->state
) {
540 case ODEBUG_STATE_ACTIVE
:
541 debug_print_object(obj
, "free");
543 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
544 debug_object_fixup(descr
->fixup_free
, addr
, state
);
547 hlist_del(&obj
->node
);
548 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
553 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
556 #ifdef CONFIG_DEBUG_OBJECTS_FREE
557 static void __debug_check_no_obj_freed(const void *address
, unsigned long size
)
559 unsigned long flags
, oaddr
, saddr
, eaddr
, paddr
, chunks
;
560 struct hlist_node
*node
, *tmp
;
561 HLIST_HEAD(freelist
);
562 struct debug_obj_descr
*descr
;
563 enum debug_obj_state state
;
564 struct debug_bucket
*db
;
565 struct debug_obj
*obj
;
568 saddr
= (unsigned long) address
;
569 eaddr
= saddr
+ size
;
570 paddr
= saddr
& ODEBUG_CHUNK_MASK
;
571 chunks
= ((eaddr
- paddr
) + (ODEBUG_CHUNK_SIZE
- 1));
572 chunks
>>= ODEBUG_CHUNK_SHIFT
;
574 for (;chunks
> 0; chunks
--, paddr
+= ODEBUG_CHUNK_SIZE
) {
575 db
= get_bucket(paddr
);
579 raw_spin_lock_irqsave(&db
->lock
, flags
);
580 hlist_for_each_entry_safe(obj
, node
, tmp
, &db
->list
, node
) {
582 oaddr
= (unsigned long) obj
->object
;
583 if (oaddr
< saddr
|| oaddr
>= eaddr
)
586 switch (obj
->state
) {
587 case ODEBUG_STATE_ACTIVE
:
588 debug_print_object(obj
, "free");
591 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
592 debug_object_fixup(descr
->fixup_free
,
593 (void *) oaddr
, state
);
596 hlist_del(&obj
->node
);
597 hlist_add_head(&obj
->node
, &freelist
);
601 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
604 hlist_for_each_entry_safe(obj
, node
, tmp
, &freelist
, node
) {
605 hlist_del(&obj
->node
);
609 if (cnt
> debug_objects_maxchain
)
610 debug_objects_maxchain
= cnt
;
614 void debug_check_no_obj_freed(const void *address
, unsigned long size
)
616 if (debug_objects_enabled
)
617 __debug_check_no_obj_freed(address
, size
);
621 #ifdef CONFIG_DEBUG_FS
623 static int debug_stats_show(struct seq_file
*m
, void *v
)
625 seq_printf(m
, "max_chain :%d\n", debug_objects_maxchain
);
626 seq_printf(m
, "warnings :%d\n", debug_objects_warnings
);
627 seq_printf(m
, "fixups :%d\n", debug_objects_fixups
);
628 seq_printf(m
, "pool_free :%d\n", obj_pool_free
);
629 seq_printf(m
, "pool_min_free :%d\n", obj_pool_min_free
);
630 seq_printf(m
, "pool_used :%d\n", obj_pool_used
);
631 seq_printf(m
, "pool_max_used :%d\n", obj_pool_max_used
);
635 static int debug_stats_open(struct inode
*inode
, struct file
*filp
)
637 return single_open(filp
, debug_stats_show
, NULL
);
640 static const struct file_operations debug_stats_fops
= {
641 .open
= debug_stats_open
,
644 .release
= single_release
,
647 static int __init
debug_objects_init_debugfs(void)
649 struct dentry
*dbgdir
, *dbgstats
;
651 if (!debug_objects_enabled
)
654 dbgdir
= debugfs_create_dir("debug_objects", NULL
);
658 dbgstats
= debugfs_create_file("stats", 0444, dbgdir
, NULL
,
666 debugfs_remove(dbgdir
);
670 __initcall(debug_objects_init_debugfs
);
673 static inline void debug_objects_init_debugfs(void) { }
676 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
678 /* Random data structure for the self test */
680 unsigned long dummy1
[6];
682 unsigned long dummy2
[3];
685 static __initdata
struct debug_obj_descr descr_type_test
;
688 * fixup_init is called when:
689 * - an active object is initialized
691 static int __init
fixup_init(void *addr
, enum debug_obj_state state
)
693 struct self_test
*obj
= addr
;
696 case ODEBUG_STATE_ACTIVE
:
697 debug_object_deactivate(obj
, &descr_type_test
);
698 debug_object_init(obj
, &descr_type_test
);
706 * fixup_activate is called when:
707 * - an active object is activated
708 * - an unknown object is activated (might be a statically initialized object)
710 static int __init
fixup_activate(void *addr
, enum debug_obj_state state
)
712 struct self_test
*obj
= addr
;
715 case ODEBUG_STATE_NOTAVAILABLE
:
716 if (obj
->static_init
== 1) {
717 debug_object_init(obj
, &descr_type_test
);
718 debug_object_activate(obj
, &descr_type_test
);
720 * Real code should return 0 here ! This is
721 * not a fixup of some bad behaviour. We
722 * merily call the debug_init function to keep
723 * track of the object.
727 /* Real code needs to emit a warning here */
731 case ODEBUG_STATE_ACTIVE
:
732 debug_object_deactivate(obj
, &descr_type_test
);
733 debug_object_activate(obj
, &descr_type_test
);
742 * fixup_destroy is called when:
743 * - an active object is destroyed
745 static int __init
fixup_destroy(void *addr
, enum debug_obj_state state
)
747 struct self_test
*obj
= addr
;
750 case ODEBUG_STATE_ACTIVE
:
751 debug_object_deactivate(obj
, &descr_type_test
);
752 debug_object_destroy(obj
, &descr_type_test
);
760 * fixup_free is called when:
761 * - an active object is freed
763 static int __init
fixup_free(void *addr
, enum debug_obj_state state
)
765 struct self_test
*obj
= addr
;
768 case ODEBUG_STATE_ACTIVE
:
769 debug_object_deactivate(obj
, &descr_type_test
);
770 debug_object_free(obj
, &descr_type_test
);
778 check_results(void *addr
, enum debug_obj_state state
, int fixups
, int warnings
)
780 struct debug_bucket
*db
;
781 struct debug_obj
*obj
;
785 db
= get_bucket((unsigned long) addr
);
787 raw_spin_lock_irqsave(&db
->lock
, flags
);
789 obj
= lookup_object(addr
, db
);
790 if (!obj
&& state
!= ODEBUG_STATE_NONE
) {
791 WARN(1, KERN_ERR
"ODEBUG: selftest object not found\n");
794 if (obj
&& obj
->state
!= state
) {
795 WARN(1, KERN_ERR
"ODEBUG: selftest wrong state: %d != %d\n",
799 if (fixups
!= debug_objects_fixups
) {
800 WARN(1, KERN_ERR
"ODEBUG: selftest fixups failed %d != %d\n",
801 fixups
, debug_objects_fixups
);
804 if (warnings
!= debug_objects_warnings
) {
805 WARN(1, KERN_ERR
"ODEBUG: selftest warnings failed %d != %d\n",
806 warnings
, debug_objects_warnings
);
811 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
813 debug_objects_enabled
= 0;
817 static __initdata
struct debug_obj_descr descr_type_test
= {
819 .fixup_init
= fixup_init
,
820 .fixup_activate
= fixup_activate
,
821 .fixup_destroy
= fixup_destroy
,
822 .fixup_free
= fixup_free
,
825 static __initdata
struct self_test obj
= { .static_init
= 0 };
827 static void __init
debug_objects_selftest(void)
829 int fixups
, oldfixups
, warnings
, oldwarnings
;
832 local_irq_save(flags
);
834 fixups
= oldfixups
= debug_objects_fixups
;
835 warnings
= oldwarnings
= debug_objects_warnings
;
836 descr_test
= &descr_type_test
;
838 debug_object_init(&obj
, &descr_type_test
);
839 if (check_results(&obj
, ODEBUG_STATE_INIT
, fixups
, warnings
))
841 debug_object_activate(&obj
, &descr_type_test
);
842 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, fixups
, warnings
))
844 debug_object_activate(&obj
, &descr_type_test
);
845 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, ++fixups
, ++warnings
))
847 debug_object_deactivate(&obj
, &descr_type_test
);
848 if (check_results(&obj
, ODEBUG_STATE_INACTIVE
, fixups
, warnings
))
850 debug_object_destroy(&obj
, &descr_type_test
);
851 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, warnings
))
853 debug_object_init(&obj
, &descr_type_test
);
854 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
856 debug_object_activate(&obj
, &descr_type_test
);
857 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
859 debug_object_deactivate(&obj
, &descr_type_test
);
860 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
862 debug_object_free(&obj
, &descr_type_test
);
863 if (check_results(&obj
, ODEBUG_STATE_NONE
, fixups
, warnings
))
867 debug_object_activate(&obj
, &descr_type_test
);
868 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, ++fixups
, warnings
))
870 debug_object_init(&obj
, &descr_type_test
);
871 if (check_results(&obj
, ODEBUG_STATE_INIT
, ++fixups
, ++warnings
))
873 debug_object_free(&obj
, &descr_type_test
);
874 if (check_results(&obj
, ODEBUG_STATE_NONE
, fixups
, warnings
))
877 #ifdef CONFIG_DEBUG_OBJECTS_FREE
878 debug_object_init(&obj
, &descr_type_test
);
879 if (check_results(&obj
, ODEBUG_STATE_INIT
, fixups
, warnings
))
881 debug_object_activate(&obj
, &descr_type_test
);
882 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, fixups
, warnings
))
884 __debug_check_no_obj_freed(&obj
, sizeof(obj
));
885 if (check_results(&obj
, ODEBUG_STATE_NONE
, ++fixups
, ++warnings
))
888 printk(KERN_INFO
"ODEBUG: selftest passed\n");
891 debug_objects_fixups
= oldfixups
;
892 debug_objects_warnings
= oldwarnings
;
895 local_irq_restore(flags
);
898 static inline void debug_objects_selftest(void) { }
902 * Called during early boot to initialize the hash buckets and link
903 * the static object pool objects into the poll list. After this call
904 * the object tracker is fully operational.
906 void __init
debug_objects_early_init(void)
910 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++)
911 raw_spin_lock_init(&obj_hash
[i
].lock
);
913 for (i
= 0; i
< ODEBUG_POOL_SIZE
; i
++)
914 hlist_add_head(&obj_static_pool
[i
].node
, &obj_pool
);
918 * Convert the statically allocated objects to dynamic ones:
920 static int debug_objects_replace_static_objects(void)
922 struct debug_bucket
*db
= obj_hash
;
923 struct hlist_node
*node
, *tmp
;
924 struct debug_obj
*obj
, *new;
928 for (i
= 0; i
< ODEBUG_POOL_SIZE
; i
++) {
929 obj
= kmem_cache_zalloc(obj_cache
, GFP_KERNEL
);
932 hlist_add_head(&obj
->node
, &objects
);
936 * When debug_objects_mem_init() is called we know that only
937 * one CPU is up, so disabling interrupts is enough
938 * protection. This avoids the lockdep hell of lock ordering.
942 /* Remove the statically allocated objects from the pool */
943 hlist_for_each_entry_safe(obj
, node
, tmp
, &obj_pool
, node
)
944 hlist_del(&obj
->node
);
945 /* Move the allocated objects to the pool */
946 hlist_move_list(&objects
, &obj_pool
);
948 /* Replace the active object references */
949 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++, db
++) {
950 hlist_move_list(&db
->list
, &objects
);
952 hlist_for_each_entry(obj
, node
, &objects
, node
) {
953 new = hlist_entry(obj_pool
.first
, typeof(*obj
), node
);
954 hlist_del(&new->node
);
955 /* copy object data */
957 hlist_add_head(&new->node
, &db
->list
);
962 printk(KERN_DEBUG
"ODEBUG: %d of %d active objects replaced\n", cnt
,
967 hlist_for_each_entry_safe(obj
, node
, tmp
, &objects
, node
) {
968 hlist_del(&obj
->node
);
969 kmem_cache_free(obj_cache
, obj
);
975 * Called after the kmem_caches are functional to setup a dedicated
976 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
977 * prevents that the debug code is called on kmem_cache_free() for the
978 * debug tracker objects to avoid recursive calls.
980 void __init
debug_objects_mem_init(void)
982 if (!debug_objects_enabled
)
985 obj_cache
= kmem_cache_create("debug_objects_cache",
986 sizeof (struct debug_obj
), 0,
987 SLAB_DEBUG_OBJECTS
, NULL
);
989 if (!obj_cache
|| debug_objects_replace_static_objects()) {
990 debug_objects_enabled
= 0;
992 kmem_cache_destroy(obj_cache
);
993 printk(KERN_WARNING
"ODEBUG: out of memory.\n");
995 debug_objects_selftest();