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/hash.h>
17 #define ODEBUG_HASH_BITS 14
18 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
20 #define ODEBUG_POOL_SIZE 512
21 #define ODEBUG_POOL_MIN_LEVEL 256
23 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
24 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
25 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
28 struct hlist_head list
;
32 static struct debug_bucket obj_hash
[ODEBUG_HASH_SIZE
];
34 static struct debug_obj obj_static_pool
[ODEBUG_POOL_SIZE
] __initdata
;
36 static DEFINE_SPINLOCK(pool_lock
);
38 static HLIST_HEAD(obj_pool
);
40 static int obj_pool_min_free
= ODEBUG_POOL_SIZE
;
41 static int obj_pool_free
= ODEBUG_POOL_SIZE
;
42 static int obj_pool_used
;
43 static int obj_pool_max_used
;
44 static struct kmem_cache
*obj_cache
;
46 static int debug_objects_maxchain __read_mostly
;
47 static int debug_objects_fixups __read_mostly
;
48 static int debug_objects_warnings __read_mostly
;
49 static int debug_objects_enabled __read_mostly
50 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT
;
52 static struct debug_obj_descr
*descr_test __read_mostly
;
54 static void free_obj_work(struct work_struct
*work
);
55 static DECLARE_WORK(debug_obj_work
, free_obj_work
);
57 static int __init
enable_object_debug(char *str
)
59 debug_objects_enabled
= 1;
63 static int __init
disable_object_debug(char *str
)
65 debug_objects_enabled
= 0;
69 early_param("debug_objects", enable_object_debug
);
70 early_param("no_debug_objects", disable_object_debug
);
72 static const char *obj_states
[ODEBUG_STATE_MAX
] = {
73 [ODEBUG_STATE_NONE
] = "none",
74 [ODEBUG_STATE_INIT
] = "initialized",
75 [ODEBUG_STATE_INACTIVE
] = "inactive",
76 [ODEBUG_STATE_ACTIVE
] = "active",
77 [ODEBUG_STATE_DESTROYED
] = "destroyed",
78 [ODEBUG_STATE_NOTAVAILABLE
] = "not available",
81 static int fill_pool(void)
83 gfp_t gfp
= GFP_ATOMIC
| __GFP_NORETRY
| __GFP_NOWARN
;
84 struct debug_obj
*new;
87 if (likely(obj_pool_free
>= ODEBUG_POOL_MIN_LEVEL
))
90 if (unlikely(!obj_cache
))
93 while (obj_pool_free
< ODEBUG_POOL_MIN_LEVEL
) {
95 new = kmem_cache_zalloc(obj_cache
, gfp
);
99 spin_lock_irqsave(&pool_lock
, flags
);
100 hlist_add_head(&new->node
, &obj_pool
);
102 spin_unlock_irqrestore(&pool_lock
, flags
);
104 return obj_pool_free
;
108 * Lookup an object in the hash bucket.
110 static struct debug_obj
*lookup_object(void *addr
, struct debug_bucket
*b
)
112 struct hlist_node
*node
;
113 struct debug_obj
*obj
;
116 hlist_for_each_entry(obj
, node
, &b
->list
, node
) {
118 if (obj
->object
== addr
)
121 if (cnt
> debug_objects_maxchain
)
122 debug_objects_maxchain
= cnt
;
128 * Allocate a new object. If the pool is empty, switch off the debugger.
129 * Must be called with interrupts disabled.
131 static struct debug_obj
*
132 alloc_object(void *addr
, struct debug_bucket
*b
, struct debug_obj_descr
*descr
)
134 struct debug_obj
*obj
= NULL
;
136 spin_lock(&pool_lock
);
137 if (obj_pool
.first
) {
138 obj
= hlist_entry(obj_pool
.first
, typeof(*obj
), node
);
142 obj
->state
= ODEBUG_STATE_NONE
;
143 hlist_del(&obj
->node
);
145 hlist_add_head(&obj
->node
, &b
->list
);
148 if (obj_pool_used
> obj_pool_max_used
)
149 obj_pool_max_used
= obj_pool_used
;
152 if (obj_pool_free
< obj_pool_min_free
)
153 obj_pool_min_free
= obj_pool_free
;
155 spin_unlock(&pool_lock
);
161 * workqueue function to free objects.
163 static void free_obj_work(struct work_struct
*work
)
165 struct debug_obj
*obj
;
168 spin_lock_irqsave(&pool_lock
, flags
);
169 while (obj_pool_free
> ODEBUG_POOL_SIZE
) {
170 obj
= hlist_entry(obj_pool
.first
, typeof(*obj
), node
);
171 hlist_del(&obj
->node
);
174 * We release pool_lock across kmem_cache_free() to
175 * avoid contention on pool_lock.
177 spin_unlock_irqrestore(&pool_lock
, flags
);
178 kmem_cache_free(obj_cache
, obj
);
179 spin_lock_irqsave(&pool_lock
, flags
);
181 spin_unlock_irqrestore(&pool_lock
, flags
);
185 * Put the object back into the pool and schedule work to free objects
188 static void free_object(struct debug_obj
*obj
)
193 spin_lock_irqsave(&pool_lock
, flags
);
195 * schedule work when the pool is filled and the cache is
198 if (obj_pool_free
> ODEBUG_POOL_SIZE
&& obj_cache
)
199 sched
= !work_pending(&debug_obj_work
);
200 hlist_add_head(&obj
->node
, &obj_pool
);
203 spin_unlock_irqrestore(&pool_lock
, flags
);
205 schedule_work(&debug_obj_work
);
209 * We run out of memory. That means we probably have tons of objects
212 static void debug_objects_oom(void)
214 struct debug_bucket
*db
= obj_hash
;
215 struct hlist_node
*node
, *tmp
;
216 HLIST_HEAD(freelist
);
217 struct debug_obj
*obj
;
221 printk(KERN_WARNING
"ODEBUG: Out of memory. ODEBUG disabled\n");
223 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++, db
++) {
224 spin_lock_irqsave(&db
->lock
, flags
);
225 hlist_move_list(&db
->list
, &freelist
);
226 spin_unlock_irqrestore(&db
->lock
, flags
);
229 hlist_for_each_entry_safe(obj
, node
, tmp
, &freelist
, node
) {
230 hlist_del(&obj
->node
);
237 * We use the pfn of the address for the hash. That way we can check
238 * for freed objects simply by checking the affected bucket.
240 static struct debug_bucket
*get_bucket(unsigned long addr
)
244 hash
= hash_long((addr
>> ODEBUG_CHUNK_SHIFT
), ODEBUG_HASH_BITS
);
245 return &obj_hash
[hash
];
248 static void debug_print_object(struct debug_obj
*obj
, char *msg
)
252 if (limit
< 5 && obj
->descr
!= descr_test
) {
254 WARN(1, KERN_ERR
"ODEBUG: %s %s object type: %s\n", msg
,
255 obj_states
[obj
->state
], obj
->descr
->name
);
257 debug_objects_warnings
++;
261 * Try to repair the damage, so we have a better chance to get useful
265 debug_object_fixup(int (*fixup
)(void *addr
, enum debug_obj_state state
),
266 void * addr
, enum debug_obj_state state
)
269 debug_objects_fixups
+= fixup(addr
, state
);
272 static void debug_object_is_on_stack(void *addr
, int onstack
)
280 is_on_stack
= object_is_on_stack(addr
);
281 if (is_on_stack
== onstack
)
287 "ODEBUG: object is on stack, but not annotated\n");
290 "ODEBUG: object is not on stack, but annotated\n");
295 __debug_object_init(void *addr
, struct debug_obj_descr
*descr
, int onstack
)
297 enum debug_obj_state state
;
298 struct debug_bucket
*db
;
299 struct debug_obj
*obj
;
304 db
= get_bucket((unsigned long) addr
);
306 spin_lock_irqsave(&db
->lock
, flags
);
308 obj
= lookup_object(addr
, db
);
310 obj
= alloc_object(addr
, db
, descr
);
312 debug_objects_enabled
= 0;
313 spin_unlock_irqrestore(&db
->lock
, flags
);
317 debug_object_is_on_stack(addr
, onstack
);
320 switch (obj
->state
) {
321 case ODEBUG_STATE_NONE
:
322 case ODEBUG_STATE_INIT
:
323 case ODEBUG_STATE_INACTIVE
:
324 obj
->state
= ODEBUG_STATE_INIT
;
327 case ODEBUG_STATE_ACTIVE
:
328 debug_print_object(obj
, "init");
330 spin_unlock_irqrestore(&db
->lock
, flags
);
331 debug_object_fixup(descr
->fixup_init
, addr
, state
);
334 case ODEBUG_STATE_DESTROYED
:
335 debug_print_object(obj
, "init");
341 spin_unlock_irqrestore(&db
->lock
, flags
);
345 * debug_object_init - debug checks when an object is initialized
346 * @addr: address of the object
347 * @descr: pointer to an object specific debug description structure
349 void debug_object_init(void *addr
, struct debug_obj_descr
*descr
)
351 if (!debug_objects_enabled
)
354 __debug_object_init(addr
, descr
, 0);
358 * debug_object_init_on_stack - debug checks when an object on stack is
360 * @addr: address of the object
361 * @descr: pointer to an object specific debug description structure
363 void debug_object_init_on_stack(void *addr
, struct debug_obj_descr
*descr
)
365 if (!debug_objects_enabled
)
368 __debug_object_init(addr
, descr
, 1);
372 * debug_object_activate - debug checks when an object is activated
373 * @addr: address of the object
374 * @descr: pointer to an object specific debug description structure
376 void debug_object_activate(void *addr
, struct debug_obj_descr
*descr
)
378 enum debug_obj_state state
;
379 struct debug_bucket
*db
;
380 struct debug_obj
*obj
;
383 if (!debug_objects_enabled
)
386 db
= get_bucket((unsigned long) addr
);
388 spin_lock_irqsave(&db
->lock
, flags
);
390 obj
= lookup_object(addr
, db
);
392 switch (obj
->state
) {
393 case ODEBUG_STATE_INIT
:
394 case ODEBUG_STATE_INACTIVE
:
395 obj
->state
= ODEBUG_STATE_ACTIVE
;
398 case ODEBUG_STATE_ACTIVE
:
399 debug_print_object(obj
, "activate");
401 spin_unlock_irqrestore(&db
->lock
, flags
);
402 debug_object_fixup(descr
->fixup_activate
, addr
, state
);
405 case ODEBUG_STATE_DESTROYED
:
406 debug_print_object(obj
, "activate");
411 spin_unlock_irqrestore(&db
->lock
, flags
);
415 spin_unlock_irqrestore(&db
->lock
, flags
);
417 * This happens when a static object is activated. We
418 * let the type specific code decide whether this is
421 debug_object_fixup(descr
->fixup_activate
, addr
,
422 ODEBUG_STATE_NOTAVAILABLE
);
426 * debug_object_deactivate - debug checks when an object is deactivated
427 * @addr: address of the object
428 * @descr: pointer to an object specific debug description structure
430 void debug_object_deactivate(void *addr
, struct debug_obj_descr
*descr
)
432 struct debug_bucket
*db
;
433 struct debug_obj
*obj
;
436 if (!debug_objects_enabled
)
439 db
= get_bucket((unsigned long) addr
);
441 spin_lock_irqsave(&db
->lock
, flags
);
443 obj
= lookup_object(addr
, db
);
445 switch (obj
->state
) {
446 case ODEBUG_STATE_INIT
:
447 case ODEBUG_STATE_INACTIVE
:
448 case ODEBUG_STATE_ACTIVE
:
449 obj
->state
= ODEBUG_STATE_INACTIVE
;
452 case ODEBUG_STATE_DESTROYED
:
453 debug_print_object(obj
, "deactivate");
459 struct debug_obj o
= { .object
= addr
,
460 .state
= ODEBUG_STATE_NOTAVAILABLE
,
463 debug_print_object(&o
, "deactivate");
466 spin_unlock_irqrestore(&db
->lock
, flags
);
470 * debug_object_destroy - debug checks when an object is destroyed
471 * @addr: address of the object
472 * @descr: pointer to an object specific debug description structure
474 void debug_object_destroy(void *addr
, struct debug_obj_descr
*descr
)
476 enum debug_obj_state state
;
477 struct debug_bucket
*db
;
478 struct debug_obj
*obj
;
481 if (!debug_objects_enabled
)
484 db
= get_bucket((unsigned long) addr
);
486 spin_lock_irqsave(&db
->lock
, flags
);
488 obj
= lookup_object(addr
, db
);
492 switch (obj
->state
) {
493 case ODEBUG_STATE_NONE
:
494 case ODEBUG_STATE_INIT
:
495 case ODEBUG_STATE_INACTIVE
:
496 obj
->state
= ODEBUG_STATE_DESTROYED
;
498 case ODEBUG_STATE_ACTIVE
:
499 debug_print_object(obj
, "destroy");
501 spin_unlock_irqrestore(&db
->lock
, flags
);
502 debug_object_fixup(descr
->fixup_destroy
, addr
, state
);
505 case ODEBUG_STATE_DESTROYED
:
506 debug_print_object(obj
, "destroy");
512 spin_unlock_irqrestore(&db
->lock
, flags
);
516 * debug_object_free - debug checks when an object is freed
517 * @addr: address of the object
518 * @descr: pointer to an object specific debug description structure
520 void debug_object_free(void *addr
, struct debug_obj_descr
*descr
)
522 enum debug_obj_state state
;
523 struct debug_bucket
*db
;
524 struct debug_obj
*obj
;
527 if (!debug_objects_enabled
)
530 db
= get_bucket((unsigned long) addr
);
532 spin_lock_irqsave(&db
->lock
, flags
);
534 obj
= lookup_object(addr
, db
);
538 switch (obj
->state
) {
539 case ODEBUG_STATE_ACTIVE
:
540 debug_print_object(obj
, "free");
542 spin_unlock_irqrestore(&db
->lock
, flags
);
543 debug_object_fixup(descr
->fixup_free
, addr
, state
);
546 hlist_del(&obj
->node
);
547 spin_unlock_irqrestore(&db
->lock
, flags
);
552 spin_unlock_irqrestore(&db
->lock
, flags
);
555 #ifdef CONFIG_DEBUG_OBJECTS_FREE
556 static void __debug_check_no_obj_freed(const void *address
, unsigned long size
)
558 unsigned long flags
, oaddr
, saddr
, eaddr
, paddr
, chunks
;
559 struct hlist_node
*node
, *tmp
;
560 HLIST_HEAD(freelist
);
561 struct debug_obj_descr
*descr
;
562 enum debug_obj_state state
;
563 struct debug_bucket
*db
;
564 struct debug_obj
*obj
;
567 saddr
= (unsigned long) address
;
568 eaddr
= saddr
+ size
;
569 paddr
= saddr
& ODEBUG_CHUNK_MASK
;
570 chunks
= ((eaddr
- paddr
) + (ODEBUG_CHUNK_SIZE
- 1));
571 chunks
>>= ODEBUG_CHUNK_SHIFT
;
573 for (;chunks
> 0; chunks
--, paddr
+= ODEBUG_CHUNK_SIZE
) {
574 db
= get_bucket(paddr
);
578 spin_lock_irqsave(&db
->lock
, flags
);
579 hlist_for_each_entry_safe(obj
, node
, tmp
, &db
->list
, node
) {
581 oaddr
= (unsigned long) obj
->object
;
582 if (oaddr
< saddr
|| oaddr
>= eaddr
)
585 switch (obj
->state
) {
586 case ODEBUG_STATE_ACTIVE
:
587 debug_print_object(obj
, "free");
590 spin_unlock_irqrestore(&db
->lock
, flags
);
591 debug_object_fixup(descr
->fixup_free
,
592 (void *) oaddr
, state
);
595 hlist_del(&obj
->node
);
596 hlist_add_head(&obj
->node
, &freelist
);
600 spin_unlock_irqrestore(&db
->lock
, flags
);
603 hlist_for_each_entry_safe(obj
, node
, tmp
, &freelist
, node
) {
604 hlist_del(&obj
->node
);
608 if (cnt
> debug_objects_maxchain
)
609 debug_objects_maxchain
= cnt
;
613 void debug_check_no_obj_freed(const void *address
, unsigned long size
)
615 if (debug_objects_enabled
)
616 __debug_check_no_obj_freed(address
, size
);
620 #ifdef CONFIG_DEBUG_FS
622 static int debug_stats_show(struct seq_file
*m
, void *v
)
624 seq_printf(m
, "max_chain :%d\n", debug_objects_maxchain
);
625 seq_printf(m
, "warnings :%d\n", debug_objects_warnings
);
626 seq_printf(m
, "fixups :%d\n", debug_objects_fixups
);
627 seq_printf(m
, "pool_free :%d\n", obj_pool_free
);
628 seq_printf(m
, "pool_min_free :%d\n", obj_pool_min_free
);
629 seq_printf(m
, "pool_used :%d\n", obj_pool_used
);
630 seq_printf(m
, "pool_max_used :%d\n", obj_pool_max_used
);
634 static int debug_stats_open(struct inode
*inode
, struct file
*filp
)
636 return single_open(filp
, debug_stats_show
, NULL
);
639 static const struct file_operations debug_stats_fops
= {
640 .open
= debug_stats_open
,
643 .release
= single_release
,
646 static int __init
debug_objects_init_debugfs(void)
648 struct dentry
*dbgdir
, *dbgstats
;
650 if (!debug_objects_enabled
)
653 dbgdir
= debugfs_create_dir("debug_objects", NULL
);
657 dbgstats
= debugfs_create_file("stats", 0444, dbgdir
, NULL
,
665 debugfs_remove(dbgdir
);
669 __initcall(debug_objects_init_debugfs
);
672 static inline void debug_objects_init_debugfs(void) { }
675 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
677 /* Random data structure for the self test */
679 unsigned long dummy1
[6];
681 unsigned long dummy2
[3];
684 static __initdata
struct debug_obj_descr descr_type_test
;
687 * fixup_init is called when:
688 * - an active object is initialized
690 static int __init
fixup_init(void *addr
, enum debug_obj_state state
)
692 struct self_test
*obj
= addr
;
695 case ODEBUG_STATE_ACTIVE
:
696 debug_object_deactivate(obj
, &descr_type_test
);
697 debug_object_init(obj
, &descr_type_test
);
705 * fixup_activate is called when:
706 * - an active object is activated
707 * - an unknown object is activated (might be a statically initialized object)
709 static int __init
fixup_activate(void *addr
, enum debug_obj_state state
)
711 struct self_test
*obj
= addr
;
714 case ODEBUG_STATE_NOTAVAILABLE
:
715 if (obj
->static_init
== 1) {
716 debug_object_init(obj
, &descr_type_test
);
717 debug_object_activate(obj
, &descr_type_test
);
719 * Real code should return 0 here ! This is
720 * not a fixup of some bad behaviour. We
721 * merily call the debug_init function to keep
722 * track of the object.
726 /* Real code needs to emit a warning here */
730 case ODEBUG_STATE_ACTIVE
:
731 debug_object_deactivate(obj
, &descr_type_test
);
732 debug_object_activate(obj
, &descr_type_test
);
741 * fixup_destroy is called when:
742 * - an active object is destroyed
744 static int __init
fixup_destroy(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_destroy(obj
, &descr_type_test
);
759 * fixup_free is called when:
760 * - an active object is freed
762 static int __init
fixup_free(void *addr
, enum debug_obj_state state
)
764 struct self_test
*obj
= addr
;
767 case ODEBUG_STATE_ACTIVE
:
768 debug_object_deactivate(obj
, &descr_type_test
);
769 debug_object_free(obj
, &descr_type_test
);
777 check_results(void *addr
, enum debug_obj_state state
, int fixups
, int warnings
)
779 struct debug_bucket
*db
;
780 struct debug_obj
*obj
;
784 db
= get_bucket((unsigned long) addr
);
786 spin_lock_irqsave(&db
->lock
, flags
);
788 obj
= lookup_object(addr
, db
);
789 if (!obj
&& state
!= ODEBUG_STATE_NONE
) {
790 WARN(1, KERN_ERR
"ODEBUG: selftest object not found\n");
793 if (obj
&& obj
->state
!= state
) {
794 WARN(1, KERN_ERR
"ODEBUG: selftest wrong state: %d != %d\n",
798 if (fixups
!= debug_objects_fixups
) {
799 WARN(1, KERN_ERR
"ODEBUG: selftest fixups failed %d != %d\n",
800 fixups
, debug_objects_fixups
);
803 if (warnings
!= debug_objects_warnings
) {
804 WARN(1, KERN_ERR
"ODEBUG: selftest warnings failed %d != %d\n",
805 warnings
, debug_objects_warnings
);
810 spin_unlock_irqrestore(&db
->lock
, flags
);
812 debug_objects_enabled
= 0;
816 static __initdata
struct debug_obj_descr descr_type_test
= {
818 .fixup_init
= fixup_init
,
819 .fixup_activate
= fixup_activate
,
820 .fixup_destroy
= fixup_destroy
,
821 .fixup_free
= fixup_free
,
824 static __initdata
struct self_test obj
= { .static_init
= 0 };
826 static void __init
debug_objects_selftest(void)
828 int fixups
, oldfixups
, warnings
, oldwarnings
;
831 local_irq_save(flags
);
833 fixups
= oldfixups
= debug_objects_fixups
;
834 warnings
= oldwarnings
= debug_objects_warnings
;
835 descr_test
= &descr_type_test
;
837 debug_object_init(&obj
, &descr_type_test
);
838 if (check_results(&obj
, ODEBUG_STATE_INIT
, fixups
, warnings
))
840 debug_object_activate(&obj
, &descr_type_test
);
841 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, fixups
, warnings
))
843 debug_object_activate(&obj
, &descr_type_test
);
844 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, ++fixups
, ++warnings
))
846 debug_object_deactivate(&obj
, &descr_type_test
);
847 if (check_results(&obj
, ODEBUG_STATE_INACTIVE
, fixups
, warnings
))
849 debug_object_destroy(&obj
, &descr_type_test
);
850 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, warnings
))
852 debug_object_init(&obj
, &descr_type_test
);
853 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
855 debug_object_activate(&obj
, &descr_type_test
);
856 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
858 debug_object_deactivate(&obj
, &descr_type_test
);
859 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
861 debug_object_free(&obj
, &descr_type_test
);
862 if (check_results(&obj
, ODEBUG_STATE_NONE
, fixups
, warnings
))
866 debug_object_activate(&obj
, &descr_type_test
);
867 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, ++fixups
, warnings
))
869 debug_object_init(&obj
, &descr_type_test
);
870 if (check_results(&obj
, ODEBUG_STATE_INIT
, ++fixups
, ++warnings
))
872 debug_object_free(&obj
, &descr_type_test
);
873 if (check_results(&obj
, ODEBUG_STATE_NONE
, fixups
, warnings
))
876 #ifdef CONFIG_DEBUG_OBJECTS_FREE
877 debug_object_init(&obj
, &descr_type_test
);
878 if (check_results(&obj
, ODEBUG_STATE_INIT
, fixups
, warnings
))
880 debug_object_activate(&obj
, &descr_type_test
);
881 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, fixups
, warnings
))
883 __debug_check_no_obj_freed(&obj
, sizeof(obj
));
884 if (check_results(&obj
, ODEBUG_STATE_NONE
, ++fixups
, ++warnings
))
887 printk(KERN_INFO
"ODEBUG: selftest passed\n");
890 debug_objects_fixups
= oldfixups
;
891 debug_objects_warnings
= oldwarnings
;
894 local_irq_restore(flags
);
897 static inline void debug_objects_selftest(void) { }
901 * Called during early boot to initialize the hash buckets and link
902 * the static object pool objects into the poll list. After this call
903 * the object tracker is fully operational.
905 void __init
debug_objects_early_init(void)
909 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++)
910 spin_lock_init(&obj_hash
[i
].lock
);
912 for (i
= 0; i
< ODEBUG_POOL_SIZE
; i
++)
913 hlist_add_head(&obj_static_pool
[i
].node
, &obj_pool
);
917 * Convert the statically allocated objects to dynamic ones:
919 static int debug_objects_replace_static_objects(void)
921 struct debug_bucket
*db
= obj_hash
;
922 struct hlist_node
*node
, *tmp
;
923 struct debug_obj
*obj
, *new;
927 for (i
= 0; i
< ODEBUG_POOL_SIZE
; i
++) {
928 obj
= kmem_cache_zalloc(obj_cache
, GFP_KERNEL
);
931 hlist_add_head(&obj
->node
, &objects
);
935 * When debug_objects_mem_init() is called we know that only
936 * one CPU is up, so disabling interrupts is enough
937 * protection. This avoids the lockdep hell of lock ordering.
941 /* Remove the statically allocated objects from the pool */
942 hlist_for_each_entry_safe(obj
, node
, tmp
, &obj_pool
, node
)
943 hlist_del(&obj
->node
);
944 /* Move the allocated objects to the pool */
945 hlist_move_list(&objects
, &obj_pool
);
947 /* Replace the active object references */
948 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++, db
++) {
949 hlist_move_list(&db
->list
, &objects
);
951 hlist_for_each_entry(obj
, node
, &objects
, node
) {
952 new = hlist_entry(obj_pool
.first
, typeof(*obj
), node
);
953 hlist_del(&new->node
);
954 /* copy object data */
956 hlist_add_head(&new->node
, &db
->list
);
961 printk(KERN_DEBUG
"ODEBUG: %d of %d active objects replaced\n", cnt
,
966 hlist_for_each_entry_safe(obj
, node
, tmp
, &objects
, node
) {
967 hlist_del(&obj
->node
);
968 kmem_cache_free(obj_cache
, obj
);
974 * Called after the kmem_caches are functional to setup a dedicated
975 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
976 * prevents that the debug code is called on kmem_cache_free() for the
977 * debug tracker objects to avoid recursive calls.
979 void __init
debug_objects_mem_init(void)
981 if (!debug_objects_enabled
)
984 obj_cache
= kmem_cache_create("debug_objects_cache",
985 sizeof (struct debug_obj
), 0,
986 SLAB_DEBUG_OBJECTS
, NULL
);
988 if (!obj_cache
|| debug_objects_replace_static_objects()) {
989 debug_objects_enabled
= 0;
991 kmem_cache_destroy(obj_cache
);
992 printk(KERN_WARNING
"ODEBUG: out of memory.\n");
994 debug_objects_selftest();