net: dcb: getnumtcs()/setnumtcs() should return an int
[linux-2.6.git] / lib / debugobjects.c
blob77cb245f8e7bdee634b98f6c275ae221c7c90715
1 /*
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
9 */
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))
28 struct debug_bucket {
29 struct hlist_head list;
30 raw_spinlock_t lock;
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;
61 return 0;
64 static int __init disable_object_debug(char *str)
66 debug_objects_enabled = 0;
67 return 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;
86 unsigned long flags;
88 if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
89 return obj_pool_free;
91 if (unlikely(!obj_cache))
92 return obj_pool_free;
94 while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
96 new = kmem_cache_zalloc(obj_cache, gfp);
97 if (!new)
98 return obj_pool_free;
100 raw_spin_lock_irqsave(&pool_lock, flags);
101 hlist_add_head(&new->node, &obj_pool);
102 obj_pool_free++;
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;
115 int cnt = 0;
117 hlist_for_each_entry(obj, node, &b->list, node) {
118 cnt++;
119 if (obj->object == addr)
120 return obj;
122 if (cnt > debug_objects_maxchain)
123 debug_objects_maxchain = cnt;
125 return NULL;
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);
141 obj->object = addr;
142 obj->descr = descr;
143 obj->state = ODEBUG_STATE_NONE;
144 obj->astate = 0;
145 hlist_del(&obj->node);
147 hlist_add_head(&obj->node, &b->list);
149 obj_pool_used++;
150 if (obj_pool_used > obj_pool_max_used)
151 obj_pool_max_used = obj_pool_used;
153 obj_pool_free--;
154 if (obj_pool_free < obj_pool_min_free)
155 obj_pool_min_free = obj_pool_free;
157 raw_spin_unlock(&pool_lock);
159 return obj;
163 * workqueue function to free objects.
165 static void free_obj_work(struct work_struct *work)
167 struct debug_obj *obj;
168 unsigned long flags;
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);
174 obj_pool_free--;
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
188 * if necessary.
190 static void free_object(struct debug_obj *obj)
192 unsigned long flags;
193 int sched = 0;
195 raw_spin_lock_irqsave(&pool_lock, flags);
197 * schedule work when the pool is filled and the cache is
198 * initialized:
200 if (obj_pool_free > ODEBUG_POOL_SIZE && obj_cache)
201 sched = keventd_up() && !work_pending(&debug_obj_work);
202 hlist_add_head(&obj->node, &obj_pool);
203 obj_pool_free++;
204 obj_pool_used--;
205 raw_spin_unlock_irqrestore(&pool_lock, flags);
206 if (sched)
207 schedule_work(&debug_obj_work);
211 * We run out of memory. That means we probably have tons of objects
212 * allocated.
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;
220 unsigned long flags;
221 int i;
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);
230 /* Now free them */
231 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
232 hlist_del(&obj->node);
233 free_object(obj);
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)
244 unsigned long hash;
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)
252 struct debug_obj_descr *descr = obj->descr;
253 static int limit;
255 if (limit < 5 && descr != descr_test) {
256 void *hint = descr->debug_hint ?
257 descr->debug_hint(obj->object) : NULL;
258 limit++;
259 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
260 "object type: %s hint: %pS\n",
261 msg, obj_states[obj->state], obj->astate,
262 descr->name, hint);
264 debug_objects_warnings++;
268 * Try to repair the damage, so we have a better chance to get useful
269 * debug output.
271 static int
272 debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state),
273 void * addr, enum debug_obj_state state)
275 int fixed = 0;
277 if (fixup)
278 fixed = fixup(addr, state);
279 debug_objects_fixups += fixed;
280 return fixed;
283 static void debug_object_is_on_stack(void *addr, int onstack)
285 int is_on_stack;
286 static int limit;
288 if (limit > 4)
289 return;
291 is_on_stack = object_is_on_stack(addr);
292 if (is_on_stack == onstack)
293 return;
295 limit++;
296 if (is_on_stack)
297 printk(KERN_WARNING
298 "ODEBUG: object is on stack, but not annotated\n");
299 else
300 printk(KERN_WARNING
301 "ODEBUG: object is not on stack, but annotated\n");
302 WARN_ON(1);
305 static void
306 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
308 enum debug_obj_state state;
309 struct debug_bucket *db;
310 struct debug_obj *obj;
311 unsigned long flags;
313 fill_pool();
315 db = get_bucket((unsigned long) addr);
317 raw_spin_lock_irqsave(&db->lock, flags);
319 obj = lookup_object(addr, db);
320 if (!obj) {
321 obj = alloc_object(addr, db, descr);
322 if (!obj) {
323 debug_objects_enabled = 0;
324 raw_spin_unlock_irqrestore(&db->lock, flags);
325 debug_objects_oom();
326 return;
328 debug_object_is_on_stack(addr, onstack);
331 switch (obj->state) {
332 case ODEBUG_STATE_NONE:
333 case ODEBUG_STATE_INIT:
334 case ODEBUG_STATE_INACTIVE:
335 obj->state = ODEBUG_STATE_INIT;
336 break;
338 case ODEBUG_STATE_ACTIVE:
339 debug_print_object(obj, "init");
340 state = obj->state;
341 raw_spin_unlock_irqrestore(&db->lock, flags);
342 debug_object_fixup(descr->fixup_init, addr, state);
343 return;
345 case ODEBUG_STATE_DESTROYED:
346 debug_print_object(obj, "init");
347 break;
348 default:
349 break;
352 raw_spin_unlock_irqrestore(&db->lock, flags);
356 * debug_object_init - debug checks when an object is initialized
357 * @addr: address of the object
358 * @descr: pointer to an object specific debug description structure
360 void debug_object_init(void *addr, struct debug_obj_descr *descr)
362 if (!debug_objects_enabled)
363 return;
365 __debug_object_init(addr, descr, 0);
369 * debug_object_init_on_stack - debug checks when an object on stack is
370 * initialized
371 * @addr: address of the object
372 * @descr: pointer to an object specific debug description structure
374 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
376 if (!debug_objects_enabled)
377 return;
379 __debug_object_init(addr, descr, 1);
383 * debug_object_activate - debug checks when an object is activated
384 * @addr: address of the object
385 * @descr: pointer to an object specific debug description structure
387 void debug_object_activate(void *addr, struct debug_obj_descr *descr)
389 enum debug_obj_state state;
390 struct debug_bucket *db;
391 struct debug_obj *obj;
392 unsigned long flags;
393 struct debug_obj o = { .object = addr,
394 .state = ODEBUG_STATE_NOTAVAILABLE,
395 .descr = descr };
397 if (!debug_objects_enabled)
398 return;
400 db = get_bucket((unsigned long) addr);
402 raw_spin_lock_irqsave(&db->lock, flags);
404 obj = lookup_object(addr, db);
405 if (obj) {
406 switch (obj->state) {
407 case ODEBUG_STATE_INIT:
408 case ODEBUG_STATE_INACTIVE:
409 obj->state = ODEBUG_STATE_ACTIVE;
410 break;
412 case ODEBUG_STATE_ACTIVE:
413 debug_print_object(obj, "activate");
414 state = obj->state;
415 raw_spin_unlock_irqrestore(&db->lock, flags);
416 debug_object_fixup(descr->fixup_activate, addr, state);
417 return;
419 case ODEBUG_STATE_DESTROYED:
420 debug_print_object(obj, "activate");
421 break;
422 default:
423 break;
425 raw_spin_unlock_irqrestore(&db->lock, flags);
426 return;
429 raw_spin_unlock_irqrestore(&db->lock, flags);
431 * This happens when a static object is activated. We
432 * let the type specific code decide whether this is
433 * true or not.
435 if (debug_object_fixup(descr->fixup_activate, addr,
436 ODEBUG_STATE_NOTAVAILABLE))
437 debug_print_object(&o, "activate");
441 * debug_object_deactivate - debug checks when an object is deactivated
442 * @addr: address of the object
443 * @descr: pointer to an object specific debug description structure
445 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
447 struct debug_bucket *db;
448 struct debug_obj *obj;
449 unsigned long flags;
451 if (!debug_objects_enabled)
452 return;
454 db = get_bucket((unsigned long) addr);
456 raw_spin_lock_irqsave(&db->lock, flags);
458 obj = lookup_object(addr, db);
459 if (obj) {
460 switch (obj->state) {
461 case ODEBUG_STATE_INIT:
462 case ODEBUG_STATE_INACTIVE:
463 case ODEBUG_STATE_ACTIVE:
464 if (!obj->astate)
465 obj->state = ODEBUG_STATE_INACTIVE;
466 else
467 debug_print_object(obj, "deactivate");
468 break;
470 case ODEBUG_STATE_DESTROYED:
471 debug_print_object(obj, "deactivate");
472 break;
473 default:
474 break;
476 } else {
477 struct debug_obj o = { .object = addr,
478 .state = ODEBUG_STATE_NOTAVAILABLE,
479 .descr = descr };
481 debug_print_object(&o, "deactivate");
484 raw_spin_unlock_irqrestore(&db->lock, flags);
488 * debug_object_destroy - debug checks when an object is destroyed
489 * @addr: address of the object
490 * @descr: pointer to an object specific debug description structure
492 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
494 enum debug_obj_state state;
495 struct debug_bucket *db;
496 struct debug_obj *obj;
497 unsigned long flags;
499 if (!debug_objects_enabled)
500 return;
502 db = get_bucket((unsigned long) addr);
504 raw_spin_lock_irqsave(&db->lock, flags);
506 obj = lookup_object(addr, db);
507 if (!obj)
508 goto out_unlock;
510 switch (obj->state) {
511 case ODEBUG_STATE_NONE:
512 case ODEBUG_STATE_INIT:
513 case ODEBUG_STATE_INACTIVE:
514 obj->state = ODEBUG_STATE_DESTROYED;
515 break;
516 case ODEBUG_STATE_ACTIVE:
517 debug_print_object(obj, "destroy");
518 state = obj->state;
519 raw_spin_unlock_irqrestore(&db->lock, flags);
520 debug_object_fixup(descr->fixup_destroy, addr, state);
521 return;
523 case ODEBUG_STATE_DESTROYED:
524 debug_print_object(obj, "destroy");
525 break;
526 default:
527 break;
529 out_unlock:
530 raw_spin_unlock_irqrestore(&db->lock, flags);
534 * debug_object_free - debug checks when an object is freed
535 * @addr: address of the object
536 * @descr: pointer to an object specific debug description structure
538 void debug_object_free(void *addr, struct debug_obj_descr *descr)
540 enum debug_obj_state state;
541 struct debug_bucket *db;
542 struct debug_obj *obj;
543 unsigned long flags;
545 if (!debug_objects_enabled)
546 return;
548 db = get_bucket((unsigned long) addr);
550 raw_spin_lock_irqsave(&db->lock, flags);
552 obj = lookup_object(addr, db);
553 if (!obj)
554 goto out_unlock;
556 switch (obj->state) {
557 case ODEBUG_STATE_ACTIVE:
558 debug_print_object(obj, "free");
559 state = obj->state;
560 raw_spin_unlock_irqrestore(&db->lock, flags);
561 debug_object_fixup(descr->fixup_free, addr, state);
562 return;
563 default:
564 hlist_del(&obj->node);
565 raw_spin_unlock_irqrestore(&db->lock, flags);
566 free_object(obj);
567 return;
569 out_unlock:
570 raw_spin_unlock_irqrestore(&db->lock, flags);
574 * debug_object_assert_init - debug checks when object should be init-ed
575 * @addr: address of the object
576 * @descr: pointer to an object specific debug description structure
578 void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
580 struct debug_bucket *db;
581 struct debug_obj *obj;
582 unsigned long flags;
584 if (!debug_objects_enabled)
585 return;
587 db = get_bucket((unsigned long) addr);
589 raw_spin_lock_irqsave(&db->lock, flags);
591 obj = lookup_object(addr, db);
592 if (!obj) {
593 struct debug_obj o = { .object = addr,
594 .state = ODEBUG_STATE_NOTAVAILABLE,
595 .descr = descr };
597 raw_spin_unlock_irqrestore(&db->lock, flags);
599 * Maybe the object is static. Let the type specific
600 * code decide what to do.
602 if (debug_object_fixup(descr->fixup_assert_init, addr,
603 ODEBUG_STATE_NOTAVAILABLE))
604 debug_print_object(&o, "assert_init");
605 return;
608 raw_spin_unlock_irqrestore(&db->lock, flags);
612 * debug_object_active_state - debug checks object usage state machine
613 * @addr: address of the object
614 * @descr: pointer to an object specific debug description structure
615 * @expect: expected state
616 * @next: state to move to if expected state is found
618 void
619 debug_object_active_state(void *addr, struct debug_obj_descr *descr,
620 unsigned int expect, unsigned int next)
622 struct debug_bucket *db;
623 struct debug_obj *obj;
624 unsigned long flags;
626 if (!debug_objects_enabled)
627 return;
629 db = get_bucket((unsigned long) addr);
631 raw_spin_lock_irqsave(&db->lock, flags);
633 obj = lookup_object(addr, db);
634 if (obj) {
635 switch (obj->state) {
636 case ODEBUG_STATE_ACTIVE:
637 if (obj->astate == expect)
638 obj->astate = next;
639 else
640 debug_print_object(obj, "active_state");
641 break;
643 default:
644 debug_print_object(obj, "active_state");
645 break;
647 } else {
648 struct debug_obj o = { .object = addr,
649 .state = ODEBUG_STATE_NOTAVAILABLE,
650 .descr = descr };
652 debug_print_object(&o, "active_state");
655 raw_spin_unlock_irqrestore(&db->lock, flags);
658 #ifdef CONFIG_DEBUG_OBJECTS_FREE
659 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
661 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
662 struct hlist_node *node, *tmp;
663 HLIST_HEAD(freelist);
664 struct debug_obj_descr *descr;
665 enum debug_obj_state state;
666 struct debug_bucket *db;
667 struct debug_obj *obj;
668 int cnt;
670 saddr = (unsigned long) address;
671 eaddr = saddr + size;
672 paddr = saddr & ODEBUG_CHUNK_MASK;
673 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
674 chunks >>= ODEBUG_CHUNK_SHIFT;
676 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
677 db = get_bucket(paddr);
679 repeat:
680 cnt = 0;
681 raw_spin_lock_irqsave(&db->lock, flags);
682 hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
683 cnt++;
684 oaddr = (unsigned long) obj->object;
685 if (oaddr < saddr || oaddr >= eaddr)
686 continue;
688 switch (obj->state) {
689 case ODEBUG_STATE_ACTIVE:
690 debug_print_object(obj, "free");
691 descr = obj->descr;
692 state = obj->state;
693 raw_spin_unlock_irqrestore(&db->lock, flags);
694 debug_object_fixup(descr->fixup_free,
695 (void *) oaddr, state);
696 goto repeat;
697 default:
698 hlist_del(&obj->node);
699 hlist_add_head(&obj->node, &freelist);
700 break;
703 raw_spin_unlock_irqrestore(&db->lock, flags);
705 /* Now free them */
706 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
707 hlist_del(&obj->node);
708 free_object(obj);
711 if (cnt > debug_objects_maxchain)
712 debug_objects_maxchain = cnt;
716 void debug_check_no_obj_freed(const void *address, unsigned long size)
718 if (debug_objects_enabled)
719 __debug_check_no_obj_freed(address, size);
721 #endif
723 #ifdef CONFIG_DEBUG_FS
725 static int debug_stats_show(struct seq_file *m, void *v)
727 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
728 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
729 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
730 seq_printf(m, "pool_free :%d\n", obj_pool_free);
731 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
732 seq_printf(m, "pool_used :%d\n", obj_pool_used);
733 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
734 return 0;
737 static int debug_stats_open(struct inode *inode, struct file *filp)
739 return single_open(filp, debug_stats_show, NULL);
742 static const struct file_operations debug_stats_fops = {
743 .open = debug_stats_open,
744 .read = seq_read,
745 .llseek = seq_lseek,
746 .release = single_release,
749 static int __init debug_objects_init_debugfs(void)
751 struct dentry *dbgdir, *dbgstats;
753 if (!debug_objects_enabled)
754 return 0;
756 dbgdir = debugfs_create_dir("debug_objects", NULL);
757 if (!dbgdir)
758 return -ENOMEM;
760 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
761 &debug_stats_fops);
762 if (!dbgstats)
763 goto err;
765 return 0;
767 err:
768 debugfs_remove(dbgdir);
770 return -ENOMEM;
772 __initcall(debug_objects_init_debugfs);
774 #else
775 static inline void debug_objects_init_debugfs(void) { }
776 #endif
778 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
780 /* Random data structure for the self test */
781 struct self_test {
782 unsigned long dummy1[6];
783 int static_init;
784 unsigned long dummy2[3];
787 static __initdata struct debug_obj_descr descr_type_test;
790 * fixup_init is called when:
791 * - an active object is initialized
793 static int __init fixup_init(void *addr, enum debug_obj_state state)
795 struct self_test *obj = addr;
797 switch (state) {
798 case ODEBUG_STATE_ACTIVE:
799 debug_object_deactivate(obj, &descr_type_test);
800 debug_object_init(obj, &descr_type_test);
801 return 1;
802 default:
803 return 0;
808 * fixup_activate is called when:
809 * - an active object is activated
810 * - an unknown object is activated (might be a statically initialized object)
812 static int __init fixup_activate(void *addr, enum debug_obj_state state)
814 struct self_test *obj = addr;
816 switch (state) {
817 case ODEBUG_STATE_NOTAVAILABLE:
818 if (obj->static_init == 1) {
819 debug_object_init(obj, &descr_type_test);
820 debug_object_activate(obj, &descr_type_test);
822 * Real code should return 0 here ! This is
823 * not a fixup of some bad behaviour. We
824 * merily call the debug_init function to keep
825 * track of the object.
827 return 1;
828 } else {
829 /* Real code needs to emit a warning here */
831 return 0;
833 case ODEBUG_STATE_ACTIVE:
834 debug_object_deactivate(obj, &descr_type_test);
835 debug_object_activate(obj, &descr_type_test);
836 return 1;
838 default:
839 return 0;
844 * fixup_destroy is called when:
845 * - an active object is destroyed
847 static int __init fixup_destroy(void *addr, enum debug_obj_state state)
849 struct self_test *obj = addr;
851 switch (state) {
852 case ODEBUG_STATE_ACTIVE:
853 debug_object_deactivate(obj, &descr_type_test);
854 debug_object_destroy(obj, &descr_type_test);
855 return 1;
856 default:
857 return 0;
862 * fixup_free is called when:
863 * - an active object is freed
865 static int __init fixup_free(void *addr, enum debug_obj_state state)
867 struct self_test *obj = addr;
869 switch (state) {
870 case ODEBUG_STATE_ACTIVE:
871 debug_object_deactivate(obj, &descr_type_test);
872 debug_object_free(obj, &descr_type_test);
873 return 1;
874 default:
875 return 0;
879 static int __init
880 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
882 struct debug_bucket *db;
883 struct debug_obj *obj;
884 unsigned long flags;
885 int res = -EINVAL;
887 db = get_bucket((unsigned long) addr);
889 raw_spin_lock_irqsave(&db->lock, flags);
891 obj = lookup_object(addr, db);
892 if (!obj && state != ODEBUG_STATE_NONE) {
893 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
894 goto out;
896 if (obj && obj->state != state) {
897 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
898 obj->state, state);
899 goto out;
901 if (fixups != debug_objects_fixups) {
902 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
903 fixups, debug_objects_fixups);
904 goto out;
906 if (warnings != debug_objects_warnings) {
907 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
908 warnings, debug_objects_warnings);
909 goto out;
911 res = 0;
912 out:
913 raw_spin_unlock_irqrestore(&db->lock, flags);
914 if (res)
915 debug_objects_enabled = 0;
916 return res;
919 static __initdata struct debug_obj_descr descr_type_test = {
920 .name = "selftest",
921 .fixup_init = fixup_init,
922 .fixup_activate = fixup_activate,
923 .fixup_destroy = fixup_destroy,
924 .fixup_free = fixup_free,
927 static __initdata struct self_test obj = { .static_init = 0 };
929 static void __init debug_objects_selftest(void)
931 int fixups, oldfixups, warnings, oldwarnings;
932 unsigned long flags;
934 local_irq_save(flags);
936 fixups = oldfixups = debug_objects_fixups;
937 warnings = oldwarnings = debug_objects_warnings;
938 descr_test = &descr_type_test;
940 debug_object_init(&obj, &descr_type_test);
941 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
942 goto out;
943 debug_object_activate(&obj, &descr_type_test);
944 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
945 goto out;
946 debug_object_activate(&obj, &descr_type_test);
947 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
948 goto out;
949 debug_object_deactivate(&obj, &descr_type_test);
950 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
951 goto out;
952 debug_object_destroy(&obj, &descr_type_test);
953 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
954 goto out;
955 debug_object_init(&obj, &descr_type_test);
956 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
957 goto out;
958 debug_object_activate(&obj, &descr_type_test);
959 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
960 goto out;
961 debug_object_deactivate(&obj, &descr_type_test);
962 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
963 goto out;
964 debug_object_free(&obj, &descr_type_test);
965 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
966 goto out;
968 obj.static_init = 1;
969 debug_object_activate(&obj, &descr_type_test);
970 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
971 goto out;
972 debug_object_init(&obj, &descr_type_test);
973 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
974 goto out;
975 debug_object_free(&obj, &descr_type_test);
976 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
977 goto out;
979 #ifdef CONFIG_DEBUG_OBJECTS_FREE
980 debug_object_init(&obj, &descr_type_test);
981 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
982 goto out;
983 debug_object_activate(&obj, &descr_type_test);
984 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
985 goto out;
986 __debug_check_no_obj_freed(&obj, sizeof(obj));
987 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
988 goto out;
989 #endif
990 printk(KERN_INFO "ODEBUG: selftest passed\n");
992 out:
993 debug_objects_fixups = oldfixups;
994 debug_objects_warnings = oldwarnings;
995 descr_test = NULL;
997 local_irq_restore(flags);
999 #else
1000 static inline void debug_objects_selftest(void) { }
1001 #endif
1004 * Called during early boot to initialize the hash buckets and link
1005 * the static object pool objects into the poll list. After this call
1006 * the object tracker is fully operational.
1008 void __init debug_objects_early_init(void)
1010 int i;
1012 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1013 raw_spin_lock_init(&obj_hash[i].lock);
1015 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1016 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1020 * Convert the statically allocated objects to dynamic ones:
1022 static int __init debug_objects_replace_static_objects(void)
1024 struct debug_bucket *db = obj_hash;
1025 struct hlist_node *node, *tmp;
1026 struct debug_obj *obj, *new;
1027 HLIST_HEAD(objects);
1028 int i, cnt = 0;
1030 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1031 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1032 if (!obj)
1033 goto free;
1034 hlist_add_head(&obj->node, &objects);
1038 * When debug_objects_mem_init() is called we know that only
1039 * one CPU is up, so disabling interrupts is enough
1040 * protection. This avoids the lockdep hell of lock ordering.
1042 local_irq_disable();
1044 /* Remove the statically allocated objects from the pool */
1045 hlist_for_each_entry_safe(obj, node, tmp, &obj_pool, node)
1046 hlist_del(&obj->node);
1047 /* Move the allocated objects to the pool */
1048 hlist_move_list(&objects, &obj_pool);
1050 /* Replace the active object references */
1051 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1052 hlist_move_list(&db->list, &objects);
1054 hlist_for_each_entry(obj, node, &objects, node) {
1055 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1056 hlist_del(&new->node);
1057 /* copy object data */
1058 *new = *obj;
1059 hlist_add_head(&new->node, &db->list);
1060 cnt++;
1064 printk(KERN_DEBUG "ODEBUG: %d of %d active objects replaced\n", cnt,
1065 obj_pool_used);
1066 local_irq_enable();
1067 return 0;
1068 free:
1069 hlist_for_each_entry_safe(obj, node, tmp, &objects, node) {
1070 hlist_del(&obj->node);
1071 kmem_cache_free(obj_cache, obj);
1073 return -ENOMEM;
1077 * Called after the kmem_caches are functional to setup a dedicated
1078 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1079 * prevents that the debug code is called on kmem_cache_free() for the
1080 * debug tracker objects to avoid recursive calls.
1082 void __init debug_objects_mem_init(void)
1084 if (!debug_objects_enabled)
1085 return;
1087 obj_cache = kmem_cache_create("debug_objects_cache",
1088 sizeof (struct debug_obj), 0,
1089 SLAB_DEBUG_OBJECTS, NULL);
1091 if (!obj_cache || debug_objects_replace_static_objects()) {
1092 debug_objects_enabled = 0;
1093 if (obj_cache)
1094 kmem_cache_destroy(obj_cache);
1095 printk(KERN_WARNING "ODEBUG: out of memory.\n");
1096 } else
1097 debug_objects_selftest();