xref: /linux/lib/debugobjects.c (revision 3e85fd614c7b6bb7f33bb04a0dcb5a3bfca4c0fe)
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/sched.h>
13 #include <linux/seq_file.h>
14 #include <linux/debugfs.h>
15 #include <linux/hash.h>
16 
17 #define ODEBUG_HASH_BITS	14
18 #define ODEBUG_HASH_SIZE	(1 << ODEBUG_HASH_BITS)
19 
20 #define ODEBUG_POOL_SIZE	512
21 #define ODEBUG_POOL_MIN_LEVEL	256
22 
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))
26 
27 struct debug_bucket {
28 	struct hlist_head	list;
29 	raw_spinlock_t		lock;
30 };
31 
32 static struct debug_bucket	obj_hash[ODEBUG_HASH_SIZE];
33 
34 static struct debug_obj		obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
35 
36 static DEFINE_RAW_SPINLOCK(pool_lock);
37 
38 static HLIST_HEAD(obj_pool);
39 
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;
45 
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;
51 
52 static struct debug_obj_descr	*descr_test  __read_mostly;
53 
54 static void free_obj_work(struct work_struct *work);
55 static DECLARE_WORK(debug_obj_work, free_obj_work);
56 
57 static int __init enable_object_debug(char *str)
58 {
59 	debug_objects_enabled = 1;
60 	return 0;
61 }
62 
63 static int __init disable_object_debug(char *str)
64 {
65 	debug_objects_enabled = 0;
66 	return 0;
67 }
68 
69 early_param("debug_objects", enable_object_debug);
70 early_param("no_debug_objects", disable_object_debug);
71 
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",
79 };
80 
81 static int fill_pool(void)
82 {
83 	gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
84 	struct debug_obj *new;
85 	unsigned long flags;
86 
87 	if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
88 		return obj_pool_free;
89 
90 	if (unlikely(!obj_cache))
91 		return obj_pool_free;
92 
93 	while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
94 
95 		new = kmem_cache_zalloc(obj_cache, gfp);
96 		if (!new)
97 			return obj_pool_free;
98 
99 		raw_spin_lock_irqsave(&pool_lock, flags);
100 		hlist_add_head(&new->node, &obj_pool);
101 		obj_pool_free++;
102 		raw_spin_unlock_irqrestore(&pool_lock, flags);
103 	}
104 	return obj_pool_free;
105 }
106 
107 /*
108  * Lookup an object in the hash bucket.
109  */
110 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
111 {
112 	struct hlist_node *node;
113 	struct debug_obj *obj;
114 	int cnt = 0;
115 
116 	hlist_for_each_entry(obj, node, &b->list, node) {
117 		cnt++;
118 		if (obj->object == addr)
119 			return obj;
120 	}
121 	if (cnt > debug_objects_maxchain)
122 		debug_objects_maxchain = cnt;
123 
124 	return NULL;
125 }
126 
127 /*
128  * Allocate a new object. If the pool is empty, switch off the debugger.
129  * Must be called with interrupts disabled.
130  */
131 static struct debug_obj *
132 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
133 {
134 	struct debug_obj *obj = NULL;
135 
136 	raw_spin_lock(&pool_lock);
137 	if (obj_pool.first) {
138 		obj	    = hlist_entry(obj_pool.first, typeof(*obj), node);
139 
140 		obj->object = addr;
141 		obj->descr  = descr;
142 		obj->state  = ODEBUG_STATE_NONE;
143 		hlist_del(&obj->node);
144 
145 		hlist_add_head(&obj->node, &b->list);
146 
147 		obj_pool_used++;
148 		if (obj_pool_used > obj_pool_max_used)
149 			obj_pool_max_used = obj_pool_used;
150 
151 		obj_pool_free--;
152 		if (obj_pool_free < obj_pool_min_free)
153 			obj_pool_min_free = obj_pool_free;
154 	}
155 	raw_spin_unlock(&pool_lock);
156 
157 	return obj;
158 }
159 
160 /*
161  * workqueue function to free objects.
162  */
163 static void free_obj_work(struct work_struct *work)
164 {
165 	struct debug_obj *obj;
166 	unsigned long flags;
167 
168 	raw_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--;
173 		/*
174 		 * We release pool_lock across kmem_cache_free() to
175 		 * avoid contention on pool_lock.
176 		 */
177 		raw_spin_unlock_irqrestore(&pool_lock, flags);
178 		kmem_cache_free(obj_cache, obj);
179 		raw_spin_lock_irqsave(&pool_lock, flags);
180 	}
181 	raw_spin_unlock_irqrestore(&pool_lock, flags);
182 }
183 
184 /*
185  * Put the object back into the pool and schedule work to free objects
186  * if necessary.
187  */
188 static void free_object(struct debug_obj *obj)
189 {
190 	unsigned long flags;
191 	int sched = 0;
192 
193 	raw_spin_lock_irqsave(&pool_lock, flags);
194 	/*
195 	 * schedule work when the pool is filled and the cache is
196 	 * initialized:
197 	 */
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 	raw_spin_unlock_irqrestore(&pool_lock, flags);
204 	if (sched)
205 		schedule_work(&debug_obj_work);
206 }
207 
208 /*
209  * We run out of memory. That means we probably have tons of objects
210  * allocated.
211  */
212 static void debug_objects_oom(void)
213 {
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;
220 
221 	printk(KERN_WARNING "ODEBUG: Out of memory. ODEBUG disabled\n");
222 
223 	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
224 		raw_spin_lock_irqsave(&db->lock, flags);
225 		hlist_move_list(&db->list, &freelist);
226 		raw_spin_unlock_irqrestore(&db->lock, flags);
227 
228 		/* Now free them */
229 		hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
230 			hlist_del(&obj->node);
231 			free_object(obj);
232 		}
233 	}
234 }
235 
236 /*
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.
239  */
240 static struct debug_bucket *get_bucket(unsigned long addr)
241 {
242 	unsigned long hash;
243 
244 	hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
245 	return &obj_hash[hash];
246 }
247 
248 static void debug_print_object(struct debug_obj *obj, char *msg)
249 {
250 	static int limit;
251 
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);
256 	}
257 	debug_objects_warnings++;
258 }
259 
260 /*
261  * Try to repair the damage, so we have a better chance to get useful
262  * debug output.
263  */
264 static void
265 debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state),
266 		   void * addr, enum debug_obj_state state)
267 {
268 	if (fixup)
269 		debug_objects_fixups += fixup(addr, state);
270 }
271 
272 static void debug_object_is_on_stack(void *addr, int onstack)
273 {
274 	int is_on_stack;
275 	static int limit;
276 
277 	if (limit > 4)
278 		return;
279 
280 	is_on_stack = object_is_on_stack(addr);
281 	if (is_on_stack == onstack)
282 		return;
283 
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);
292 }
293 
294 static void
295 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
296 {
297 	enum debug_obj_state state;
298 	struct debug_bucket *db;
299 	struct debug_obj *obj;
300 	unsigned long flags;
301 
302 	fill_pool();
303 
304 	db = get_bucket((unsigned long) addr);
305 
306 	raw_spin_lock_irqsave(&db->lock, flags);
307 
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 			raw_spin_unlock_irqrestore(&db->lock, flags);
314 			debug_objects_oom();
315 			return;
316 		}
317 		debug_object_is_on_stack(addr, onstack);
318 	}
319 
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;
326 
327 	case ODEBUG_STATE_ACTIVE:
328 		debug_print_object(obj, "init");
329 		state = obj->state;
330 		raw_spin_unlock_irqrestore(&db->lock, flags);
331 		debug_object_fixup(descr->fixup_init, addr, state);
332 		return;
333 
334 	case ODEBUG_STATE_DESTROYED:
335 		debug_print_object(obj, "init");
336 		break;
337 	default:
338 		break;
339 	}
340 
341 	raw_spin_unlock_irqrestore(&db->lock, flags);
342 }
343 
344 /**
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
348  */
349 void debug_object_init(void *addr, struct debug_obj_descr *descr)
350 {
351 	if (!debug_objects_enabled)
352 		return;
353 
354 	__debug_object_init(addr, descr, 0);
355 }
356 
357 /**
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
362  */
363 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
364 {
365 	if (!debug_objects_enabled)
366 		return;
367 
368 	__debug_object_init(addr, descr, 1);
369 }
370 
371 /**
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
375  */
376 void debug_object_activate(void *addr, struct debug_obj_descr *descr)
377 {
378 	enum debug_obj_state state;
379 	struct debug_bucket *db;
380 	struct debug_obj *obj;
381 	unsigned long flags;
382 
383 	if (!debug_objects_enabled)
384 		return;
385 
386 	db = get_bucket((unsigned long) addr);
387 
388 	raw_spin_lock_irqsave(&db->lock, flags);
389 
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;
397 
398 		case ODEBUG_STATE_ACTIVE:
399 			debug_print_object(obj, "activate");
400 			state = obj->state;
401 			raw_spin_unlock_irqrestore(&db->lock, flags);
402 			debug_object_fixup(descr->fixup_activate, addr, state);
403 			return;
404 
405 		case ODEBUG_STATE_DESTROYED:
406 			debug_print_object(obj, "activate");
407 			break;
408 		default:
409 			break;
410 		}
411 		raw_spin_unlock_irqrestore(&db->lock, flags);
412 		return;
413 	}
414 
415 	raw_spin_unlock_irqrestore(&db->lock, flags);
416 	/*
417 	 * This happens when a static object is activated. We
418 	 * let the type specific code decide whether this is
419 	 * true or not.
420 	 */
421 	debug_object_fixup(descr->fixup_activate, addr,
422 			   ODEBUG_STATE_NOTAVAILABLE);
423 }
424 
425 /**
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
429  */
430 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
431 {
432 	struct debug_bucket *db;
433 	struct debug_obj *obj;
434 	unsigned long flags;
435 
436 	if (!debug_objects_enabled)
437 		return;
438 
439 	db = get_bucket((unsigned long) addr);
440 
441 	raw_spin_lock_irqsave(&db->lock, flags);
442 
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;
451 
452 		case ODEBUG_STATE_DESTROYED:
453 			debug_print_object(obj, "deactivate");
454 			break;
455 		default:
456 			break;
457 		}
458 	} else {
459 		struct debug_obj o = { .object = addr,
460 				       .state = ODEBUG_STATE_NOTAVAILABLE,
461 				       .descr = descr };
462 
463 		debug_print_object(&o, "deactivate");
464 	}
465 
466 	raw_spin_unlock_irqrestore(&db->lock, flags);
467 }
468 
469 /**
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
473  */
474 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
475 {
476 	enum debug_obj_state state;
477 	struct debug_bucket *db;
478 	struct debug_obj *obj;
479 	unsigned long flags;
480 
481 	if (!debug_objects_enabled)
482 		return;
483 
484 	db = get_bucket((unsigned long) addr);
485 
486 	raw_spin_lock_irqsave(&db->lock, flags);
487 
488 	obj = lookup_object(addr, db);
489 	if (!obj)
490 		goto out_unlock;
491 
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 		raw_spin_unlock_irqrestore(&db->lock, flags);
502 		debug_object_fixup(descr->fixup_destroy, addr, state);
503 		return;
504 
505 	case ODEBUG_STATE_DESTROYED:
506 		debug_print_object(obj, "destroy");
507 		break;
508 	default:
509 		break;
510 	}
511 out_unlock:
512 	raw_spin_unlock_irqrestore(&db->lock, flags);
513 }
514 
515 /**
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
519  */
520 void debug_object_free(void *addr, struct debug_obj_descr *descr)
521 {
522 	enum debug_obj_state state;
523 	struct debug_bucket *db;
524 	struct debug_obj *obj;
525 	unsigned long flags;
526 
527 	if (!debug_objects_enabled)
528 		return;
529 
530 	db = get_bucket((unsigned long) addr);
531 
532 	raw_spin_lock_irqsave(&db->lock, flags);
533 
534 	obj = lookup_object(addr, db);
535 	if (!obj)
536 		goto out_unlock;
537 
538 	switch (obj->state) {
539 	case ODEBUG_STATE_ACTIVE:
540 		debug_print_object(obj, "free");
541 		state = obj->state;
542 		raw_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 		raw_spin_unlock_irqrestore(&db->lock, flags);
548 		free_object(obj);
549 		return;
550 	}
551 out_unlock:
552 	raw_spin_unlock_irqrestore(&db->lock, flags);
553 }
554 
555 #ifdef CONFIG_DEBUG_OBJECTS_FREE
556 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
557 {
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;
566 
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;
572 
573 	for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
574 		db = get_bucket(paddr);
575 
576 repeat:
577 		cnt = 0;
578 		raw_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;
584 
585 			switch (obj->state) {
586 			case ODEBUG_STATE_ACTIVE:
587 				debug_print_object(obj, "free");
588 				descr = obj->descr;
589 				state = obj->state;
590 				raw_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;
598 			}
599 		}
600 		raw_spin_unlock_irqrestore(&db->lock, flags);
601 
602 		/* Now free them */
603 		hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
604 			hlist_del(&obj->node);
605 			free_object(obj);
606 		}
607 
608 		if (cnt > debug_objects_maxchain)
609 			debug_objects_maxchain = cnt;
610 	}
611 }
612 
613 void debug_check_no_obj_freed(const void *address, unsigned long size)
614 {
615 	if (debug_objects_enabled)
616 		__debug_check_no_obj_freed(address, size);
617 }
618 #endif
619 
620 #ifdef CONFIG_DEBUG_FS
621 
622 static int debug_stats_show(struct seq_file *m, void *v)
623 {
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;
632 }
633 
634 static int debug_stats_open(struct inode *inode, struct file *filp)
635 {
636 	return single_open(filp, debug_stats_show, NULL);
637 }
638 
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,
644 };
645 
646 static int __init debug_objects_init_debugfs(void)
647 {
648 	struct dentry *dbgdir, *dbgstats;
649 
650 	if (!debug_objects_enabled)
651 		return 0;
652 
653 	dbgdir = debugfs_create_dir("debug_objects", NULL);
654 	if (!dbgdir)
655 		return -ENOMEM;
656 
657 	dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
658 				       &debug_stats_fops);
659 	if (!dbgstats)
660 		goto err;
661 
662 	return 0;
663 
664 err:
665 	debugfs_remove(dbgdir);
666 
667 	return -ENOMEM;
668 }
669 __initcall(debug_objects_init_debugfs);
670 
671 #else
672 static inline void debug_objects_init_debugfs(void) { }
673 #endif
674 
675 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
676 
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];
682 };
683 
684 static __initdata struct debug_obj_descr descr_type_test;
685 
686 /*
687  * fixup_init is called when:
688  * - an active object is initialized
689  */
690 static int __init fixup_init(void *addr, enum debug_obj_state state)
691 {
692 	struct self_test *obj = addr;
693 
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;
701 	}
702 }
703 
704 /*
705  * fixup_activate is called when:
706  * - an active object is activated
707  * - an unknown object is activated (might be a statically initialized object)
708  */
709 static int __init fixup_activate(void *addr, enum debug_obj_state state)
710 {
711 	struct self_test *obj = addr;
712 
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);
718 			/*
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.
723 			 */
724 			return 1;
725 		} else {
726 			/* Real code needs to emit a warning here */
727 		}
728 		return 0;
729 
730 	case ODEBUG_STATE_ACTIVE:
731 		debug_object_deactivate(obj, &descr_type_test);
732 		debug_object_activate(obj, &descr_type_test);
733 		return 1;
734 
735 	default:
736 		return 0;
737 	}
738 }
739 
740 /*
741  * fixup_destroy is called when:
742  * - an active object is destroyed
743  */
744 static int __init fixup_destroy(void *addr, enum debug_obj_state state)
745 {
746 	struct self_test *obj = addr;
747 
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;
755 	}
756 }
757 
758 /*
759  * fixup_free is called when:
760  * - an active object is freed
761  */
762 static int __init fixup_free(void *addr, enum debug_obj_state state)
763 {
764 	struct self_test *obj = addr;
765 
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;
773 	}
774 }
775 
776 static int
777 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
778 {
779 	struct debug_bucket *db;
780 	struct debug_obj *obj;
781 	unsigned long flags;
782 	int res = -EINVAL;
783 
784 	db = get_bucket((unsigned long) addr);
785 
786 	raw_spin_lock_irqsave(&db->lock, flags);
787 
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;
792 	}
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;
797 	}
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;
802 	}
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;
807 	}
808 	res = 0;
809 out:
810 	raw_spin_unlock_irqrestore(&db->lock, flags);
811 	if (res)
812 		debug_objects_enabled = 0;
813 	return res;
814 }
815 
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,
822 };
823 
824 static __initdata struct self_test obj = { .static_init = 0 };
825 
826 static void __init debug_objects_selftest(void)
827 {
828 	int fixups, oldfixups, warnings, oldwarnings;
829 	unsigned long flags;
830 
831 	local_irq_save(flags);
832 
833 	fixups = oldfixups = debug_objects_fixups;
834 	warnings = oldwarnings = debug_objects_warnings;
835 	descr_test = &descr_type_test;
836 
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;
864 
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;
875 
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");
888 
889 out:
890 	debug_objects_fixups = oldfixups;
891 	debug_objects_warnings = oldwarnings;
892 	descr_test = NULL;
893 
894 	local_irq_restore(flags);
895 }
896 #else
897 static inline void debug_objects_selftest(void) { }
898 #endif
899 
900 /*
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.
904  */
905 void __init debug_objects_early_init(void)
906 {
907 	int i;
908 
909 	for (i = 0; i < ODEBUG_HASH_SIZE; i++)
910 		raw_spin_lock_init(&obj_hash[i].lock);
911 
912 	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
913 		hlist_add_head(&obj_static_pool[i].node, &obj_pool);
914 }
915 
916 /*
917  * Convert the statically allocated objects to dynamic ones:
918  */
919 static int debug_objects_replace_static_objects(void)
920 {
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;
926 
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);
932 	}
933 
934 	/*
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.
938 	 */
939 	local_irq_disable();
940 
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);
946 
947 	/* Replace the active object references */
948 	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
949 		hlist_move_list(&db->list, &objects);
950 
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++;
958 		}
959 	}
960 
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);
969 	}
970 	return -ENOMEM;
971 }
972 
973 /*
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.
978  */
979 void __init debug_objects_mem_init(void)
980 {
981 	if (!debug_objects_enabled)
982 		return;
983 
984 	obj_cache = kmem_cache_create("debug_objects_cache",
985 				      sizeof (struct debug_obj), 0,
986 				      SLAB_DEBUG_OBJECTS, NULL);
987 
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();
995 }
996