debug_objects: add boot-parameter toggle to turn object debugging off again
[deliverable/linux.git] / lib / debugobjects.c
1 /*
2 * Generic infrastructure for lifetime debugging of objects.
3 *
4 * Started by Thomas Gleixner
5 *
6 * Copyright (C) 2008, Thomas Gleixner <tglx@linutronix.de>
7 *
8 * For licencing details see kernel-base/COPYING
9 */
10 #include <linux/debugobjects.h>
11 #include <linux/interrupt.h>
12 #include <linux/seq_file.h>
13 #include <linux/debugfs.h>
14 #include <linux/hash.h>
15
16 #define ODEBUG_HASH_BITS 14
17 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
18
19 #define ODEBUG_POOL_SIZE 512
20 #define ODEBUG_POOL_MIN_LEVEL 256
21
22 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
23 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
24 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
25
26 struct debug_bucket {
27 struct hlist_head list;
28 spinlock_t lock;
29 };
30
31 static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
32
33 static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE];
34
35 static DEFINE_SPINLOCK(pool_lock);
36
37 static HLIST_HEAD(obj_pool);
38
39 static int obj_pool_min_free = ODEBUG_POOL_SIZE;
40 static int obj_pool_free = ODEBUG_POOL_SIZE;
41 static int obj_pool_used;
42 static int obj_pool_max_used;
43 static struct kmem_cache *obj_cache;
44
45 static int debug_objects_maxchain __read_mostly;
46 static int debug_objects_fixups __read_mostly;
47 static int debug_objects_warnings __read_mostly;
48 static int debug_objects_enabled __read_mostly
49 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
50
51 static struct debug_obj_descr *descr_test __read_mostly;
52
53 static int __init enable_object_debug(char *str)
54 {
55 debug_objects_enabled = 1;
56 return 0;
57 }
58
59 static int __init disable_object_debug(char *str)
60 {
61 debug_objects_enabled = 0;
62 return 0;
63 }
64
65 early_param("debug_objects", enable_object_debug);
66 early_param("no_debug_objects", disable_object_debug);
67
68 static const char *obj_states[ODEBUG_STATE_MAX] = {
69 [ODEBUG_STATE_NONE] = "none",
70 [ODEBUG_STATE_INIT] = "initialized",
71 [ODEBUG_STATE_INACTIVE] = "inactive",
72 [ODEBUG_STATE_ACTIVE] = "active",
73 [ODEBUG_STATE_DESTROYED] = "destroyed",
74 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
75 };
76
77 static int fill_pool(void)
78 {
79 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
80 struct debug_obj *new;
81 unsigned long flags;
82
83 if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
84 return obj_pool_free;
85
86 if (unlikely(!obj_cache))
87 return obj_pool_free;
88
89 while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
90
91 new = kmem_cache_zalloc(obj_cache, gfp);
92 if (!new)
93 return obj_pool_free;
94
95 spin_lock_irqsave(&pool_lock, flags);
96 hlist_add_head(&new->node, &obj_pool);
97 obj_pool_free++;
98 spin_unlock_irqrestore(&pool_lock, flags);
99 }
100 return obj_pool_free;
101 }
102
103 /*
104 * Lookup an object in the hash bucket.
105 */
106 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
107 {
108 struct hlist_node *node;
109 struct debug_obj *obj;
110 int cnt = 0;
111
112 hlist_for_each_entry(obj, node, &b->list, node) {
113 cnt++;
114 if (obj->object == addr)
115 return obj;
116 }
117 if (cnt > debug_objects_maxchain)
118 debug_objects_maxchain = cnt;
119
120 return NULL;
121 }
122
123 /*
124 * Allocate a new object. If the pool is empty, switch off the debugger.
125 * Must be called with interrupts disabled.
126 */
127 static struct debug_obj *
128 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
129 {
130 struct debug_obj *obj = NULL;
131
132 spin_lock(&pool_lock);
133 if (obj_pool.first) {
134 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
135
136 obj->object = addr;
137 obj->descr = descr;
138 obj->state = ODEBUG_STATE_NONE;
139 hlist_del(&obj->node);
140
141 hlist_add_head(&obj->node, &b->list);
142
143 obj_pool_used++;
144 if (obj_pool_used > obj_pool_max_used)
145 obj_pool_max_used = obj_pool_used;
146
147 obj_pool_free--;
148 if (obj_pool_free < obj_pool_min_free)
149 obj_pool_min_free = obj_pool_free;
150 }
151 spin_unlock(&pool_lock);
152
153 return obj;
154 }
155
156 /*
157 * Put the object back into the pool or give it back to kmem_cache:
158 */
159 static void free_object(struct debug_obj *obj)
160 {
161 unsigned long idx = (unsigned long)(obj - obj_static_pool);
162 unsigned long flags;
163
164 if (obj_pool_free < ODEBUG_POOL_SIZE || idx < ODEBUG_POOL_SIZE) {
165 spin_lock_irqsave(&pool_lock, flags);
166 hlist_add_head(&obj->node, &obj_pool);
167 obj_pool_free++;
168 obj_pool_used--;
169 spin_unlock_irqrestore(&pool_lock, flags);
170 } else {
171 spin_lock_irqsave(&pool_lock, flags);
172 obj_pool_used--;
173 spin_unlock_irqrestore(&pool_lock, flags);
174 kmem_cache_free(obj_cache, obj);
175 }
176 }
177
178 /*
179 * We run out of memory. That means we probably have tons of objects
180 * allocated.
181 */
182 static void debug_objects_oom(void)
183 {
184 struct debug_bucket *db = obj_hash;
185 struct hlist_node *node, *tmp;
186 HLIST_HEAD(freelist);
187 struct debug_obj *obj;
188 unsigned long flags;
189 int i;
190
191 printk(KERN_WARNING "ODEBUG: Out of memory. ODEBUG disabled\n");
192
193 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
194 spin_lock_irqsave(&db->lock, flags);
195 hlist_move_list(&db->list, &freelist);
196 spin_unlock_irqrestore(&db->lock, flags);
197
198 /* Now free them */
199 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
200 hlist_del(&obj->node);
201 free_object(obj);
202 }
203 }
204 }
205
206 /*
207 * We use the pfn of the address for the hash. That way we can check
208 * for freed objects simply by checking the affected bucket.
209 */
210 static struct debug_bucket *get_bucket(unsigned long addr)
211 {
212 unsigned long hash;
213
214 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
215 return &obj_hash[hash];
216 }
217
218 static void debug_print_object(struct debug_obj *obj, char *msg)
219 {
220 static int limit;
221
222 if (limit < 5 && obj->descr != descr_test) {
223 limit++;
224 WARN(1, KERN_ERR "ODEBUG: %s %s object type: %s\n", msg,
225 obj_states[obj->state], obj->descr->name);
226 }
227 debug_objects_warnings++;
228 }
229
230 /*
231 * Try to repair the damage, so we have a better chance to get useful
232 * debug output.
233 */
234 static void
235 debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state),
236 void * addr, enum debug_obj_state state)
237 {
238 if (fixup)
239 debug_objects_fixups += fixup(addr, state);
240 }
241
242 static void debug_object_is_on_stack(void *addr, int onstack)
243 {
244 int is_on_stack;
245 static int limit;
246
247 if (limit > 4)
248 return;
249
250 is_on_stack = object_is_on_stack(addr);
251 if (is_on_stack == onstack)
252 return;
253
254 limit++;
255 if (is_on_stack)
256 printk(KERN_WARNING
257 "ODEBUG: object is on stack, but not annotated\n");
258 else
259 printk(KERN_WARNING
260 "ODEBUG: object is not on stack, but annotated\n");
261 WARN_ON(1);
262 }
263
264 static void
265 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
266 {
267 enum debug_obj_state state;
268 struct debug_bucket *db;
269 struct debug_obj *obj;
270 unsigned long flags;
271
272 fill_pool();
273
274 db = get_bucket((unsigned long) addr);
275
276 spin_lock_irqsave(&db->lock, flags);
277
278 obj = lookup_object(addr, db);
279 if (!obj) {
280 obj = alloc_object(addr, db, descr);
281 if (!obj) {
282 debug_objects_enabled = 0;
283 spin_unlock_irqrestore(&db->lock, flags);
284 debug_objects_oom();
285 return;
286 }
287 debug_object_is_on_stack(addr, onstack);
288 }
289
290 switch (obj->state) {
291 case ODEBUG_STATE_NONE:
292 case ODEBUG_STATE_INIT:
293 case ODEBUG_STATE_INACTIVE:
294 obj->state = ODEBUG_STATE_INIT;
295 break;
296
297 case ODEBUG_STATE_ACTIVE:
298 debug_print_object(obj, "init");
299 state = obj->state;
300 spin_unlock_irqrestore(&db->lock, flags);
301 debug_object_fixup(descr->fixup_init, addr, state);
302 return;
303
304 case ODEBUG_STATE_DESTROYED:
305 debug_print_object(obj, "init");
306 break;
307 default:
308 break;
309 }
310
311 spin_unlock_irqrestore(&db->lock, flags);
312 }
313
314 /**
315 * debug_object_init - debug checks when an object is initialized
316 * @addr: address of the object
317 * @descr: pointer to an object specific debug description structure
318 */
319 void debug_object_init(void *addr, struct debug_obj_descr *descr)
320 {
321 if (!debug_objects_enabled)
322 return;
323
324 __debug_object_init(addr, descr, 0);
325 }
326
327 /**
328 * debug_object_init_on_stack - debug checks when an object on stack is
329 * initialized
330 * @addr: address of the object
331 * @descr: pointer to an object specific debug description structure
332 */
333 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
334 {
335 if (!debug_objects_enabled)
336 return;
337
338 __debug_object_init(addr, descr, 1);
339 }
340
341 /**
342 * debug_object_activate - debug checks when an object is activated
343 * @addr: address of the object
344 * @descr: pointer to an object specific debug description structure
345 */
346 void debug_object_activate(void *addr, struct debug_obj_descr *descr)
347 {
348 enum debug_obj_state state;
349 struct debug_bucket *db;
350 struct debug_obj *obj;
351 unsigned long flags;
352
353 if (!debug_objects_enabled)
354 return;
355
356 db = get_bucket((unsigned long) addr);
357
358 spin_lock_irqsave(&db->lock, flags);
359
360 obj = lookup_object(addr, db);
361 if (obj) {
362 switch (obj->state) {
363 case ODEBUG_STATE_INIT:
364 case ODEBUG_STATE_INACTIVE:
365 obj->state = ODEBUG_STATE_ACTIVE;
366 break;
367
368 case ODEBUG_STATE_ACTIVE:
369 debug_print_object(obj, "activate");
370 state = obj->state;
371 spin_unlock_irqrestore(&db->lock, flags);
372 debug_object_fixup(descr->fixup_activate, addr, state);
373 return;
374
375 case ODEBUG_STATE_DESTROYED:
376 debug_print_object(obj, "activate");
377 break;
378 default:
379 break;
380 }
381 spin_unlock_irqrestore(&db->lock, flags);
382 return;
383 }
384
385 spin_unlock_irqrestore(&db->lock, flags);
386 /*
387 * This happens when a static object is activated. We
388 * let the type specific code decide whether this is
389 * true or not.
390 */
391 debug_object_fixup(descr->fixup_activate, addr,
392 ODEBUG_STATE_NOTAVAILABLE);
393 }
394
395 /**
396 * debug_object_deactivate - debug checks when an object is deactivated
397 * @addr: address of the object
398 * @descr: pointer to an object specific debug description structure
399 */
400 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
401 {
402 struct debug_bucket *db;
403 struct debug_obj *obj;
404 unsigned long flags;
405
406 if (!debug_objects_enabled)
407 return;
408
409 db = get_bucket((unsigned long) addr);
410
411 spin_lock_irqsave(&db->lock, flags);
412
413 obj = lookup_object(addr, db);
414 if (obj) {
415 switch (obj->state) {
416 case ODEBUG_STATE_INIT:
417 case ODEBUG_STATE_INACTIVE:
418 case ODEBUG_STATE_ACTIVE:
419 obj->state = ODEBUG_STATE_INACTIVE;
420 break;
421
422 case ODEBUG_STATE_DESTROYED:
423 debug_print_object(obj, "deactivate");
424 break;
425 default:
426 break;
427 }
428 } else {
429 struct debug_obj o = { .object = addr,
430 .state = ODEBUG_STATE_NOTAVAILABLE,
431 .descr = descr };
432
433 debug_print_object(&o, "deactivate");
434 }
435
436 spin_unlock_irqrestore(&db->lock, flags);
437 }
438
439 /**
440 * debug_object_destroy - debug checks when an object is destroyed
441 * @addr: address of the object
442 * @descr: pointer to an object specific debug description structure
443 */
444 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
445 {
446 enum debug_obj_state state;
447 struct debug_bucket *db;
448 struct debug_obj *obj;
449 unsigned long flags;
450
451 if (!debug_objects_enabled)
452 return;
453
454 db = get_bucket((unsigned long) addr);
455
456 spin_lock_irqsave(&db->lock, flags);
457
458 obj = lookup_object(addr, db);
459 if (!obj)
460 goto out_unlock;
461
462 switch (obj->state) {
463 case ODEBUG_STATE_NONE:
464 case ODEBUG_STATE_INIT:
465 case ODEBUG_STATE_INACTIVE:
466 obj->state = ODEBUG_STATE_DESTROYED;
467 break;
468 case ODEBUG_STATE_ACTIVE:
469 debug_print_object(obj, "destroy");
470 state = obj->state;
471 spin_unlock_irqrestore(&db->lock, flags);
472 debug_object_fixup(descr->fixup_destroy, addr, state);
473 return;
474
475 case ODEBUG_STATE_DESTROYED:
476 debug_print_object(obj, "destroy");
477 break;
478 default:
479 break;
480 }
481 out_unlock:
482 spin_unlock_irqrestore(&db->lock, flags);
483 }
484
485 /**
486 * debug_object_free - debug checks when an object is freed
487 * @addr: address of the object
488 * @descr: pointer to an object specific debug description structure
489 */
490 void debug_object_free(void *addr, struct debug_obj_descr *descr)
491 {
492 enum debug_obj_state state;
493 struct debug_bucket *db;
494 struct debug_obj *obj;
495 unsigned long flags;
496
497 if (!debug_objects_enabled)
498 return;
499
500 db = get_bucket((unsigned long) addr);
501
502 spin_lock_irqsave(&db->lock, flags);
503
504 obj = lookup_object(addr, db);
505 if (!obj)
506 goto out_unlock;
507
508 switch (obj->state) {
509 case ODEBUG_STATE_ACTIVE:
510 debug_print_object(obj, "free");
511 state = obj->state;
512 spin_unlock_irqrestore(&db->lock, flags);
513 debug_object_fixup(descr->fixup_free, addr, state);
514 return;
515 default:
516 hlist_del(&obj->node);
517 spin_unlock_irqrestore(&db->lock, flags);
518 free_object(obj);
519 return;
520 }
521 out_unlock:
522 spin_unlock_irqrestore(&db->lock, flags);
523 }
524
525 #ifdef CONFIG_DEBUG_OBJECTS_FREE
526 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
527 {
528 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
529 struct hlist_node *node, *tmp;
530 HLIST_HEAD(freelist);
531 struct debug_obj_descr *descr;
532 enum debug_obj_state state;
533 struct debug_bucket *db;
534 struct debug_obj *obj;
535 int cnt;
536
537 saddr = (unsigned long) address;
538 eaddr = saddr + size;
539 paddr = saddr & ODEBUG_CHUNK_MASK;
540 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
541 chunks >>= ODEBUG_CHUNK_SHIFT;
542
543 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
544 db = get_bucket(paddr);
545
546 repeat:
547 cnt = 0;
548 spin_lock_irqsave(&db->lock, flags);
549 hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
550 cnt++;
551 oaddr = (unsigned long) obj->object;
552 if (oaddr < saddr || oaddr >= eaddr)
553 continue;
554
555 switch (obj->state) {
556 case ODEBUG_STATE_ACTIVE:
557 debug_print_object(obj, "free");
558 descr = obj->descr;
559 state = obj->state;
560 spin_unlock_irqrestore(&db->lock, flags);
561 debug_object_fixup(descr->fixup_free,
562 (void *) oaddr, state);
563 goto repeat;
564 default:
565 hlist_del(&obj->node);
566 hlist_add_head(&obj->node, &freelist);
567 break;
568 }
569 }
570 spin_unlock_irqrestore(&db->lock, flags);
571
572 /* Now free them */
573 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
574 hlist_del(&obj->node);
575 free_object(obj);
576 }
577
578 if (cnt > debug_objects_maxchain)
579 debug_objects_maxchain = cnt;
580 }
581 }
582
583 void debug_check_no_obj_freed(const void *address, unsigned long size)
584 {
585 if (debug_objects_enabled)
586 __debug_check_no_obj_freed(address, size);
587 }
588 #endif
589
590 #ifdef CONFIG_DEBUG_FS
591
592 static int debug_stats_show(struct seq_file *m, void *v)
593 {
594 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
595 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
596 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
597 seq_printf(m, "pool_free :%d\n", obj_pool_free);
598 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
599 seq_printf(m, "pool_used :%d\n", obj_pool_used);
600 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
601 return 0;
602 }
603
604 static int debug_stats_open(struct inode *inode, struct file *filp)
605 {
606 return single_open(filp, debug_stats_show, NULL);
607 }
608
609 static const struct file_operations debug_stats_fops = {
610 .open = debug_stats_open,
611 .read = seq_read,
612 .llseek = seq_lseek,
613 .release = single_release,
614 };
615
616 static int __init debug_objects_init_debugfs(void)
617 {
618 struct dentry *dbgdir, *dbgstats;
619
620 if (!debug_objects_enabled)
621 return 0;
622
623 dbgdir = debugfs_create_dir("debug_objects", NULL);
624 if (!dbgdir)
625 return -ENOMEM;
626
627 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
628 &debug_stats_fops);
629 if (!dbgstats)
630 goto err;
631
632 return 0;
633
634 err:
635 debugfs_remove(dbgdir);
636
637 return -ENOMEM;
638 }
639 __initcall(debug_objects_init_debugfs);
640
641 #else
642 static inline void debug_objects_init_debugfs(void) { }
643 #endif
644
645 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
646
647 /* Random data structure for the self test */
648 struct self_test {
649 unsigned long dummy1[6];
650 int static_init;
651 unsigned long dummy2[3];
652 };
653
654 static __initdata struct debug_obj_descr descr_type_test;
655
656 /*
657 * fixup_init is called when:
658 * - an active object is initialized
659 */
660 static int __init fixup_init(void *addr, enum debug_obj_state state)
661 {
662 struct self_test *obj = addr;
663
664 switch (state) {
665 case ODEBUG_STATE_ACTIVE:
666 debug_object_deactivate(obj, &descr_type_test);
667 debug_object_init(obj, &descr_type_test);
668 return 1;
669 default:
670 return 0;
671 }
672 }
673
674 /*
675 * fixup_activate is called when:
676 * - an active object is activated
677 * - an unknown object is activated (might be a statically initialized object)
678 */
679 static int __init fixup_activate(void *addr, enum debug_obj_state state)
680 {
681 struct self_test *obj = addr;
682
683 switch (state) {
684 case ODEBUG_STATE_NOTAVAILABLE:
685 if (obj->static_init == 1) {
686 debug_object_init(obj, &descr_type_test);
687 debug_object_activate(obj, &descr_type_test);
688 /*
689 * Real code should return 0 here ! This is
690 * not a fixup of some bad behaviour. We
691 * merily call the debug_init function to keep
692 * track of the object.
693 */
694 return 1;
695 } else {
696 /* Real code needs to emit a warning here */
697 }
698 return 0;
699
700 case ODEBUG_STATE_ACTIVE:
701 debug_object_deactivate(obj, &descr_type_test);
702 debug_object_activate(obj, &descr_type_test);
703 return 1;
704
705 default:
706 return 0;
707 }
708 }
709
710 /*
711 * fixup_destroy is called when:
712 * - an active object is destroyed
713 */
714 static int __init fixup_destroy(void *addr, enum debug_obj_state state)
715 {
716 struct self_test *obj = addr;
717
718 switch (state) {
719 case ODEBUG_STATE_ACTIVE:
720 debug_object_deactivate(obj, &descr_type_test);
721 debug_object_destroy(obj, &descr_type_test);
722 return 1;
723 default:
724 return 0;
725 }
726 }
727
728 /*
729 * fixup_free is called when:
730 * - an active object is freed
731 */
732 static int __init fixup_free(void *addr, enum debug_obj_state state)
733 {
734 struct self_test *obj = addr;
735
736 switch (state) {
737 case ODEBUG_STATE_ACTIVE:
738 debug_object_deactivate(obj, &descr_type_test);
739 debug_object_free(obj, &descr_type_test);
740 return 1;
741 default:
742 return 0;
743 }
744 }
745
746 static int
747 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
748 {
749 struct debug_bucket *db;
750 struct debug_obj *obj;
751 unsigned long flags;
752 int res = -EINVAL;
753
754 db = get_bucket((unsigned long) addr);
755
756 spin_lock_irqsave(&db->lock, flags);
757
758 obj = lookup_object(addr, db);
759 if (!obj && state != ODEBUG_STATE_NONE) {
760 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
761 goto out;
762 }
763 if (obj && obj->state != state) {
764 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
765 obj->state, state);
766 goto out;
767 }
768 if (fixups != debug_objects_fixups) {
769 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
770 fixups, debug_objects_fixups);
771 goto out;
772 }
773 if (warnings != debug_objects_warnings) {
774 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
775 warnings, debug_objects_warnings);
776 goto out;
777 }
778 res = 0;
779 out:
780 spin_unlock_irqrestore(&db->lock, flags);
781 if (res)
782 debug_objects_enabled = 0;
783 return res;
784 }
785
786 static __initdata struct debug_obj_descr descr_type_test = {
787 .name = "selftest",
788 .fixup_init = fixup_init,
789 .fixup_activate = fixup_activate,
790 .fixup_destroy = fixup_destroy,
791 .fixup_free = fixup_free,
792 };
793
794 static __initdata struct self_test obj = { .static_init = 0 };
795
796 static void __init debug_objects_selftest(void)
797 {
798 int fixups, oldfixups, warnings, oldwarnings;
799 unsigned long flags;
800
801 local_irq_save(flags);
802
803 fixups = oldfixups = debug_objects_fixups;
804 warnings = oldwarnings = debug_objects_warnings;
805 descr_test = &descr_type_test;
806
807 debug_object_init(&obj, &descr_type_test);
808 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
809 goto out;
810 debug_object_activate(&obj, &descr_type_test);
811 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
812 goto out;
813 debug_object_activate(&obj, &descr_type_test);
814 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
815 goto out;
816 debug_object_deactivate(&obj, &descr_type_test);
817 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
818 goto out;
819 debug_object_destroy(&obj, &descr_type_test);
820 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
821 goto out;
822 debug_object_init(&obj, &descr_type_test);
823 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
824 goto out;
825 debug_object_activate(&obj, &descr_type_test);
826 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
827 goto out;
828 debug_object_deactivate(&obj, &descr_type_test);
829 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
830 goto out;
831 debug_object_free(&obj, &descr_type_test);
832 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
833 goto out;
834
835 obj.static_init = 1;
836 debug_object_activate(&obj, &descr_type_test);
837 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
838 goto out;
839 debug_object_init(&obj, &descr_type_test);
840 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
841 goto out;
842 debug_object_free(&obj, &descr_type_test);
843 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
844 goto out;
845
846 #ifdef CONFIG_DEBUG_OBJECTS_FREE
847 debug_object_init(&obj, &descr_type_test);
848 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
849 goto out;
850 debug_object_activate(&obj, &descr_type_test);
851 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
852 goto out;
853 __debug_check_no_obj_freed(&obj, sizeof(obj));
854 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
855 goto out;
856 #endif
857 printk(KERN_INFO "ODEBUG: selftest passed\n");
858
859 out:
860 debug_objects_fixups = oldfixups;
861 debug_objects_warnings = oldwarnings;
862 descr_test = NULL;
863
864 local_irq_restore(flags);
865 }
866 #else
867 static inline void debug_objects_selftest(void) { }
868 #endif
869
870 /*
871 * Called during early boot to initialize the hash buckets and link
872 * the static object pool objects into the poll list. After this call
873 * the object tracker is fully operational.
874 */
875 void __init debug_objects_early_init(void)
876 {
877 int i;
878
879 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
880 spin_lock_init(&obj_hash[i].lock);
881
882 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
883 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
884 }
885
886 /*
887 * Called after the kmem_caches are functional to setup a dedicated
888 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
889 * prevents that the debug code is called on kmem_cache_free() for the
890 * debug tracker objects to avoid recursive calls.
891 */
892 void __init debug_objects_mem_init(void)
893 {
894 if (!debug_objects_enabled)
895 return;
896
897 obj_cache = kmem_cache_create("debug_objects_cache",
898 sizeof (struct debug_obj), 0,
899 SLAB_DEBUG_OBJECTS, NULL);
900
901 if (!obj_cache)
902 debug_objects_enabled = 0;
903 else
904 debug_objects_selftest();
905 }
This page took 0.050875 seconds and 6 git commands to generate.