blob: b862b30369ffe5afa25c471263964d62321a2872 [file] [log] [blame]
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001/*
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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040012#include <linux/sched.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070013#include <linux/seq_file.h>
14#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070016#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;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010030 raw_spinlock_t lock;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070031};
32
33static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
34
Thomas Gleixner1be1cb72009-03-16 18:53:18 +010035static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070036
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010037static DEFINE_RAW_SPINLOCK(pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070038
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;
Ingo Molnar3ae70202008-11-26 10:02:00 +010050static int debug_objects_enabled __read_mostly
51 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
52
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070053static struct debug_obj_descr *descr_test __read_mostly;
54
Thomas Gleixner337fff82009-03-16 10:04:53 +010055static void free_obj_work(struct work_struct *work);
56static DECLARE_WORK(debug_obj_work, free_obj_work);
57
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070058static int __init enable_object_debug(char *str)
59{
60 debug_objects_enabled = 1;
61 return 0;
62}
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050063
64static int __init disable_object_debug(char *str)
65{
66 debug_objects_enabled = 0;
67 return 0;
68}
69
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070070early_param("debug_objects", enable_object_debug);
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050071early_param("no_debug_objects", disable_object_debug);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070072
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;
Vegard Nossum50db04dd2008-06-15 00:47:36 +020086 unsigned long flags;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070087
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
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100100 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700101 hlist_add_head(&new->node, &obj_pool);
102 obj_pool_free++;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100103 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700104 }
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/*
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200129 * Allocate a new object. If the pool is empty, switch off the debugger.
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200130 * Must be called with interrupts disabled.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700131 */
132static struct debug_obj *
133alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
134{
135 struct debug_obj *obj = NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700136
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100137 raw_spin_lock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700138 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;
144 hlist_del(&obj->node);
145
146 hlist_add_head(&obj->node, &b->list);
147
148 obj_pool_used++;
149 if (obj_pool_used > obj_pool_max_used)
150 obj_pool_max_used = obj_pool_used;
151
152 obj_pool_free--;
153 if (obj_pool_free < obj_pool_min_free)
154 obj_pool_min_free = obj_pool_free;
155 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100156 raw_spin_unlock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700157
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700158 return obj;
159}
160
161/*
Thomas Gleixner337fff82009-03-16 10:04:53 +0100162 * workqueue function to free objects.
163 */
164static void free_obj_work(struct work_struct *work)
165{
166 struct debug_obj *obj;
167 unsigned long flags;
168
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100169 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100170 while (obj_pool_free > ODEBUG_POOL_SIZE) {
171 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
172 hlist_del(&obj->node);
173 obj_pool_free--;
174 /*
175 * We release pool_lock across kmem_cache_free() to
176 * avoid contention on pool_lock.
177 */
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100178 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100179 kmem_cache_free(obj_cache, obj);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100180 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100181 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100182 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100183}
184
185/*
186 * Put the object back into the pool and schedule work to free objects
187 * if necessary.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700188 */
189static void free_object(struct debug_obj *obj)
190{
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200191 unsigned long flags;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100192 int sched = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700193
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100194 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100195 /*
196 * schedule work when the pool is filled and the cache is
197 * initialized:
198 */
199 if (obj_pool_free > ODEBUG_POOL_SIZE && obj_cache)
200 sched = !work_pending(&debug_obj_work);
201 hlist_add_head(&obj->node, &obj_pool);
202 obj_pool_free++;
203 obj_pool_used--;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100204 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100205 if (sched)
206 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700207}
208
209/*
210 * We run out of memory. That means we probably have tons of objects
211 * allocated.
212 */
213static void debug_objects_oom(void)
214{
215 struct debug_bucket *db = obj_hash;
216 struct hlist_node *node, *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200217 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700218 struct debug_obj *obj;
219 unsigned long flags;
220 int i;
221
222 printk(KERN_WARNING "ODEBUG: Out of memory. ODEBUG disabled\n");
223
224 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100225 raw_spin_lock_irqsave(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200226 hlist_move_list(&db->list, &freelist);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100227 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200228
229 /* Now free them */
230 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700231 hlist_del(&obj->node);
232 free_object(obj);
233 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700234 }
235}
236
237/*
238 * We use the pfn of the address for the hash. That way we can check
239 * for freed objects simply by checking the affected bucket.
240 */
241static struct debug_bucket *get_bucket(unsigned long addr)
242{
243 unsigned long hash;
244
245 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
246 return &obj_hash[hash];
247}
248
249static void debug_print_object(struct debug_obj *obj, char *msg)
250{
251 static int limit;
252
253 if (limit < 5 && obj->descr != descr_test) {
254 limit++;
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700255 WARN(1, KERN_ERR "ODEBUG: %s %s object type: %s\n", msg,
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700256 obj_states[obj->state], obj->descr->name);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700257 }
258 debug_objects_warnings++;
259}
260
261/*
262 * Try to repair the damage, so we have a better chance to get useful
263 * debug output.
264 */
265static void
266debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state),
267 void * addr, enum debug_obj_state state)
268{
269 if (fixup)
270 debug_objects_fixups += fixup(addr, state);
271}
272
273static void debug_object_is_on_stack(void *addr, int onstack)
274{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700275 int is_on_stack;
276 static int limit;
277
278 if (limit > 4)
279 return;
280
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -0700281 is_on_stack = object_is_on_stack(addr);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700282 if (is_on_stack == onstack)
283 return;
284
285 limit++;
286 if (is_on_stack)
287 printk(KERN_WARNING
288 "ODEBUG: object is on stack, but not annotated\n");
289 else
290 printk(KERN_WARNING
291 "ODEBUG: object is not on stack, but annotated\n");
292 WARN_ON(1);
293}
294
295static void
296__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
297{
298 enum debug_obj_state state;
299 struct debug_bucket *db;
300 struct debug_obj *obj;
301 unsigned long flags;
302
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200303 fill_pool();
304
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700305 db = get_bucket((unsigned long) addr);
306
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100307 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700308
309 obj = lookup_object(addr, db);
310 if (!obj) {
311 obj = alloc_object(addr, db, descr);
312 if (!obj) {
313 debug_objects_enabled = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100314 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700315 debug_objects_oom();
316 return;
317 }
318 debug_object_is_on_stack(addr, onstack);
319 }
320
321 switch (obj->state) {
322 case ODEBUG_STATE_NONE:
323 case ODEBUG_STATE_INIT:
324 case ODEBUG_STATE_INACTIVE:
325 obj->state = ODEBUG_STATE_INIT;
326 break;
327
328 case ODEBUG_STATE_ACTIVE:
329 debug_print_object(obj, "init");
330 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100331 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700332 debug_object_fixup(descr->fixup_init, addr, state);
333 return;
334
335 case ODEBUG_STATE_DESTROYED:
336 debug_print_object(obj, "init");
337 break;
338 default:
339 break;
340 }
341
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100342 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700343}
344
345/**
346 * debug_object_init - debug checks when an object is initialized
347 * @addr: address of the object
348 * @descr: pointer to an object specific debug description structure
349 */
350void debug_object_init(void *addr, struct debug_obj_descr *descr)
351{
352 if (!debug_objects_enabled)
353 return;
354
355 __debug_object_init(addr, descr, 0);
356}
357
358/**
359 * debug_object_init_on_stack - debug checks when an object on stack is
360 * initialized
361 * @addr: address of the object
362 * @descr: pointer to an object specific debug description structure
363 */
364void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
365{
366 if (!debug_objects_enabled)
367 return;
368
369 __debug_object_init(addr, descr, 1);
370}
371
372/**
373 * debug_object_activate - debug checks when an object is activated
374 * @addr: address of the object
375 * @descr: pointer to an object specific debug description structure
376 */
377void debug_object_activate(void *addr, struct debug_obj_descr *descr)
378{
379 enum debug_obj_state state;
380 struct debug_bucket *db;
381 struct debug_obj *obj;
382 unsigned long flags;
383
384 if (!debug_objects_enabled)
385 return;
386
387 db = get_bucket((unsigned long) addr);
388
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100389 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700390
391 obj = lookup_object(addr, db);
392 if (obj) {
393 switch (obj->state) {
394 case ODEBUG_STATE_INIT:
395 case ODEBUG_STATE_INACTIVE:
396 obj->state = ODEBUG_STATE_ACTIVE;
397 break;
398
399 case ODEBUG_STATE_ACTIVE:
400 debug_print_object(obj, "activate");
401 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100402 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700403 debug_object_fixup(descr->fixup_activate, addr, state);
404 return;
405
406 case ODEBUG_STATE_DESTROYED:
407 debug_print_object(obj, "activate");
408 break;
409 default:
410 break;
411 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100412 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700413 return;
414 }
415
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100416 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700417 /*
418 * This happens when a static object is activated. We
419 * let the type specific code decide whether this is
420 * true or not.
421 */
422 debug_object_fixup(descr->fixup_activate, addr,
423 ODEBUG_STATE_NOTAVAILABLE);
424}
425
426/**
427 * debug_object_deactivate - debug checks when an object is deactivated
428 * @addr: address of the object
429 * @descr: pointer to an object specific debug description structure
430 */
431void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
432{
433 struct debug_bucket *db;
434 struct debug_obj *obj;
435 unsigned long flags;
436
437 if (!debug_objects_enabled)
438 return;
439
440 db = get_bucket((unsigned long) addr);
441
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100442 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700443
444 obj = lookup_object(addr, db);
445 if (obj) {
446 switch (obj->state) {
447 case ODEBUG_STATE_INIT:
448 case ODEBUG_STATE_INACTIVE:
449 case ODEBUG_STATE_ACTIVE:
450 obj->state = ODEBUG_STATE_INACTIVE;
451 break;
452
453 case ODEBUG_STATE_DESTROYED:
454 debug_print_object(obj, "deactivate");
455 break;
456 default:
457 break;
458 }
459 } else {
460 struct debug_obj o = { .object = addr,
461 .state = ODEBUG_STATE_NOTAVAILABLE,
462 .descr = descr };
463
464 debug_print_object(&o, "deactivate");
465 }
466
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100467 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700468}
469
470/**
471 * debug_object_destroy - debug checks when an object is destroyed
472 * @addr: address of the object
473 * @descr: pointer to an object specific debug description structure
474 */
475void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
476{
477 enum debug_obj_state state;
478 struct debug_bucket *db;
479 struct debug_obj *obj;
480 unsigned long flags;
481
482 if (!debug_objects_enabled)
483 return;
484
485 db = get_bucket((unsigned long) addr);
486
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100487 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700488
489 obj = lookup_object(addr, db);
490 if (!obj)
491 goto out_unlock;
492
493 switch (obj->state) {
494 case ODEBUG_STATE_NONE:
495 case ODEBUG_STATE_INIT:
496 case ODEBUG_STATE_INACTIVE:
497 obj->state = ODEBUG_STATE_DESTROYED;
498 break;
499 case ODEBUG_STATE_ACTIVE:
500 debug_print_object(obj, "destroy");
501 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100502 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700503 debug_object_fixup(descr->fixup_destroy, addr, state);
504 return;
505
506 case ODEBUG_STATE_DESTROYED:
507 debug_print_object(obj, "destroy");
508 break;
509 default:
510 break;
511 }
512out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100513 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700514}
515
516/**
517 * debug_object_free - debug checks when an object is freed
518 * @addr: address of the object
519 * @descr: pointer to an object specific debug description structure
520 */
521void debug_object_free(void *addr, struct debug_obj_descr *descr)
522{
523 enum debug_obj_state state;
524 struct debug_bucket *db;
525 struct debug_obj *obj;
526 unsigned long flags;
527
528 if (!debug_objects_enabled)
529 return;
530
531 db = get_bucket((unsigned long) addr);
532
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100533 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700534
535 obj = lookup_object(addr, db);
536 if (!obj)
537 goto out_unlock;
538
539 switch (obj->state) {
540 case ODEBUG_STATE_ACTIVE:
541 debug_print_object(obj, "free");
542 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100543 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700544 debug_object_fixup(descr->fixup_free, addr, state);
545 return;
546 default:
547 hlist_del(&obj->node);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100548 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700549 free_object(obj);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200550 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700551 }
552out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100553 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700554}
555
556#ifdef CONFIG_DEBUG_OBJECTS_FREE
557static void __debug_check_no_obj_freed(const void *address, unsigned long size)
558{
559 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
560 struct hlist_node *node, *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200561 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700562 struct debug_obj_descr *descr;
563 enum debug_obj_state state;
564 struct debug_bucket *db;
565 struct debug_obj *obj;
566 int cnt;
567
568 saddr = (unsigned long) address;
569 eaddr = saddr + size;
570 paddr = saddr & ODEBUG_CHUNK_MASK;
571 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
572 chunks >>= ODEBUG_CHUNK_SHIFT;
573
574 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
575 db = get_bucket(paddr);
576
577repeat:
578 cnt = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100579 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700580 hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
581 cnt++;
582 oaddr = (unsigned long) obj->object;
583 if (oaddr < saddr || oaddr >= eaddr)
584 continue;
585
586 switch (obj->state) {
587 case ODEBUG_STATE_ACTIVE:
588 debug_print_object(obj, "free");
589 descr = obj->descr;
590 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100591 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700592 debug_object_fixup(descr->fixup_free,
593 (void *) oaddr, state);
594 goto repeat;
595 default:
596 hlist_del(&obj->node);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200597 hlist_add_head(&obj->node, &freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700598 break;
599 }
600 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100601 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200602
603 /* Now free them */
604 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
605 hlist_del(&obj->node);
606 free_object(obj);
607 }
608
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700609 if (cnt > debug_objects_maxchain)
610 debug_objects_maxchain = cnt;
611 }
612}
613
614void debug_check_no_obj_freed(const void *address, unsigned long size)
615{
616 if (debug_objects_enabled)
617 __debug_check_no_obj_freed(address, size);
618}
619#endif
620
621#ifdef CONFIG_DEBUG_FS
622
623static int debug_stats_show(struct seq_file *m, void *v)
624{
625 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
626 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
627 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
628 seq_printf(m, "pool_free :%d\n", obj_pool_free);
629 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
630 seq_printf(m, "pool_used :%d\n", obj_pool_used);
631 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
632 return 0;
633}
634
635static int debug_stats_open(struct inode *inode, struct file *filp)
636{
637 return single_open(filp, debug_stats_show, NULL);
638}
639
640static const struct file_operations debug_stats_fops = {
641 .open = debug_stats_open,
642 .read = seq_read,
643 .llseek = seq_lseek,
644 .release = single_release,
645};
646
647static int __init debug_objects_init_debugfs(void)
648{
649 struct dentry *dbgdir, *dbgstats;
650
651 if (!debug_objects_enabled)
652 return 0;
653
654 dbgdir = debugfs_create_dir("debug_objects", NULL);
655 if (!dbgdir)
656 return -ENOMEM;
657
658 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
659 &debug_stats_fops);
660 if (!dbgstats)
661 goto err;
662
663 return 0;
664
665err:
666 debugfs_remove(dbgdir);
667
668 return -ENOMEM;
669}
670__initcall(debug_objects_init_debugfs);
671
672#else
673static inline void debug_objects_init_debugfs(void) { }
674#endif
675
676#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
677
678/* Random data structure for the self test */
679struct self_test {
680 unsigned long dummy1[6];
681 int static_init;
682 unsigned long dummy2[3];
683};
684
685static __initdata struct debug_obj_descr descr_type_test;
686
687/*
688 * fixup_init is called when:
689 * - an active object is initialized
690 */
691static int __init fixup_init(void *addr, enum debug_obj_state state)
692{
693 struct self_test *obj = addr;
694
695 switch (state) {
696 case ODEBUG_STATE_ACTIVE:
697 debug_object_deactivate(obj, &descr_type_test);
698 debug_object_init(obj, &descr_type_test);
699 return 1;
700 default:
701 return 0;
702 }
703}
704
705/*
706 * fixup_activate is called when:
707 * - an active object is activated
708 * - an unknown object is activated (might be a statically initialized object)
709 */
710static int __init fixup_activate(void *addr, enum debug_obj_state state)
711{
712 struct self_test *obj = addr;
713
714 switch (state) {
715 case ODEBUG_STATE_NOTAVAILABLE:
716 if (obj->static_init == 1) {
717 debug_object_init(obj, &descr_type_test);
718 debug_object_activate(obj, &descr_type_test);
719 /*
720 * Real code should return 0 here ! This is
721 * not a fixup of some bad behaviour. We
722 * merily call the debug_init function to keep
723 * track of the object.
724 */
725 return 1;
726 } else {
727 /* Real code needs to emit a warning here */
728 }
729 return 0;
730
731 case ODEBUG_STATE_ACTIVE:
732 debug_object_deactivate(obj, &descr_type_test);
733 debug_object_activate(obj, &descr_type_test);
734 return 1;
735
736 default:
737 return 0;
738 }
739}
740
741/*
742 * fixup_destroy is called when:
743 * - an active object is destroyed
744 */
745static int __init fixup_destroy(void *addr, enum debug_obj_state state)
746{
747 struct self_test *obj = addr;
748
749 switch (state) {
750 case ODEBUG_STATE_ACTIVE:
751 debug_object_deactivate(obj, &descr_type_test);
752 debug_object_destroy(obj, &descr_type_test);
753 return 1;
754 default:
755 return 0;
756 }
757}
758
759/*
760 * fixup_free is called when:
761 * - an active object is freed
762 */
763static int __init fixup_free(void *addr, enum debug_obj_state state)
764{
765 struct self_test *obj = addr;
766
767 switch (state) {
768 case ODEBUG_STATE_ACTIVE:
769 debug_object_deactivate(obj, &descr_type_test);
770 debug_object_free(obj, &descr_type_test);
771 return 1;
772 default:
773 return 0;
774 }
775}
776
777static int
778check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
779{
780 struct debug_bucket *db;
781 struct debug_obj *obj;
782 unsigned long flags;
783 int res = -EINVAL;
784
785 db = get_bucket((unsigned long) addr);
786
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100787 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700788
789 obj = lookup_object(addr, db);
790 if (!obj && state != ODEBUG_STATE_NONE) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700791 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700792 goto out;
793 }
794 if (obj && obj->state != state) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700795 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700796 obj->state, state);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700797 goto out;
798 }
799 if (fixups != debug_objects_fixups) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700800 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700801 fixups, debug_objects_fixups);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700802 goto out;
803 }
804 if (warnings != debug_objects_warnings) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700805 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700806 warnings, debug_objects_warnings);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700807 goto out;
808 }
809 res = 0;
810out:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100811 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700812 if (res)
813 debug_objects_enabled = 0;
814 return res;
815}
816
817static __initdata struct debug_obj_descr descr_type_test = {
818 .name = "selftest",
819 .fixup_init = fixup_init,
820 .fixup_activate = fixup_activate,
821 .fixup_destroy = fixup_destroy,
822 .fixup_free = fixup_free,
823};
824
825static __initdata struct self_test obj = { .static_init = 0 };
826
827static void __init debug_objects_selftest(void)
828{
829 int fixups, oldfixups, warnings, oldwarnings;
830 unsigned long flags;
831
832 local_irq_save(flags);
833
834 fixups = oldfixups = debug_objects_fixups;
835 warnings = oldwarnings = debug_objects_warnings;
836 descr_test = &descr_type_test;
837
838 debug_object_init(&obj, &descr_type_test);
839 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
840 goto out;
841 debug_object_activate(&obj, &descr_type_test);
842 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
843 goto out;
844 debug_object_activate(&obj, &descr_type_test);
845 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
846 goto out;
847 debug_object_deactivate(&obj, &descr_type_test);
848 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
849 goto out;
850 debug_object_destroy(&obj, &descr_type_test);
851 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
852 goto out;
853 debug_object_init(&obj, &descr_type_test);
854 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
855 goto out;
856 debug_object_activate(&obj, &descr_type_test);
857 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
858 goto out;
859 debug_object_deactivate(&obj, &descr_type_test);
860 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
861 goto out;
862 debug_object_free(&obj, &descr_type_test);
863 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
864 goto out;
865
866 obj.static_init = 1;
867 debug_object_activate(&obj, &descr_type_test);
868 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
869 goto out;
870 debug_object_init(&obj, &descr_type_test);
871 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
872 goto out;
873 debug_object_free(&obj, &descr_type_test);
874 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
875 goto out;
876
877#ifdef CONFIG_DEBUG_OBJECTS_FREE
878 debug_object_init(&obj, &descr_type_test);
879 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
880 goto out;
881 debug_object_activate(&obj, &descr_type_test);
882 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
883 goto out;
884 __debug_check_no_obj_freed(&obj, sizeof(obj));
885 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
886 goto out;
887#endif
888 printk(KERN_INFO "ODEBUG: selftest passed\n");
889
890out:
891 debug_objects_fixups = oldfixups;
892 debug_objects_warnings = oldwarnings;
893 descr_test = NULL;
894
895 local_irq_restore(flags);
896}
897#else
898static inline void debug_objects_selftest(void) { }
899#endif
900
901/*
902 * Called during early boot to initialize the hash buckets and link
903 * the static object pool objects into the poll list. After this call
904 * the object tracker is fully operational.
905 */
906void __init debug_objects_early_init(void)
907{
908 int i;
909
910 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100911 raw_spin_lock_init(&obj_hash[i].lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700912
913 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
914 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
915}
916
917/*
Thomas Gleixner1be1cb72009-03-16 18:53:18 +0100918 * Convert the statically allocated objects to dynamic ones:
919 */
920static int debug_objects_replace_static_objects(void)
921{
922 struct debug_bucket *db = obj_hash;
923 struct hlist_node *node, *tmp;
924 struct debug_obj *obj, *new;
925 HLIST_HEAD(objects);
926 int i, cnt = 0;
927
928 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
929 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
930 if (!obj)
931 goto free;
932 hlist_add_head(&obj->node, &objects);
933 }
934
935 /*
936 * When debug_objects_mem_init() is called we know that only
937 * one CPU is up, so disabling interrupts is enough
938 * protection. This avoids the lockdep hell of lock ordering.
939 */
940 local_irq_disable();
941
942 /* Remove the statically allocated objects from the pool */
943 hlist_for_each_entry_safe(obj, node, tmp, &obj_pool, node)
944 hlist_del(&obj->node);
945 /* Move the allocated objects to the pool */
946 hlist_move_list(&objects, &obj_pool);
947
948 /* Replace the active object references */
949 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
950 hlist_move_list(&db->list, &objects);
951
952 hlist_for_each_entry(obj, node, &objects, node) {
953 new = hlist_entry(obj_pool.first, typeof(*obj), node);
954 hlist_del(&new->node);
955 /* copy object data */
956 *new = *obj;
957 hlist_add_head(&new->node, &db->list);
958 cnt++;
959 }
960 }
961
962 printk(KERN_DEBUG "ODEBUG: %d of %d active objects replaced\n", cnt,
963 obj_pool_used);
964 local_irq_enable();
965 return 0;
966free:
967 hlist_for_each_entry_safe(obj, node, tmp, &objects, node) {
968 hlist_del(&obj->node);
969 kmem_cache_free(obj_cache, obj);
970 }
971 return -ENOMEM;
972}
973
974/*
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700975 * Called after the kmem_caches are functional to setup a dedicated
976 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
977 * prevents that the debug code is called on kmem_cache_free() for the
978 * debug tracker objects to avoid recursive calls.
979 */
980void __init debug_objects_mem_init(void)
981{
982 if (!debug_objects_enabled)
983 return;
984
985 obj_cache = kmem_cache_create("debug_objects_cache",
986 sizeof (struct debug_obj), 0,
987 SLAB_DEBUG_OBJECTS, NULL);
988
Thomas Gleixner1be1cb72009-03-16 18:53:18 +0100989 if (!obj_cache || debug_objects_replace_static_objects()) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700990 debug_objects_enabled = 0;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +0100991 if (obj_cache)
992 kmem_cache_destroy(obj_cache);
993 printk(KERN_WARNING "ODEBUG: out of memory.\n");
994 } else
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700995 debug_objects_selftest();
996}