2 * Generic infrastructure for lifetime debugging of objects.
4 * Started by Thomas Gleixner
6 * Copyright (C) 2008, Thomas Gleixner <tglx@linutronix.de>
8 * For licencing details see kernel-base/COPYING
11 #define pr_fmt(fmt) "ODEBUG: " fmt
13 #include <linux/debugobjects.h>
14 #include <linux/interrupt.h>
15 #include <linux/sched.h>
16 #include <linux/sched/task_stack.h>
17 #include <linux/seq_file.h>
18 #include <linux/debugfs.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/kmemleak.h>
23 #define ODEBUG_HASH_BITS 14
24 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
26 #define ODEBUG_POOL_SIZE 1024
27 #define ODEBUG_POOL_MIN_LEVEL 256
28 #define ODEBUG_POOL_PERCPU_SIZE 64
30 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
31 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
32 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
35 struct hlist_head list;
40 * Debug object percpu free list
41 * Access is protected by disabling irq
43 struct debug_percpu_free {
44 struct hlist_head free_objs;
48 static DEFINE_PER_CPU(struct debug_percpu_free, percpu_obj_pool);
50 static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
52 static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
54 static DEFINE_RAW_SPINLOCK(pool_lock);
56 static HLIST_HEAD(obj_pool);
57 static HLIST_HEAD(obj_to_free);
60 * Because of the presence of percpu free pools, obj_pool_free will
61 * under-count those in the percpu free pools. Similarly, obj_pool_used
62 * will over-count those in the percpu free pools. Adjustments will be
63 * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
66 static int obj_pool_min_free = ODEBUG_POOL_SIZE;
67 static int obj_pool_free = ODEBUG_POOL_SIZE;
68 static int obj_pool_used;
69 static int obj_pool_max_used;
70 /* The number of objs on the global free list */
71 static int obj_nr_tofree;
73 static int debug_objects_maxchain __read_mostly;
74 static int __maybe_unused debug_objects_maxchecked __read_mostly;
75 static int debug_objects_fixups __read_mostly;
76 static int debug_objects_warnings __read_mostly;
77 static int debug_objects_enabled __read_mostly
78 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
79 static int debug_objects_pool_size __read_mostly
81 static int debug_objects_pool_min_level __read_mostly
82 = ODEBUG_POOL_MIN_LEVEL;
83 static struct debug_obj_descr *descr_test __read_mostly;
84 static struct kmem_cache *obj_cache __read_mostly;
87 * Track numbers of kmem_cache_alloc()/free() calls done.
89 static int debug_objects_allocated;
90 static int debug_objects_freed;
92 static void free_obj_work(struct work_struct *work);
93 static DECLARE_WORK(debug_obj_work, free_obj_work);
95 static int __init enable_object_debug(char *str)
97 debug_objects_enabled = 1;
101 static int __init disable_object_debug(char *str)
103 debug_objects_enabled = 0;
107 early_param("debug_objects", enable_object_debug);
108 early_param("no_debug_objects", disable_object_debug);
110 static const char *obj_states[ODEBUG_STATE_MAX] = {
111 [ODEBUG_STATE_NONE] = "none",
112 [ODEBUG_STATE_INIT] = "initialized",
113 [ODEBUG_STATE_INACTIVE] = "inactive",
114 [ODEBUG_STATE_ACTIVE] = "active",
115 [ODEBUG_STATE_DESTROYED] = "destroyed",
116 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
119 static void fill_pool(void)
121 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
122 struct debug_obj *new, *obj;
125 if (likely(obj_pool_free >= debug_objects_pool_min_level))
129 * Reuse objs from the global free list; they will be reinitialized
132 while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
133 raw_spin_lock_irqsave(&pool_lock, flags);
135 * Recheck with the lock held as the worker thread might have
136 * won the race and freed the global free list already.
139 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
140 hlist_del(&obj->node);
142 hlist_add_head(&obj->node, &obj_pool);
145 raw_spin_unlock_irqrestore(&pool_lock, flags);
148 if (unlikely(!obj_cache))
151 while (obj_pool_free < debug_objects_pool_min_level) {
153 new = kmem_cache_zalloc(obj_cache, gfp);
157 raw_spin_lock_irqsave(&pool_lock, flags);
158 hlist_add_head(&new->node, &obj_pool);
159 debug_objects_allocated++;
161 raw_spin_unlock_irqrestore(&pool_lock, flags);
166 * Lookup an object in the hash bucket.
168 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
170 struct debug_obj *obj;
173 hlist_for_each_entry(obj, &b->list, node) {
175 if (obj->object == addr)
178 if (cnt > debug_objects_maxchain)
179 debug_objects_maxchain = cnt;
185 * Allocate a new object from the hlist
187 static struct debug_obj *__alloc_object(struct hlist_head *list)
189 struct debug_obj *obj = NULL;
192 obj = hlist_entry(list->first, typeof(*obj), node);
193 hlist_del(&obj->node);
200 * Allocate a new object. If the pool is empty, switch off the debugger.
201 * Must be called with interrupts disabled.
203 static struct debug_obj *
204 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
206 struct debug_percpu_free *percpu_pool;
207 struct debug_obj *obj;
209 if (likely(obj_cache)) {
210 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
211 obj = __alloc_object(&percpu_pool->free_objs);
213 percpu_pool->obj_free--;
218 raw_spin_lock(&pool_lock);
219 obj = __alloc_object(&obj_pool);
222 if (obj_pool_used > obj_pool_max_used)
223 obj_pool_max_used = obj_pool_used;
226 if (obj_pool_free < obj_pool_min_free)
227 obj_pool_min_free = obj_pool_free;
229 raw_spin_unlock(&pool_lock);
235 obj->state = ODEBUG_STATE_NONE;
237 hlist_add_head(&obj->node, &b->list);
243 * workqueue function to free objects.
245 * To reduce contention on the global pool_lock, the actual freeing of
246 * debug objects will be delayed if the pool_lock is busy.
248 static void free_obj_work(struct work_struct *work)
250 struct hlist_node *tmp;
251 struct debug_obj *obj;
255 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
259 * The objs on the pool list might be allocated before the work is
260 * run, so recheck if pool list it full or not, if not fill pool
261 * list from the global free list
263 while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
264 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
265 hlist_del(&obj->node);
266 hlist_add_head(&obj->node, &obj_pool);
272 * Pool list is already full and there are still objs on the free
273 * list. Move remaining free objs to a temporary list to free the
274 * memory outside the pool_lock held region.
277 hlist_move_list(&obj_to_free, &tofree);
278 debug_objects_freed += obj_nr_tofree;
281 raw_spin_unlock_irqrestore(&pool_lock, flags);
283 hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
284 hlist_del(&obj->node);
285 kmem_cache_free(obj_cache, obj);
289 static bool __free_object(struct debug_obj *obj)
293 struct debug_percpu_free *percpu_pool;
295 local_irq_save(flags);
297 * Try to free it into the percpu pool first.
299 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
300 if (obj_cache && percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
301 hlist_add_head(&obj->node, &percpu_pool->free_objs);
302 percpu_pool->obj_free++;
303 local_irq_restore(flags);
307 raw_spin_lock(&pool_lock);
308 work = (obj_pool_free > debug_objects_pool_size) && obj_cache;
313 hlist_add_head(&obj->node, &obj_to_free);
316 hlist_add_head(&obj->node, &obj_pool);
318 raw_spin_unlock(&pool_lock);
319 local_irq_restore(flags);
324 * Put the object back into the pool and schedule work to free objects
327 static void free_object(struct debug_obj *obj)
329 if (__free_object(obj))
330 schedule_work(&debug_obj_work);
334 * We run out of memory. That means we probably have tons of objects
337 static void debug_objects_oom(void)
339 struct debug_bucket *db = obj_hash;
340 struct hlist_node *tmp;
341 HLIST_HEAD(freelist);
342 struct debug_obj *obj;
346 pr_warn("Out of memory. ODEBUG disabled\n");
348 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
349 raw_spin_lock_irqsave(&db->lock, flags);
350 hlist_move_list(&db->list, &freelist);
351 raw_spin_unlock_irqrestore(&db->lock, flags);
354 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
355 hlist_del(&obj->node);
362 * We use the pfn of the address for the hash. That way we can check
363 * for freed objects simply by checking the affected bucket.
365 static struct debug_bucket *get_bucket(unsigned long addr)
369 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
370 return &obj_hash[hash];
373 static void debug_print_object(struct debug_obj *obj, char *msg)
375 struct debug_obj_descr *descr = obj->descr;
378 if (limit < 5 && descr != descr_test) {
379 void *hint = descr->debug_hint ?
380 descr->debug_hint(obj->object) : NULL;
382 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
383 "object type: %s hint: %pS\n",
384 msg, obj_states[obj->state], obj->astate,
387 debug_objects_warnings++;
391 * Try to repair the damage, so we have a better chance to get useful
395 debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
396 void * addr, enum debug_obj_state state)
398 if (fixup && fixup(addr, state)) {
399 debug_objects_fixups++;
405 static void debug_object_is_on_stack(void *addr, int onstack)
413 is_on_stack = object_is_on_stack(addr);
414 if (is_on_stack == onstack)
419 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
420 task_stack_page(current));
422 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
423 task_stack_page(current));
429 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
431 enum debug_obj_state state;
432 struct debug_bucket *db;
433 struct debug_obj *obj;
438 db = get_bucket((unsigned long) addr);
440 raw_spin_lock_irqsave(&db->lock, flags);
442 obj = lookup_object(addr, db);
444 obj = alloc_object(addr, db, descr);
446 debug_objects_enabled = 0;
447 raw_spin_unlock_irqrestore(&db->lock, flags);
451 debug_object_is_on_stack(addr, onstack);
454 switch (obj->state) {
455 case ODEBUG_STATE_NONE:
456 case ODEBUG_STATE_INIT:
457 case ODEBUG_STATE_INACTIVE:
458 obj->state = ODEBUG_STATE_INIT;
461 case ODEBUG_STATE_ACTIVE:
462 debug_print_object(obj, "init");
464 raw_spin_unlock_irqrestore(&db->lock, flags);
465 debug_object_fixup(descr->fixup_init, addr, state);
468 case ODEBUG_STATE_DESTROYED:
469 debug_print_object(obj, "init");
475 raw_spin_unlock_irqrestore(&db->lock, flags);
479 * debug_object_init - debug checks when an object is initialized
480 * @addr: address of the object
481 * @descr: pointer to an object specific debug description structure
483 void debug_object_init(void *addr, struct debug_obj_descr *descr)
485 if (!debug_objects_enabled)
488 __debug_object_init(addr, descr, 0);
490 EXPORT_SYMBOL_GPL(debug_object_init);
493 * debug_object_init_on_stack - debug checks when an object on stack is
495 * @addr: address of the object
496 * @descr: pointer to an object specific debug description structure
498 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
500 if (!debug_objects_enabled)
503 __debug_object_init(addr, descr, 1);
505 EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
508 * debug_object_activate - debug checks when an object is activated
509 * @addr: address of the object
510 * @descr: pointer to an object specific debug description structure
511 * Returns 0 for success, -EINVAL for check failed.
513 int debug_object_activate(void *addr, struct debug_obj_descr *descr)
515 enum debug_obj_state state;
516 struct debug_bucket *db;
517 struct debug_obj *obj;
520 struct debug_obj o = { .object = addr,
521 .state = ODEBUG_STATE_NOTAVAILABLE,
524 if (!debug_objects_enabled)
527 db = get_bucket((unsigned long) addr);
529 raw_spin_lock_irqsave(&db->lock, flags);
531 obj = lookup_object(addr, db);
533 switch (obj->state) {
534 case ODEBUG_STATE_INIT:
535 case ODEBUG_STATE_INACTIVE:
536 obj->state = ODEBUG_STATE_ACTIVE;
540 case ODEBUG_STATE_ACTIVE:
541 debug_print_object(obj, "activate");
543 raw_spin_unlock_irqrestore(&db->lock, flags);
544 ret = debug_object_fixup(descr->fixup_activate, addr, state);
545 return ret ? 0 : -EINVAL;
547 case ODEBUG_STATE_DESTROYED:
548 debug_print_object(obj, "activate");
555 raw_spin_unlock_irqrestore(&db->lock, flags);
559 raw_spin_unlock_irqrestore(&db->lock, flags);
561 * We are here when a static object is activated. We
562 * let the type specific code confirm whether this is
563 * true or not. if true, we just make sure that the
564 * static object is tracked in the object tracker. If
565 * not, this must be a bug, so we try to fix it up.
567 if (descr->is_static_object && descr->is_static_object(addr)) {
568 /* track this static object */
569 debug_object_init(addr, descr);
570 debug_object_activate(addr, descr);
572 debug_print_object(&o, "activate");
573 ret = debug_object_fixup(descr->fixup_activate, addr,
574 ODEBUG_STATE_NOTAVAILABLE);
575 return ret ? 0 : -EINVAL;
579 EXPORT_SYMBOL_GPL(debug_object_activate);
582 * debug_object_deactivate - debug checks when an object is deactivated
583 * @addr: address of the object
584 * @descr: pointer to an object specific debug description structure
586 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
588 struct debug_bucket *db;
589 struct debug_obj *obj;
592 if (!debug_objects_enabled)
595 db = get_bucket((unsigned long) addr);
597 raw_spin_lock_irqsave(&db->lock, flags);
599 obj = lookup_object(addr, db);
601 switch (obj->state) {
602 case ODEBUG_STATE_INIT:
603 case ODEBUG_STATE_INACTIVE:
604 case ODEBUG_STATE_ACTIVE:
606 obj->state = ODEBUG_STATE_INACTIVE;
608 debug_print_object(obj, "deactivate");
611 case ODEBUG_STATE_DESTROYED:
612 debug_print_object(obj, "deactivate");
618 struct debug_obj o = { .object = addr,
619 .state = ODEBUG_STATE_NOTAVAILABLE,
622 debug_print_object(&o, "deactivate");
625 raw_spin_unlock_irqrestore(&db->lock, flags);
627 EXPORT_SYMBOL_GPL(debug_object_deactivate);
630 * debug_object_destroy - debug checks when an object is destroyed
631 * @addr: address of the object
632 * @descr: pointer to an object specific debug description structure
634 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
636 enum debug_obj_state state;
637 struct debug_bucket *db;
638 struct debug_obj *obj;
641 if (!debug_objects_enabled)
644 db = get_bucket((unsigned long) addr);
646 raw_spin_lock_irqsave(&db->lock, flags);
648 obj = lookup_object(addr, db);
652 switch (obj->state) {
653 case ODEBUG_STATE_NONE:
654 case ODEBUG_STATE_INIT:
655 case ODEBUG_STATE_INACTIVE:
656 obj->state = ODEBUG_STATE_DESTROYED;
658 case ODEBUG_STATE_ACTIVE:
659 debug_print_object(obj, "destroy");
661 raw_spin_unlock_irqrestore(&db->lock, flags);
662 debug_object_fixup(descr->fixup_destroy, addr, state);
665 case ODEBUG_STATE_DESTROYED:
666 debug_print_object(obj, "destroy");
672 raw_spin_unlock_irqrestore(&db->lock, flags);
674 EXPORT_SYMBOL_GPL(debug_object_destroy);
677 * debug_object_free - debug checks when an object is freed
678 * @addr: address of the object
679 * @descr: pointer to an object specific debug description structure
681 void debug_object_free(void *addr, struct debug_obj_descr *descr)
683 enum debug_obj_state state;
684 struct debug_bucket *db;
685 struct debug_obj *obj;
688 if (!debug_objects_enabled)
691 db = get_bucket((unsigned long) addr);
693 raw_spin_lock_irqsave(&db->lock, flags);
695 obj = lookup_object(addr, db);
699 switch (obj->state) {
700 case ODEBUG_STATE_ACTIVE:
701 debug_print_object(obj, "free");
703 raw_spin_unlock_irqrestore(&db->lock, flags);
704 debug_object_fixup(descr->fixup_free, addr, state);
707 hlist_del(&obj->node);
708 raw_spin_unlock_irqrestore(&db->lock, flags);
713 raw_spin_unlock_irqrestore(&db->lock, flags);
715 EXPORT_SYMBOL_GPL(debug_object_free);
718 * debug_object_assert_init - debug checks when object should be init-ed
719 * @addr: address of the object
720 * @descr: pointer to an object specific debug description structure
722 void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
724 struct debug_bucket *db;
725 struct debug_obj *obj;
728 if (!debug_objects_enabled)
731 db = get_bucket((unsigned long) addr);
733 raw_spin_lock_irqsave(&db->lock, flags);
735 obj = lookup_object(addr, db);
737 struct debug_obj o = { .object = addr,
738 .state = ODEBUG_STATE_NOTAVAILABLE,
741 raw_spin_unlock_irqrestore(&db->lock, flags);
743 * Maybe the object is static, and we let the type specific
744 * code confirm. Track this static object if true, else invoke
747 if (descr->is_static_object && descr->is_static_object(addr)) {
748 /* Track this static object */
749 debug_object_init(addr, descr);
751 debug_print_object(&o, "assert_init");
752 debug_object_fixup(descr->fixup_assert_init, addr,
753 ODEBUG_STATE_NOTAVAILABLE);
758 raw_spin_unlock_irqrestore(&db->lock, flags);
760 EXPORT_SYMBOL_GPL(debug_object_assert_init);
763 * debug_object_active_state - debug checks object usage state machine
764 * @addr: address of the object
765 * @descr: pointer to an object specific debug description structure
766 * @expect: expected state
767 * @next: state to move to if expected state is found
770 debug_object_active_state(void *addr, struct debug_obj_descr *descr,
771 unsigned int expect, unsigned int next)
773 struct debug_bucket *db;
774 struct debug_obj *obj;
777 if (!debug_objects_enabled)
780 db = get_bucket((unsigned long) addr);
782 raw_spin_lock_irqsave(&db->lock, flags);
784 obj = lookup_object(addr, db);
786 switch (obj->state) {
787 case ODEBUG_STATE_ACTIVE:
788 if (obj->astate == expect)
791 debug_print_object(obj, "active_state");
795 debug_print_object(obj, "active_state");
799 struct debug_obj o = { .object = addr,
800 .state = ODEBUG_STATE_NOTAVAILABLE,
803 debug_print_object(&o, "active_state");
806 raw_spin_unlock_irqrestore(&db->lock, flags);
808 EXPORT_SYMBOL_GPL(debug_object_active_state);
810 #ifdef CONFIG_DEBUG_OBJECTS_FREE
811 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
813 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
814 struct debug_obj_descr *descr;
815 enum debug_obj_state state;
816 struct debug_bucket *db;
817 struct hlist_node *tmp;
818 struct debug_obj *obj;
819 int cnt, objs_checked = 0;
822 saddr = (unsigned long) address;
823 eaddr = saddr + size;
824 paddr = saddr & ODEBUG_CHUNK_MASK;
825 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
826 chunks >>= ODEBUG_CHUNK_SHIFT;
828 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
829 db = get_bucket(paddr);
833 raw_spin_lock_irqsave(&db->lock, flags);
834 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
836 oaddr = (unsigned long) obj->object;
837 if (oaddr < saddr || oaddr >= eaddr)
840 switch (obj->state) {
841 case ODEBUG_STATE_ACTIVE:
842 debug_print_object(obj, "free");
845 raw_spin_unlock_irqrestore(&db->lock, flags);
846 debug_object_fixup(descr->fixup_free,
847 (void *) oaddr, state);
850 hlist_del(&obj->node);
851 work |= __free_object(obj);
855 raw_spin_unlock_irqrestore(&db->lock, flags);
857 if (cnt > debug_objects_maxchain)
858 debug_objects_maxchain = cnt;
863 if (objs_checked > debug_objects_maxchecked)
864 debug_objects_maxchecked = objs_checked;
866 /* Schedule work to actually kmem_cache_free() objects */
868 schedule_work(&debug_obj_work);
871 void debug_check_no_obj_freed(const void *address, unsigned long size)
873 if (debug_objects_enabled)
874 __debug_check_no_obj_freed(address, size);
878 #ifdef CONFIG_DEBUG_FS
880 static int debug_stats_show(struct seq_file *m, void *v)
882 int cpu, obj_percpu_free = 0;
884 for_each_possible_cpu(cpu)
885 obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
887 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
888 seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked);
889 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
890 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
891 seq_printf(m, "pool_free :%d\n", obj_pool_free + obj_percpu_free);
892 seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
893 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
894 seq_printf(m, "pool_used :%d\n", obj_pool_used - obj_percpu_free);
895 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
896 seq_printf(m, "on_free_list :%d\n", obj_nr_tofree);
897 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
898 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
902 static int debug_stats_open(struct inode *inode, struct file *filp)
904 return single_open(filp, debug_stats_show, NULL);
907 static const struct file_operations debug_stats_fops = {
908 .open = debug_stats_open,
911 .release = single_release,
914 static int __init debug_objects_init_debugfs(void)
916 struct dentry *dbgdir;
918 if (!debug_objects_enabled)
921 dbgdir = debugfs_create_dir("debug_objects", NULL);
923 debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
927 __initcall(debug_objects_init_debugfs);
930 static inline void debug_objects_init_debugfs(void) { }
933 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
935 /* Random data structure for the self test */
937 unsigned long dummy1[6];
939 unsigned long dummy2[3];
942 static __initdata struct debug_obj_descr descr_type_test;
944 static bool __init is_static_object(void *addr)
946 struct self_test *obj = addr;
948 return obj->static_init;
952 * fixup_init is called when:
953 * - an active object is initialized
955 static bool __init fixup_init(void *addr, enum debug_obj_state state)
957 struct self_test *obj = addr;
960 case ODEBUG_STATE_ACTIVE:
961 debug_object_deactivate(obj, &descr_type_test);
962 debug_object_init(obj, &descr_type_test);
970 * fixup_activate is called when:
971 * - an active object is activated
972 * - an unknown non-static object is activated
974 static bool __init fixup_activate(void *addr, enum debug_obj_state state)
976 struct self_test *obj = addr;
979 case ODEBUG_STATE_NOTAVAILABLE:
981 case ODEBUG_STATE_ACTIVE:
982 debug_object_deactivate(obj, &descr_type_test);
983 debug_object_activate(obj, &descr_type_test);
992 * fixup_destroy is called when:
993 * - an active object is destroyed
995 static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
997 struct self_test *obj = addr;
1000 case ODEBUG_STATE_ACTIVE:
1001 debug_object_deactivate(obj, &descr_type_test);
1002 debug_object_destroy(obj, &descr_type_test);
1010 * fixup_free is called when:
1011 * - an active object is freed
1013 static bool __init fixup_free(void *addr, enum debug_obj_state state)
1015 struct self_test *obj = addr;
1018 case ODEBUG_STATE_ACTIVE:
1019 debug_object_deactivate(obj, &descr_type_test);
1020 debug_object_free(obj, &descr_type_test);
1028 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
1030 struct debug_bucket *db;
1031 struct debug_obj *obj;
1032 unsigned long flags;
1035 db = get_bucket((unsigned long) addr);
1037 raw_spin_lock_irqsave(&db->lock, flags);
1039 obj = lookup_object(addr, db);
1040 if (!obj && state != ODEBUG_STATE_NONE) {
1041 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
1044 if (obj && obj->state != state) {
1045 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
1049 if (fixups != debug_objects_fixups) {
1050 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
1051 fixups, debug_objects_fixups);
1054 if (warnings != debug_objects_warnings) {
1055 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
1056 warnings, debug_objects_warnings);
1061 raw_spin_unlock_irqrestore(&db->lock, flags);
1063 debug_objects_enabled = 0;
1067 static __initdata struct debug_obj_descr descr_type_test = {
1069 .is_static_object = is_static_object,
1070 .fixup_init = fixup_init,
1071 .fixup_activate = fixup_activate,
1072 .fixup_destroy = fixup_destroy,
1073 .fixup_free = fixup_free,
1076 static __initdata struct self_test obj = { .static_init = 0 };
1078 static void __init debug_objects_selftest(void)
1080 int fixups, oldfixups, warnings, oldwarnings;
1081 unsigned long flags;
1083 local_irq_save(flags);
1085 fixups = oldfixups = debug_objects_fixups;
1086 warnings = oldwarnings = debug_objects_warnings;
1087 descr_test = &descr_type_test;
1089 debug_object_init(&obj, &descr_type_test);
1090 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1092 debug_object_activate(&obj, &descr_type_test);
1093 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1095 debug_object_activate(&obj, &descr_type_test);
1096 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1098 debug_object_deactivate(&obj, &descr_type_test);
1099 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1101 debug_object_destroy(&obj, &descr_type_test);
1102 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1104 debug_object_init(&obj, &descr_type_test);
1105 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1107 debug_object_activate(&obj, &descr_type_test);
1108 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1110 debug_object_deactivate(&obj, &descr_type_test);
1111 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1113 debug_object_free(&obj, &descr_type_test);
1114 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1117 obj.static_init = 1;
1118 debug_object_activate(&obj, &descr_type_test);
1119 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1121 debug_object_init(&obj, &descr_type_test);
1122 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1124 debug_object_free(&obj, &descr_type_test);
1125 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1128 #ifdef CONFIG_DEBUG_OBJECTS_FREE
1129 debug_object_init(&obj, &descr_type_test);
1130 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1132 debug_object_activate(&obj, &descr_type_test);
1133 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1135 __debug_check_no_obj_freed(&obj, sizeof(obj));
1136 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1139 pr_info("selftest passed\n");
1142 debug_objects_fixups = oldfixups;
1143 debug_objects_warnings = oldwarnings;
1146 local_irq_restore(flags);
1149 static inline void debug_objects_selftest(void) { }
1153 * Called during early boot to initialize the hash buckets and link
1154 * the static object pool objects into the poll list. After this call
1155 * the object tracker is fully operational.
1157 void __init debug_objects_early_init(void)
1161 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1162 raw_spin_lock_init(&obj_hash[i].lock);
1164 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1165 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1169 * Convert the statically allocated objects to dynamic ones:
1171 static int __init debug_objects_replace_static_objects(void)
1173 struct debug_bucket *db = obj_hash;
1174 struct hlist_node *tmp;
1175 struct debug_obj *obj, *new;
1176 HLIST_HEAD(objects);
1179 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1180 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1183 hlist_add_head(&obj->node, &objects);
1187 * debug_objects_mem_init() is now called early that only one CPU is up
1188 * and interrupts have been disabled, so it is safe to replace the
1189 * active object references.
1192 /* Remove the statically allocated objects from the pool */
1193 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1194 hlist_del(&obj->node);
1195 /* Move the allocated objects to the pool */
1196 hlist_move_list(&objects, &obj_pool);
1198 /* Replace the active object references */
1199 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1200 hlist_move_list(&db->list, &objects);
1202 hlist_for_each_entry(obj, &objects, node) {
1203 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1204 hlist_del(&new->node);
1205 /* copy object data */
1207 hlist_add_head(&new->node, &db->list);
1212 pr_debug("%d of %d active objects replaced\n",
1213 cnt, obj_pool_used);
1216 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1217 hlist_del(&obj->node);
1218 kmem_cache_free(obj_cache, obj);
1224 * Called after the kmem_caches are functional to setup a dedicated
1225 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1226 * prevents that the debug code is called on kmem_cache_free() for the
1227 * debug tracker objects to avoid recursive calls.
1229 void __init debug_objects_mem_init(void)
1233 if (!debug_objects_enabled)
1237 * Initialize the percpu object pools
1239 * Initialization is not strictly necessary, but was done for
1242 for_each_possible_cpu(cpu)
1243 INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
1245 obj_cache = kmem_cache_create("debug_objects_cache",
1246 sizeof (struct debug_obj), 0,
1247 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
1250 if (!obj_cache || debug_objects_replace_static_objects()) {
1251 debug_objects_enabled = 0;
1252 kmem_cache_destroy(obj_cache);
1253 pr_warn("out of memory.\n");
1255 debug_objects_selftest();