debugobjects: Be smarter about static objects
[linux-2.6-block.git] / lib / debugobjects.c
CommitLineData
3ac7fe5a
TG
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>
d43c36dc 12#include <linux/sched.h>
3ac7fe5a
TG
13#include <linux/seq_file.h>
14#include <linux/debugfs.h>
5a0e3ad6 15#include <linux/slab.h>
3ac7fe5a
TG
16#include <linux/hash.h>
17
18#define ODEBUG_HASH_BITS 14
19#define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
20
21#define ODEBUG_POOL_SIZE 512
22#define ODEBUG_POOL_MIN_LEVEL 256
23
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))
27
28struct debug_bucket {
29 struct hlist_head list;
aef9cb05 30 raw_spinlock_t lock;
3ac7fe5a
TG
31};
32
33static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
34
1be1cb7b 35static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
3ac7fe5a 36
aef9cb05 37static DEFINE_RAW_SPINLOCK(pool_lock);
3ac7fe5a
TG
38
39static HLIST_HEAD(obj_pool);
40
41static int obj_pool_min_free = ODEBUG_POOL_SIZE;
42static int obj_pool_free = ODEBUG_POOL_SIZE;
43static int obj_pool_used;
44static int obj_pool_max_used;
45static struct kmem_cache *obj_cache;
46
47static int debug_objects_maxchain __read_mostly;
48static int debug_objects_fixups __read_mostly;
49static int debug_objects_warnings __read_mostly;
3ae70205
IM
50static int debug_objects_enabled __read_mostly
51 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
52
3ac7fe5a
TG
53static struct debug_obj_descr *descr_test __read_mostly;
54
337fff8b
TG
55static void free_obj_work(struct work_struct *work);
56static DECLARE_WORK(debug_obj_work, free_obj_work);
57
3ac7fe5a
TG
58static int __init enable_object_debug(char *str)
59{
60 debug_objects_enabled = 1;
61 return 0;
62}
3e8ebb5c
KM
63
64static int __init disable_object_debug(char *str)
65{
66 debug_objects_enabled = 0;
67 return 0;
68}
69
3ac7fe5a 70early_param("debug_objects", enable_object_debug);
3e8ebb5c 71early_param("no_debug_objects", disable_object_debug);
3ac7fe5a
TG
72
73static 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",
80};
81
82static int fill_pool(void)
83{
84 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
85 struct debug_obj *new;
50db04dd 86 unsigned long flags;
3ac7fe5a
TG
87
88 if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
89 return obj_pool_free;
90
91 if (unlikely(!obj_cache))
92 return obj_pool_free;
93
94 while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
95
96 new = kmem_cache_zalloc(obj_cache, gfp);
97 if (!new)
98 return obj_pool_free;
99
aef9cb05 100 raw_spin_lock_irqsave(&pool_lock, flags);
3ac7fe5a
TG
101 hlist_add_head(&new->node, &obj_pool);
102 obj_pool_free++;
aef9cb05 103 raw_spin_unlock_irqrestore(&pool_lock, flags);
3ac7fe5a
TG
104 }
105 return obj_pool_free;
106}
107
108/*
109 * Lookup an object in the hash bucket.
110 */
111static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
112{
113 struct hlist_node *node;
114 struct debug_obj *obj;
115 int cnt = 0;
116
117 hlist_for_each_entry(obj, node, &b->list, node) {
118 cnt++;
119 if (obj->object == addr)
120 return obj;
121 }
122 if (cnt > debug_objects_maxchain)
123 debug_objects_maxchain = cnt;
124
125 return NULL;
126}
127
128/*
50db04dd 129 * Allocate a new object. If the pool is empty, switch off the debugger.
673d62cc 130 * Must be called with interrupts disabled.
3ac7fe5a
TG
131 */
132static struct debug_obj *
133alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
134{
135 struct debug_obj *obj = NULL;
3ac7fe5a 136
aef9cb05 137 raw_spin_lock(&pool_lock);
3ac7fe5a
TG
138 if (obj_pool.first) {
139 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
140
141 obj->object = addr;
142 obj->descr = descr;
143 obj->state = ODEBUG_STATE_NONE;
a5d8e467 144 obj->astate = 0;
3ac7fe5a
TG
145 hlist_del(&obj->node);
146
147 hlist_add_head(&obj->node, &b->list);
148
149 obj_pool_used++;
150 if (obj_pool_used > obj_pool_max_used)
151 obj_pool_max_used = obj_pool_used;
152
153 obj_pool_free--;
154 if (obj_pool_free < obj_pool_min_free)
155 obj_pool_min_free = obj_pool_free;
156 }
aef9cb05 157 raw_spin_unlock(&pool_lock);
3ac7fe5a 158
3ac7fe5a
TG
159 return obj;
160}
161
162/*
337fff8b 163 * workqueue function to free objects.
3ac7fe5a 164 */
337fff8b 165static void free_obj_work(struct work_struct *work)
3ac7fe5a 166{
337fff8b 167 struct debug_obj *obj;
673d62cc 168 unsigned long flags;
3ac7fe5a 169
aef9cb05 170 raw_spin_lock_irqsave(&pool_lock, flags);
337fff8b
TG
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--;
175 /*
176 * We release pool_lock across kmem_cache_free() to
177 * avoid contention on pool_lock.
178 */
aef9cb05 179 raw_spin_unlock_irqrestore(&pool_lock, flags);
3ac7fe5a 180 kmem_cache_free(obj_cache, obj);
aef9cb05 181 raw_spin_lock_irqsave(&pool_lock, flags);
3ac7fe5a 182 }
aef9cb05 183 raw_spin_unlock_irqrestore(&pool_lock, flags);
337fff8b
TG
184}
185
186/*
187 * Put the object back into the pool and schedule work to free objects
188 * if necessary.
189 */
190static void free_object(struct debug_obj *obj)
191{
192 unsigned long flags;
193 int sched = 0;
194
aef9cb05 195 raw_spin_lock_irqsave(&pool_lock, flags);
337fff8b
TG
196 /*
197 * schedule work when the pool is filled and the cache is
198 * initialized:
199 */
200 if (obj_pool_free > ODEBUG_POOL_SIZE && obj_cache)
161b6ae0 201 sched = keventd_up() && !work_pending(&debug_obj_work);
337fff8b
TG
202 hlist_add_head(&obj->node, &obj_pool);
203 obj_pool_free++;
204 obj_pool_used--;
aef9cb05 205 raw_spin_unlock_irqrestore(&pool_lock, flags);
337fff8b
TG
206 if (sched)
207 schedule_work(&debug_obj_work);
3ac7fe5a
TG
208}
209
210/*
211 * We run out of memory. That means we probably have tons of objects
212 * allocated.
213 */
214static void debug_objects_oom(void)
215{
216 struct debug_bucket *db = obj_hash;
217 struct hlist_node *node, *tmp;
673d62cc 218 HLIST_HEAD(freelist);
3ac7fe5a
TG
219 struct debug_obj *obj;
220 unsigned long flags;
221 int i;
222
223 printk(KERN_WARNING "ODEBUG: Out of memory. ODEBUG disabled\n");
224
225 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
aef9cb05 226 raw_spin_lock_irqsave(&db->lock, flags);
673d62cc 227 hlist_move_list(&db->list, &freelist);
aef9cb05 228 raw_spin_unlock_irqrestore(&db->lock, flags);
673d62cc
VN
229
230 /* Now free them */
231 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
3ac7fe5a
TG
232 hlist_del(&obj->node);
233 free_object(obj);
234 }
3ac7fe5a
TG
235 }
236}
237
238/*
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.
241 */
242static struct debug_bucket *get_bucket(unsigned long addr)
243{
244 unsigned long hash;
245
246 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
247 return &obj_hash[hash];
248}
249
250static void debug_print_object(struct debug_obj *obj, char *msg)
251{
99777288 252 struct debug_obj_descr *descr = obj->descr;
3ac7fe5a
TG
253 static int limit;
254
99777288
SG
255 if (limit < 5 && descr != descr_test) {
256 void *hint = descr->debug_hint ?
257 descr->debug_hint(obj->object) : NULL;
3ac7fe5a 258 limit++;
a5d8e467 259 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
99777288 260 "object type: %s hint: %pS\n",
a5d8e467 261 msg, obj_states[obj->state], obj->astate,
99777288 262 descr->name, hint);
3ac7fe5a
TG
263 }
264 debug_objects_warnings++;
265}
266
267/*
268 * Try to repair the damage, so we have a better chance to get useful
269 * debug output.
270 */
feac18dd 271static int
3ac7fe5a
TG
272debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state),
273 void * addr, enum debug_obj_state state)
274{
feac18dd
SB
275 int fixed = 0;
276
3ac7fe5a 277 if (fixup)
feac18dd
SB
278 fixed = fixup(addr, state);
279 debug_objects_fixups += fixed;
280 return fixed;
3ac7fe5a
TG
281}
282
283static void debug_object_is_on_stack(void *addr, int onstack)
284{
3ac7fe5a
TG
285 int is_on_stack;
286 static int limit;
287
288 if (limit > 4)
289 return;
290
8b05c7e6 291 is_on_stack = object_is_on_stack(addr);
3ac7fe5a
TG
292 if (is_on_stack == onstack)
293 return;
294
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);
303}
304
305static void
306__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
307{
308 enum debug_obj_state state;
309 struct debug_bucket *db;
310 struct debug_obj *obj;
311 unsigned long flags;
312
50db04dd
VN
313 fill_pool();
314
3ac7fe5a
TG
315 db = get_bucket((unsigned long) addr);
316
aef9cb05 317 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
318
319 obj = lookup_object(addr, db);
320 if (!obj) {
321 obj = alloc_object(addr, db, descr);
322 if (!obj) {
323 debug_objects_enabled = 0;
aef9cb05 324 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
325 debug_objects_oom();
326 return;
327 }
328 debug_object_is_on_stack(addr, onstack);
329 }
330
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;
337
338 case ODEBUG_STATE_ACTIVE:
339 debug_print_object(obj, "init");
340 state = obj->state;
aef9cb05 341 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
342 debug_object_fixup(descr->fixup_init, addr, state);
343 return;
344
345 case ODEBUG_STATE_DESTROYED:
346 debug_print_object(obj, "init");
347 break;
348 default:
349 break;
350 }
351
aef9cb05 352 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
353}
354
355/**
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
359 */
360void debug_object_init(void *addr, struct debug_obj_descr *descr)
361{
362 if (!debug_objects_enabled)
363 return;
364
365 __debug_object_init(addr, descr, 0);
366}
367
368/**
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
373 */
374void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
375{
376 if (!debug_objects_enabled)
377 return;
378
379 __debug_object_init(addr, descr, 1);
380}
381
382/**
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
386 */
387void debug_object_activate(void *addr, struct debug_obj_descr *descr)
388{
389 enum debug_obj_state state;
390 struct debug_bucket *db;
391 struct debug_obj *obj;
392 unsigned long flags;
feac18dd
SB
393 struct debug_obj o = { .object = addr,
394 .state = ODEBUG_STATE_NOTAVAILABLE,
395 .descr = descr };
3ac7fe5a
TG
396
397 if (!debug_objects_enabled)
398 return;
399
400 db = get_bucket((unsigned long) addr);
401
aef9cb05 402 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
403
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;
411
412 case ODEBUG_STATE_ACTIVE:
413 debug_print_object(obj, "activate");
414 state = obj->state;
aef9cb05 415 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
416 debug_object_fixup(descr->fixup_activate, addr, state);
417 return;
418
419 case ODEBUG_STATE_DESTROYED:
420 debug_print_object(obj, "activate");
421 break;
422 default:
423 break;
424 }
aef9cb05 425 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
426 return;
427 }
428
aef9cb05 429 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
430 /*
431 * This happens when a static object is activated. We
432 * let the type specific code decide whether this is
433 * true or not.
434 */
feac18dd
SB
435 if (debug_object_fixup(descr->fixup_activate, addr,
436 ODEBUG_STATE_NOTAVAILABLE))
437 debug_print_object(&o, "activate");
3ac7fe5a
TG
438}
439
440/**
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
444 */
445void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
446{
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
aef9cb05 456 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
457
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:
a5d8e467
MD
464 if (!obj->astate)
465 obj->state = ODEBUG_STATE_INACTIVE;
466 else
467 debug_print_object(obj, "deactivate");
3ac7fe5a
TG
468 break;
469
470 case ODEBUG_STATE_DESTROYED:
471 debug_print_object(obj, "deactivate");
472 break;
473 default:
474 break;
475 }
476 } else {
477 struct debug_obj o = { .object = addr,
478 .state = ODEBUG_STATE_NOTAVAILABLE,
479 .descr = descr };
480
481 debug_print_object(&o, "deactivate");
482 }
483
aef9cb05 484 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
485}
486
487/**
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
491 */
492void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
493{
494 enum debug_obj_state state;
495 struct debug_bucket *db;
496 struct debug_obj *obj;
497 unsigned long flags;
498
499 if (!debug_objects_enabled)
500 return;
501
502 db = get_bucket((unsigned long) addr);
503
aef9cb05 504 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
505
506 obj = lookup_object(addr, db);
507 if (!obj)
508 goto out_unlock;
509
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;
aef9cb05 519 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
520 debug_object_fixup(descr->fixup_destroy, addr, state);
521 return;
522
523 case ODEBUG_STATE_DESTROYED:
524 debug_print_object(obj, "destroy");
525 break;
526 default:
527 break;
528 }
529out_unlock:
aef9cb05 530 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
531}
532
533/**
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
537 */
538void debug_object_free(void *addr, struct debug_obj_descr *descr)
539{
540 enum debug_obj_state state;
541 struct debug_bucket *db;
542 struct debug_obj *obj;
543 unsigned long flags;
544
545 if (!debug_objects_enabled)
546 return;
547
548 db = get_bucket((unsigned long) addr);
549
aef9cb05 550 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
551
552 obj = lookup_object(addr, db);
553 if (!obj)
554 goto out_unlock;
555
556 switch (obj->state) {
557 case ODEBUG_STATE_ACTIVE:
558 debug_print_object(obj, "free");
559 state = obj->state;
aef9cb05 560 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
561 debug_object_fixup(descr->fixup_free, addr, state);
562 return;
563 default:
564 hlist_del(&obj->node);
aef9cb05 565 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a 566 free_object(obj);
673d62cc 567 return;
3ac7fe5a
TG
568 }
569out_unlock:
aef9cb05 570 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
571}
572
a5d8e467
MD
573/**
574 * debug_object_active_state - debug checks object usage state machine
575 * @addr: address of the object
576 * @descr: pointer to an object specific debug description structure
577 * @expect: expected state
578 * @next: state to move to if expected state is found
579 */
580void
581debug_object_active_state(void *addr, struct debug_obj_descr *descr,
582 unsigned int expect, unsigned int next)
583{
584 struct debug_bucket *db;
585 struct debug_obj *obj;
586 unsigned long flags;
587
588 if (!debug_objects_enabled)
589 return;
590
591 db = get_bucket((unsigned long) addr);
592
593 raw_spin_lock_irqsave(&db->lock, flags);
594
595 obj = lookup_object(addr, db);
596 if (obj) {
597 switch (obj->state) {
598 case ODEBUG_STATE_ACTIVE:
599 if (obj->astate == expect)
600 obj->astate = next;
601 else
602 debug_print_object(obj, "active_state");
603 break;
604
605 default:
606 debug_print_object(obj, "active_state");
607 break;
608 }
609 } else {
610 struct debug_obj o = { .object = addr,
611 .state = ODEBUG_STATE_NOTAVAILABLE,
612 .descr = descr };
613
614 debug_print_object(&o, "active_state");
615 }
616
617 raw_spin_unlock_irqrestore(&db->lock, flags);
618}
619
3ac7fe5a
TG
620#ifdef CONFIG_DEBUG_OBJECTS_FREE
621static void __debug_check_no_obj_freed(const void *address, unsigned long size)
622{
623 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
624 struct hlist_node *node, *tmp;
673d62cc 625 HLIST_HEAD(freelist);
3ac7fe5a
TG
626 struct debug_obj_descr *descr;
627 enum debug_obj_state state;
628 struct debug_bucket *db;
629 struct debug_obj *obj;
630 int cnt;
631
632 saddr = (unsigned long) address;
633 eaddr = saddr + size;
634 paddr = saddr & ODEBUG_CHUNK_MASK;
635 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
636 chunks >>= ODEBUG_CHUNK_SHIFT;
637
638 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
639 db = get_bucket(paddr);
640
641repeat:
642 cnt = 0;
aef9cb05 643 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
644 hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
645 cnt++;
646 oaddr = (unsigned long) obj->object;
647 if (oaddr < saddr || oaddr >= eaddr)
648 continue;
649
650 switch (obj->state) {
651 case ODEBUG_STATE_ACTIVE:
652 debug_print_object(obj, "free");
653 descr = obj->descr;
654 state = obj->state;
aef9cb05 655 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
656 debug_object_fixup(descr->fixup_free,
657 (void *) oaddr, state);
658 goto repeat;
659 default:
660 hlist_del(&obj->node);
673d62cc 661 hlist_add_head(&obj->node, &freelist);
3ac7fe5a
TG
662 break;
663 }
664 }
aef9cb05 665 raw_spin_unlock_irqrestore(&db->lock, flags);
673d62cc
VN
666
667 /* Now free them */
668 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
669 hlist_del(&obj->node);
670 free_object(obj);
671 }
672
3ac7fe5a
TG
673 if (cnt > debug_objects_maxchain)
674 debug_objects_maxchain = cnt;
675 }
676}
677
678void debug_check_no_obj_freed(const void *address, unsigned long size)
679{
680 if (debug_objects_enabled)
681 __debug_check_no_obj_freed(address, size);
682}
683#endif
684
685#ifdef CONFIG_DEBUG_FS
686
687static int debug_stats_show(struct seq_file *m, void *v)
688{
689 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
690 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
691 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
692 seq_printf(m, "pool_free :%d\n", obj_pool_free);
693 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
694 seq_printf(m, "pool_used :%d\n", obj_pool_used);
695 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
696 return 0;
697}
698
699static int debug_stats_open(struct inode *inode, struct file *filp)
700{
701 return single_open(filp, debug_stats_show, NULL);
702}
703
704static const struct file_operations debug_stats_fops = {
705 .open = debug_stats_open,
706 .read = seq_read,
707 .llseek = seq_lseek,
708 .release = single_release,
709};
710
711static int __init debug_objects_init_debugfs(void)
712{
713 struct dentry *dbgdir, *dbgstats;
714
715 if (!debug_objects_enabled)
716 return 0;
717
718 dbgdir = debugfs_create_dir("debug_objects", NULL);
719 if (!dbgdir)
720 return -ENOMEM;
721
722 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
723 &debug_stats_fops);
724 if (!dbgstats)
725 goto err;
726
727 return 0;
728
729err:
730 debugfs_remove(dbgdir);
731
732 return -ENOMEM;
733}
734__initcall(debug_objects_init_debugfs);
735
736#else
737static inline void debug_objects_init_debugfs(void) { }
738#endif
739
740#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
741
742/* Random data structure for the self test */
743struct self_test {
744 unsigned long dummy1[6];
745 int static_init;
746 unsigned long dummy2[3];
747};
748
749static __initdata struct debug_obj_descr descr_type_test;
750
751/*
752 * fixup_init is called when:
753 * - an active object is initialized
754 */
755static int __init fixup_init(void *addr, enum debug_obj_state state)
756{
757 struct self_test *obj = addr;
758
759 switch (state) {
760 case ODEBUG_STATE_ACTIVE:
761 debug_object_deactivate(obj, &descr_type_test);
762 debug_object_init(obj, &descr_type_test);
763 return 1;
764 default:
765 return 0;
766 }
767}
768
769/*
770 * fixup_activate is called when:
771 * - an active object is activated
772 * - an unknown object is activated (might be a statically initialized object)
773 */
774static int __init fixup_activate(void *addr, enum debug_obj_state state)
775{
776 struct self_test *obj = addr;
777
778 switch (state) {
779 case ODEBUG_STATE_NOTAVAILABLE:
780 if (obj->static_init == 1) {
781 debug_object_init(obj, &descr_type_test);
782 debug_object_activate(obj, &descr_type_test);
783 /*
784 * Real code should return 0 here ! This is
785 * not a fixup of some bad behaviour. We
786 * merily call the debug_init function to keep
787 * track of the object.
788 */
789 return 1;
790 } else {
791 /* Real code needs to emit a warning here */
792 }
793 return 0;
794
795 case ODEBUG_STATE_ACTIVE:
796 debug_object_deactivate(obj, &descr_type_test);
797 debug_object_activate(obj, &descr_type_test);
798 return 1;
799
800 default:
801 return 0;
802 }
803}
804
805/*
806 * fixup_destroy is called when:
807 * - an active object is destroyed
808 */
809static int __init fixup_destroy(void *addr, enum debug_obj_state state)
810{
811 struct self_test *obj = addr;
812
813 switch (state) {
814 case ODEBUG_STATE_ACTIVE:
815 debug_object_deactivate(obj, &descr_type_test);
816 debug_object_destroy(obj, &descr_type_test);
817 return 1;
818 default:
819 return 0;
820 }
821}
822
823/*
824 * fixup_free is called when:
825 * - an active object is freed
826 */
827static int __init fixup_free(void *addr, enum debug_obj_state state)
828{
829 struct self_test *obj = addr;
830
831 switch (state) {
832 case ODEBUG_STATE_ACTIVE:
833 debug_object_deactivate(obj, &descr_type_test);
834 debug_object_free(obj, &descr_type_test);
835 return 1;
836 default:
837 return 0;
838 }
839}
840
1fb2f77c 841static int __init
3ac7fe5a
TG
842check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
843{
844 struct debug_bucket *db;
845 struct debug_obj *obj;
846 unsigned long flags;
847 int res = -EINVAL;
848
849 db = get_bucket((unsigned long) addr);
850
aef9cb05 851 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
852
853 obj = lookup_object(addr, db);
854 if (!obj && state != ODEBUG_STATE_NONE) {
5cd2b459 855 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
3ac7fe5a
TG
856 goto out;
857 }
858 if (obj && obj->state != state) {
5cd2b459 859 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
3ac7fe5a 860 obj->state, state);
3ac7fe5a
TG
861 goto out;
862 }
863 if (fixups != debug_objects_fixups) {
5cd2b459 864 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
3ac7fe5a 865 fixups, debug_objects_fixups);
3ac7fe5a
TG
866 goto out;
867 }
868 if (warnings != debug_objects_warnings) {
5cd2b459 869 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
3ac7fe5a 870 warnings, debug_objects_warnings);
3ac7fe5a
TG
871 goto out;
872 }
873 res = 0;
874out:
aef9cb05 875 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
876 if (res)
877 debug_objects_enabled = 0;
878 return res;
879}
880
881static __initdata struct debug_obj_descr descr_type_test = {
882 .name = "selftest",
883 .fixup_init = fixup_init,
884 .fixup_activate = fixup_activate,
885 .fixup_destroy = fixup_destroy,
886 .fixup_free = fixup_free,
887};
888
889static __initdata struct self_test obj = { .static_init = 0 };
890
891static void __init debug_objects_selftest(void)
892{
893 int fixups, oldfixups, warnings, oldwarnings;
894 unsigned long flags;
895
896 local_irq_save(flags);
897
898 fixups = oldfixups = debug_objects_fixups;
899 warnings = oldwarnings = debug_objects_warnings;
900 descr_test = &descr_type_test;
901
902 debug_object_init(&obj, &descr_type_test);
903 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
904 goto out;
905 debug_object_activate(&obj, &descr_type_test);
906 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
907 goto out;
908 debug_object_activate(&obj, &descr_type_test);
909 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
910 goto out;
911 debug_object_deactivate(&obj, &descr_type_test);
912 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
913 goto out;
914 debug_object_destroy(&obj, &descr_type_test);
915 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
916 goto out;
917 debug_object_init(&obj, &descr_type_test);
918 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
919 goto out;
920 debug_object_activate(&obj, &descr_type_test);
921 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
922 goto out;
923 debug_object_deactivate(&obj, &descr_type_test);
924 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
925 goto out;
926 debug_object_free(&obj, &descr_type_test);
927 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
928 goto out;
929
930 obj.static_init = 1;
931 debug_object_activate(&obj, &descr_type_test);
932 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
933 goto out;
934 debug_object_init(&obj, &descr_type_test);
935 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
936 goto out;
937 debug_object_free(&obj, &descr_type_test);
938 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
939 goto out;
940
941#ifdef CONFIG_DEBUG_OBJECTS_FREE
942 debug_object_init(&obj, &descr_type_test);
943 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
944 goto out;
945 debug_object_activate(&obj, &descr_type_test);
946 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
947 goto out;
948 __debug_check_no_obj_freed(&obj, sizeof(obj));
949 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
950 goto out;
951#endif
952 printk(KERN_INFO "ODEBUG: selftest passed\n");
953
954out:
955 debug_objects_fixups = oldfixups;
956 debug_objects_warnings = oldwarnings;
957 descr_test = NULL;
958
959 local_irq_restore(flags);
960}
961#else
962static inline void debug_objects_selftest(void) { }
963#endif
964
965/*
966 * Called during early boot to initialize the hash buckets and link
967 * the static object pool objects into the poll list. After this call
968 * the object tracker is fully operational.
969 */
970void __init debug_objects_early_init(void)
971{
972 int i;
973
974 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
aef9cb05 975 raw_spin_lock_init(&obj_hash[i].lock);
3ac7fe5a
TG
976
977 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
978 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
979}
980
1be1cb7b
TG
981/*
982 * Convert the statically allocated objects to dynamic ones:
983 */
1fb2f77c 984static int __init debug_objects_replace_static_objects(void)
1be1cb7b
TG
985{
986 struct debug_bucket *db = obj_hash;
987 struct hlist_node *node, *tmp;
988 struct debug_obj *obj, *new;
989 HLIST_HEAD(objects);
990 int i, cnt = 0;
991
992 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
993 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
994 if (!obj)
995 goto free;
996 hlist_add_head(&obj->node, &objects);
997 }
998
999 /*
1000 * When debug_objects_mem_init() is called we know that only
1001 * one CPU is up, so disabling interrupts is enough
1002 * protection. This avoids the lockdep hell of lock ordering.
1003 */
1004 local_irq_disable();
1005
1006 /* Remove the statically allocated objects from the pool */
1007 hlist_for_each_entry_safe(obj, node, tmp, &obj_pool, node)
1008 hlist_del(&obj->node);
1009 /* Move the allocated objects to the pool */
1010 hlist_move_list(&objects, &obj_pool);
1011
1012 /* Replace the active object references */
1013 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1014 hlist_move_list(&db->list, &objects);
1015
1016 hlist_for_each_entry(obj, node, &objects, node) {
1017 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1018 hlist_del(&new->node);
1019 /* copy object data */
1020 *new = *obj;
1021 hlist_add_head(&new->node, &db->list);
1022 cnt++;
1023 }
1024 }
1025
1026 printk(KERN_DEBUG "ODEBUG: %d of %d active objects replaced\n", cnt,
1027 obj_pool_used);
1028 local_irq_enable();
1029 return 0;
1030free:
1031 hlist_for_each_entry_safe(obj, node, tmp, &objects, node) {
1032 hlist_del(&obj->node);
1033 kmem_cache_free(obj_cache, obj);
1034 }
1035 return -ENOMEM;
1036}
1037
3ac7fe5a
TG
1038/*
1039 * Called after the kmem_caches are functional to setup a dedicated
1040 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1041 * prevents that the debug code is called on kmem_cache_free() for the
1042 * debug tracker objects to avoid recursive calls.
1043 */
1044void __init debug_objects_mem_init(void)
1045{
1046 if (!debug_objects_enabled)
1047 return;
1048
1049 obj_cache = kmem_cache_create("debug_objects_cache",
1050 sizeof (struct debug_obj), 0,
1051 SLAB_DEBUG_OBJECTS, NULL);
1052
1be1cb7b 1053 if (!obj_cache || debug_objects_replace_static_objects()) {
3ac7fe5a 1054 debug_objects_enabled = 0;
1be1cb7b
TG
1055 if (obj_cache)
1056 kmem_cache_destroy(obj_cache);
1057 printk(KERN_WARNING "ODEBUG: out of memory.\n");
1058 } else
3ac7fe5a
TG
1059 debug_objects_selftest();
1060}