drm/ttm: Fix printk format & compute bo->mem.size at bo initialization
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / lib / debugobjects.c
blobeae56fddfa3bd6517653e9387de92c4e56875044
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/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))
27 struct debug_bucket {
28 struct hlist_head list;
29 spinlock_t lock;
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;
60 return 0;
63 static int __init disable_object_debug(char *str)
65 debug_objects_enabled = 0;
66 return 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;
85 unsigned long flags;
87 if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
88 return obj_pool_free;
90 if (unlikely(!obj_cache))
91 return obj_pool_free;
93 while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
95 new = kmem_cache_zalloc(obj_cache, gfp);
96 if (!new)
97 return obj_pool_free;
99 spin_lock_irqsave(&pool_lock, flags);
100 hlist_add_head(&new->node, &obj_pool);
101 obj_pool_free++;
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;
114 int cnt = 0;
116 hlist_for_each_entry(obj, node, &b->list, node) {
117 cnt++;
118 if (obj->object == addr)
119 return obj;
121 if (cnt > debug_objects_maxchain)
122 debug_objects_maxchain = cnt;
124 return NULL;
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);
140 obj->object = addr;
141 obj->descr = descr;
142 obj->state = ODEBUG_STATE_NONE;
143 hlist_del(&obj->node);
145 hlist_add_head(&obj->node, &b->list);
147 obj_pool_used++;
148 if (obj_pool_used > obj_pool_max_used)
149 obj_pool_max_used = obj_pool_used;
151 obj_pool_free--;
152 if (obj_pool_free < obj_pool_min_free)
153 obj_pool_min_free = obj_pool_free;
155 spin_unlock(&pool_lock);
157 return obj;
161 * workqueue function to free objects.
163 static void free_obj_work(struct work_struct *work)
165 struct debug_obj *obj;
166 unsigned long flags;
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);
172 obj_pool_free--;
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
186 * if necessary.
188 static void free_object(struct debug_obj *obj)
190 unsigned long flags;
191 int sched = 0;
193 spin_lock_irqsave(&pool_lock, flags);
195 * schedule work when the pool is filled and the cache is
196 * initialized:
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);
201 obj_pool_free++;
202 obj_pool_used--;
203 spin_unlock_irqrestore(&pool_lock, flags);
204 if (sched)
205 schedule_work(&debug_obj_work);
209 * We run out of memory. That means we probably have tons of objects
210 * allocated.
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;
218 unsigned long flags;
219 int i;
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);
228 /* Now free them */
229 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
230 hlist_del(&obj->node);
231 free_object(obj);
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)
242 unsigned long hash;
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)
250 static int limit;
252 if (limit < 5 && obj->descr != descr_test) {
253 limit++;
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
262 * debug output.
264 static void
265 debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state),
266 void * addr, enum debug_obj_state state)
268 if (fixup)
269 debug_objects_fixups += fixup(addr, state);
272 static void debug_object_is_on_stack(void *addr, int onstack)
274 int is_on_stack;
275 static int limit;
277 if (limit > 4)
278 return;
280 is_on_stack = object_is_on_stack(addr);
281 if (is_on_stack == onstack)
282 return;
284 limit++;
285 if (is_on_stack)
286 printk(KERN_WARNING
287 "ODEBUG: object is on stack, but not annotated\n");
288 else
289 printk(KERN_WARNING
290 "ODEBUG: object is not on stack, but annotated\n");
291 WARN_ON(1);
294 static void
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;
300 unsigned long flags;
302 fill_pool();
304 db = get_bucket((unsigned long) addr);
306 spin_lock_irqsave(&db->lock, flags);
308 obj = lookup_object(addr, db);
309 if (!obj) {
310 obj = alloc_object(addr, db, descr);
311 if (!obj) {
312 debug_objects_enabled = 0;
313 spin_unlock_irqrestore(&db->lock, flags);
314 debug_objects_oom();
315 return;
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;
325 break;
327 case ODEBUG_STATE_ACTIVE:
328 debug_print_object(obj, "init");
329 state = obj->state;
330 spin_unlock_irqrestore(&db->lock, flags);
331 debug_object_fixup(descr->fixup_init, addr, state);
332 return;
334 case ODEBUG_STATE_DESTROYED:
335 debug_print_object(obj, "init");
336 break;
337 default:
338 break;
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)
352 return;
354 __debug_object_init(addr, descr, 0);
358 * debug_object_init_on_stack - debug checks when an object on stack is
359 * initialized
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)
366 return;
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;
381 unsigned long flags;
383 if (!debug_objects_enabled)
384 return;
386 db = get_bucket((unsigned long) addr);
388 spin_lock_irqsave(&db->lock, flags);
390 obj = lookup_object(addr, db);
391 if (obj) {
392 switch (obj->state) {
393 case ODEBUG_STATE_INIT:
394 case ODEBUG_STATE_INACTIVE:
395 obj->state = ODEBUG_STATE_ACTIVE;
396 break;
398 case ODEBUG_STATE_ACTIVE:
399 debug_print_object(obj, "activate");
400 state = obj->state;
401 spin_unlock_irqrestore(&db->lock, flags);
402 debug_object_fixup(descr->fixup_activate, addr, state);
403 return;
405 case ODEBUG_STATE_DESTROYED:
406 debug_print_object(obj, "activate");
407 break;
408 default:
409 break;
411 spin_unlock_irqrestore(&db->lock, flags);
412 return;
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
419 * true or not.
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;
434 unsigned long flags;
436 if (!debug_objects_enabled)
437 return;
439 db = get_bucket((unsigned long) addr);
441 spin_lock_irqsave(&db->lock, flags);
443 obj = lookup_object(addr, db);
444 if (obj) {
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;
450 break;
452 case ODEBUG_STATE_DESTROYED:
453 debug_print_object(obj, "deactivate");
454 break;
455 default:
456 break;
458 } else {
459 struct debug_obj o = { .object = addr,
460 .state = ODEBUG_STATE_NOTAVAILABLE,
461 .descr = descr };
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;
479 unsigned long flags;
481 if (!debug_objects_enabled)
482 return;
484 db = get_bucket((unsigned long) addr);
486 spin_lock_irqsave(&db->lock, flags);
488 obj = lookup_object(addr, db);
489 if (!obj)
490 goto out_unlock;
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;
497 break;
498 case ODEBUG_STATE_ACTIVE:
499 debug_print_object(obj, "destroy");
500 state = obj->state;
501 spin_unlock_irqrestore(&db->lock, flags);
502 debug_object_fixup(descr->fixup_destroy, addr, state);
503 return;
505 case ODEBUG_STATE_DESTROYED:
506 debug_print_object(obj, "destroy");
507 break;
508 default:
509 break;
511 out_unlock:
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;
525 unsigned long flags;
527 if (!debug_objects_enabled)
528 return;
530 db = get_bucket((unsigned long) addr);
532 spin_lock_irqsave(&db->lock, flags);
534 obj = lookup_object(addr, db);
535 if (!obj)
536 goto out_unlock;
538 switch (obj->state) {
539 case ODEBUG_STATE_ACTIVE:
540 debug_print_object(obj, "free");
541 state = obj->state;
542 spin_unlock_irqrestore(&db->lock, flags);
543 debug_object_fixup(descr->fixup_free, addr, state);
544 return;
545 default:
546 hlist_del(&obj->node);
547 spin_unlock_irqrestore(&db->lock, flags);
548 free_object(obj);
549 return;
551 out_unlock:
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;
565 int cnt;
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);
576 repeat:
577 cnt = 0;
578 spin_lock_irqsave(&db->lock, flags);
579 hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
580 cnt++;
581 oaddr = (unsigned long) obj->object;
582 if (oaddr < saddr || oaddr >= eaddr)
583 continue;
585 switch (obj->state) {
586 case ODEBUG_STATE_ACTIVE:
587 debug_print_object(obj, "free");
588 descr = obj->descr;
589 state = obj->state;
590 spin_unlock_irqrestore(&db->lock, flags);
591 debug_object_fixup(descr->fixup_free,
592 (void *) oaddr, state);
593 goto repeat;
594 default:
595 hlist_del(&obj->node);
596 hlist_add_head(&obj->node, &freelist);
597 break;
600 spin_unlock_irqrestore(&db->lock, flags);
602 /* Now free them */
603 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
604 hlist_del(&obj->node);
605 free_object(obj);
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);
618 #endif
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);
631 return 0;
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,
641 .read = seq_read,
642 .llseek = seq_lseek,
643 .release = single_release,
646 static int __init debug_objects_init_debugfs(void)
648 struct dentry *dbgdir, *dbgstats;
650 if (!debug_objects_enabled)
651 return 0;
653 dbgdir = debugfs_create_dir("debug_objects", NULL);
654 if (!dbgdir)
655 return -ENOMEM;
657 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
658 &debug_stats_fops);
659 if (!dbgstats)
660 goto err;
662 return 0;
664 err:
665 debugfs_remove(dbgdir);
667 return -ENOMEM;
669 __initcall(debug_objects_init_debugfs);
671 #else
672 static inline void debug_objects_init_debugfs(void) { }
673 #endif
675 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
677 /* Random data structure for the self test */
678 struct self_test {
679 unsigned long dummy1[6];
680 int static_init;
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;
694 switch (state) {
695 case ODEBUG_STATE_ACTIVE:
696 debug_object_deactivate(obj, &descr_type_test);
697 debug_object_init(obj, &descr_type_test);
698 return 1;
699 default:
700 return 0;
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;
713 switch (state) {
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.
724 return 1;
725 } else {
726 /* Real code needs to emit a warning here */
728 return 0;
730 case ODEBUG_STATE_ACTIVE:
731 debug_object_deactivate(obj, &descr_type_test);
732 debug_object_activate(obj, &descr_type_test);
733 return 1;
735 default:
736 return 0;
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;
748 switch (state) {
749 case ODEBUG_STATE_ACTIVE:
750 debug_object_deactivate(obj, &descr_type_test);
751 debug_object_destroy(obj, &descr_type_test);
752 return 1;
753 default:
754 return 0;
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;
766 switch (state) {
767 case ODEBUG_STATE_ACTIVE:
768 debug_object_deactivate(obj, &descr_type_test);
769 debug_object_free(obj, &descr_type_test);
770 return 1;
771 default:
772 return 0;
776 static int
777 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
779 struct debug_bucket *db;
780 struct debug_obj *obj;
781 unsigned long flags;
782 int res = -EINVAL;
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");
791 goto out;
793 if (obj && obj->state != state) {
794 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
795 obj->state, state);
796 goto out;
798 if (fixups != debug_objects_fixups) {
799 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
800 fixups, debug_objects_fixups);
801 goto out;
803 if (warnings != debug_objects_warnings) {
804 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
805 warnings, debug_objects_warnings);
806 goto out;
808 res = 0;
809 out:
810 spin_unlock_irqrestore(&db->lock, flags);
811 if (res)
812 debug_objects_enabled = 0;
813 return res;
816 static __initdata struct debug_obj_descr descr_type_test = {
817 .name = "selftest",
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;
829 unsigned long flags;
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))
839 goto out;
840 debug_object_activate(&obj, &descr_type_test);
841 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
842 goto out;
843 debug_object_activate(&obj, &descr_type_test);
844 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
845 goto out;
846 debug_object_deactivate(&obj, &descr_type_test);
847 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
848 goto out;
849 debug_object_destroy(&obj, &descr_type_test);
850 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
851 goto out;
852 debug_object_init(&obj, &descr_type_test);
853 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
854 goto out;
855 debug_object_activate(&obj, &descr_type_test);
856 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
857 goto out;
858 debug_object_deactivate(&obj, &descr_type_test);
859 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
860 goto out;
861 debug_object_free(&obj, &descr_type_test);
862 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
863 goto out;
865 obj.static_init = 1;
866 debug_object_activate(&obj, &descr_type_test);
867 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
868 goto out;
869 debug_object_init(&obj, &descr_type_test);
870 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
871 goto out;
872 debug_object_free(&obj, &descr_type_test);
873 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
874 goto out;
876 #ifdef CONFIG_DEBUG_OBJECTS_FREE
877 debug_object_init(&obj, &descr_type_test);
878 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
879 goto out;
880 debug_object_activate(&obj, &descr_type_test);
881 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
882 goto out;
883 __debug_check_no_obj_freed(&obj, sizeof(obj));
884 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
885 goto out;
886 #endif
887 printk(KERN_INFO "ODEBUG: selftest passed\n");
889 out:
890 debug_objects_fixups = oldfixups;
891 debug_objects_warnings = oldwarnings;
892 descr_test = NULL;
894 local_irq_restore(flags);
896 #else
897 static inline void debug_objects_selftest(void) { }
898 #endif
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)
907 int i;
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;
924 HLIST_HEAD(objects);
925 int i, cnt = 0;
927 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
928 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
929 if (!obj)
930 goto free;
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.
939 local_irq_disable();
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 */
955 *new = *obj;
956 hlist_add_head(&new->node, &db->list);
957 cnt++;
961 printk(KERN_DEBUG "ODEBUG: %d of %d active objects replaced\n", cnt,
962 obj_pool_used);
963 local_irq_enable();
964 return 0;
965 free:
966 hlist_for_each_entry_safe(obj, node, tmp, &objects, node) {
967 hlist_del(&obj->node);
968 kmem_cache_free(obj_cache, obj);
970 return -ENOMEM;
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)
982 return;
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;
990 if (obj_cache)
991 kmem_cache_destroy(obj_cache);
992 printk(KERN_WARNING "ODEBUG: out of memory.\n");
993 } else
994 debug_objects_selftest();