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);
199 static struct debug_obj *
200 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
202 struct debug_percpu_free *percpu_pool;
203 struct debug_obj *obj;
205 if (likely(obj_cache)) {
206 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
207 obj = __alloc_object(&percpu_pool->free_objs);
209 percpu_pool->obj_free--;
214 raw_spin_lock(&pool_lock);
215 obj = __alloc_object(&obj_pool);
218 if (obj_pool_used > obj_pool_max_used)
219 obj_pool_max_used = obj_pool_used;
222 if (obj_pool_free < obj_pool_min_free)
223 obj_pool_min_free = obj_pool_free;
225 raw_spin_unlock(&pool_lock);
231 obj->state = ODEBUG_STATE_NONE;
233 hlist_add_head(&obj->node, &b->list);
239 * workqueue function to free objects.
241 * To reduce contention on the global pool_lock, the actual freeing of
242 * debug objects will be delayed if the pool_lock is busy.
244 static void free_obj_work(struct work_struct *work)
246 struct hlist_node *tmp;
247 struct debug_obj *obj;
251 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
255 * The objs on the pool list might be allocated before the work is
256 * run, so recheck if pool list it full or not, if not fill pool
257 * list from the global free list
259 while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
260 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
261 hlist_del(&obj->node);
262 hlist_add_head(&obj->node, &obj_pool);
268 * Pool list is already full and there are still objs on the free
269 * list. Move remaining free objs to a temporary list to free the
270 * memory outside the pool_lock held region.
273 hlist_move_list(&obj_to_free, &tofree);
274 debug_objects_freed += obj_nr_tofree;
277 raw_spin_unlock_irqrestore(&pool_lock, flags);
279 hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
280 hlist_del(&obj->node);
281 kmem_cache_free(obj_cache, obj);
285 static bool __free_object(struct debug_obj *obj)
289 struct debug_percpu_free *percpu_pool;
291 local_irq_save(flags);
293 * Try to free it into the percpu pool first.
295 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
296 if (obj_cache && percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
297 hlist_add_head(&obj->node, &percpu_pool->free_objs);
298 percpu_pool->obj_free++;
299 local_irq_restore(flags);
303 raw_spin_lock(&pool_lock);
304 work = (obj_pool_free > debug_objects_pool_size) && obj_cache;
309 hlist_add_head(&obj->node, &obj_to_free);
312 hlist_add_head(&obj->node, &obj_pool);
314 raw_spin_unlock(&pool_lock);
315 local_irq_restore(flags);
320 * Put the object back into the pool and schedule work to free objects
323 static void free_object(struct debug_obj *obj)
325 if (__free_object(obj))
326 schedule_work(&debug_obj_work);
330 * We run out of memory. That means we probably have tons of objects
333 static void debug_objects_oom(void)
335 struct debug_bucket *db = obj_hash;
336 struct hlist_node *tmp;
337 HLIST_HEAD(freelist);
338 struct debug_obj *obj;
342 pr_warn("Out of memory. ODEBUG disabled\n");
344 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
345 raw_spin_lock_irqsave(&db->lock, flags);
346 hlist_move_list(&db->list, &freelist);
347 raw_spin_unlock_irqrestore(&db->lock, flags);
350 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
351 hlist_del(&obj->node);
358 * We use the pfn of the address for the hash. That way we can check
359 * for freed objects simply by checking the affected bucket.
361 static struct debug_bucket *get_bucket(unsigned long addr)
365 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
366 return &obj_hash[hash];
369 static void debug_print_object(struct debug_obj *obj, char *msg)
371 struct debug_obj_descr *descr = obj->descr;
374 if (limit < 5 && descr != descr_test) {
375 void *hint = descr->debug_hint ?
376 descr->debug_hint(obj->object) : NULL;
378 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
379 "object type: %s hint: %pS\n",
380 msg, obj_states[obj->state], obj->astate,
383 debug_objects_warnings++;
387 * Try to repair the damage, so we have a better chance to get useful
391 debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
392 void * addr, enum debug_obj_state state)
394 if (fixup && fixup(addr, state)) {
395 debug_objects_fixups++;
401 static void debug_object_is_on_stack(void *addr, int onstack)
409 is_on_stack = object_is_on_stack(addr);
410 if (is_on_stack == onstack)
415 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
416 task_stack_page(current));
418 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
419 task_stack_page(current));
424 static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
425 struct debug_obj_descr *descr,
426 bool onstack, bool alloc_ifstatic)
428 struct debug_obj *obj = lookup_object(addr, b);
429 enum debug_obj_state state = ODEBUG_STATE_NONE;
435 * debug_object_init() unconditionally allocates untracked
436 * objects. It does not matter whether it is a static object or
439 * debug_object_assert_init() and debug_object_activate() allow
440 * allocation only if the descriptor callback confirms that the
441 * object is static and considered initialized. For non-static
442 * objects the allocation needs to be done from the fixup callback.
444 if (unlikely(alloc_ifstatic)) {
445 if (!descr->is_static_object || !descr->is_static_object(addr))
446 return ERR_PTR(-ENOENT);
447 /* Statically allocated objects are considered initialized */
448 state = ODEBUG_STATE_INIT;
451 obj = alloc_object(addr, b, descr);
454 debug_object_is_on_stack(addr, onstack);
458 /* Out of memory. Do the cleanup outside of the locked region */
459 debug_objects_enabled = 0;
463 static void debug_objects_fill_pool(void)
466 * On RT enabled kernels the pool refill must happen in preemptible
469 if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible())
474 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
476 enum debug_obj_state state;
477 struct debug_bucket *db;
478 struct debug_obj *obj;
481 debug_objects_fill_pool();
483 db = get_bucket((unsigned long) addr);
485 raw_spin_lock_irqsave(&db->lock, flags);
487 obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
488 if (unlikely(!obj)) {
489 raw_spin_unlock_irqrestore(&db->lock, flags);
494 switch (obj->state) {
495 case ODEBUG_STATE_NONE:
496 case ODEBUG_STATE_INIT:
497 case ODEBUG_STATE_INACTIVE:
498 obj->state = ODEBUG_STATE_INIT;
501 case ODEBUG_STATE_ACTIVE:
503 raw_spin_unlock_irqrestore(&db->lock, flags);
504 debug_print_object(obj, "init");
505 debug_object_fixup(descr->fixup_init, addr, state);
508 case ODEBUG_STATE_DESTROYED:
509 raw_spin_unlock_irqrestore(&db->lock, flags);
510 debug_print_object(obj, "init");
516 raw_spin_unlock_irqrestore(&db->lock, flags);
520 * debug_object_init - debug checks when an object is initialized
521 * @addr: address of the object
522 * @descr: pointer to an object specific debug description structure
524 void debug_object_init(void *addr, struct debug_obj_descr *descr)
526 if (!debug_objects_enabled)
529 __debug_object_init(addr, descr, 0);
531 EXPORT_SYMBOL_GPL(debug_object_init);
534 * debug_object_init_on_stack - debug checks when an object on stack is
536 * @addr: address of the object
537 * @descr: pointer to an object specific debug description structure
539 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
541 if (!debug_objects_enabled)
544 __debug_object_init(addr, descr, 1);
546 EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
549 * debug_object_activate - debug checks when an object is activated
550 * @addr: address of the object
551 * @descr: pointer to an object specific debug description structure
552 * Returns 0 for success, -EINVAL for check failed.
554 int debug_object_activate(void *addr, struct debug_obj_descr *descr)
556 struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
557 enum debug_obj_state state;
558 struct debug_bucket *db;
559 struct debug_obj *obj;
563 if (!debug_objects_enabled)
566 debug_objects_fill_pool();
568 db = get_bucket((unsigned long) addr);
570 raw_spin_lock_irqsave(&db->lock, flags);
572 obj = lookup_object_or_alloc(addr, db, descr, false, true);
573 if (likely(!IS_ERR_OR_NULL(obj))) {
574 bool print_object = false;
576 switch (obj->state) {
577 case ODEBUG_STATE_INIT:
578 case ODEBUG_STATE_INACTIVE:
579 obj->state = ODEBUG_STATE_ACTIVE;
583 case ODEBUG_STATE_ACTIVE:
585 raw_spin_unlock_irqrestore(&db->lock, flags);
586 debug_print_object(obj, "activate");
587 ret = debug_object_fixup(descr->fixup_activate, addr, state);
588 return ret ? 0 : -EINVAL;
590 case ODEBUG_STATE_DESTROYED:
598 raw_spin_unlock_irqrestore(&db->lock, flags);
600 debug_print_object(obj, "activate");
604 raw_spin_unlock_irqrestore(&db->lock, flags);
606 /* If NULL the allocation has hit OOM */
612 /* Object is neither static nor tracked. It's not initialized */
613 debug_print_object(&o, "activate");
614 ret = debug_object_fixup(descr->fixup_activate, addr, ODEBUG_STATE_NOTAVAILABLE);
615 return ret ? 0 : -EINVAL;
617 EXPORT_SYMBOL_GPL(debug_object_activate);
620 * debug_object_deactivate - debug checks when an object is deactivated
621 * @addr: address of the object
622 * @descr: pointer to an object specific debug description structure
624 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
626 struct debug_bucket *db;
627 struct debug_obj *obj;
629 bool print_object = false;
631 if (!debug_objects_enabled)
634 db = get_bucket((unsigned long) addr);
636 raw_spin_lock_irqsave(&db->lock, flags);
638 obj = lookup_object(addr, db);
640 switch (obj->state) {
641 case ODEBUG_STATE_INIT:
642 case ODEBUG_STATE_INACTIVE:
643 case ODEBUG_STATE_ACTIVE:
645 obj->state = ODEBUG_STATE_INACTIVE;
650 case ODEBUG_STATE_DESTROYED:
658 raw_spin_unlock_irqrestore(&db->lock, flags);
660 struct debug_obj o = { .object = addr,
661 .state = ODEBUG_STATE_NOTAVAILABLE,
664 debug_print_object(&o, "deactivate");
665 } else if (print_object) {
666 debug_print_object(obj, "deactivate");
669 EXPORT_SYMBOL_GPL(debug_object_deactivate);
672 * debug_object_destroy - debug checks when an object is destroyed
673 * @addr: address of the object
674 * @descr: pointer to an object specific debug description structure
676 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
678 enum debug_obj_state state;
679 struct debug_bucket *db;
680 struct debug_obj *obj;
682 bool print_object = false;
684 if (!debug_objects_enabled)
687 db = get_bucket((unsigned long) addr);
689 raw_spin_lock_irqsave(&db->lock, flags);
691 obj = lookup_object(addr, db);
695 switch (obj->state) {
696 case ODEBUG_STATE_NONE:
697 case ODEBUG_STATE_INIT:
698 case ODEBUG_STATE_INACTIVE:
699 obj->state = ODEBUG_STATE_DESTROYED;
701 case ODEBUG_STATE_ACTIVE:
703 raw_spin_unlock_irqrestore(&db->lock, flags);
704 debug_print_object(obj, "destroy");
705 debug_object_fixup(descr->fixup_destroy, addr, state);
708 case ODEBUG_STATE_DESTROYED:
715 raw_spin_unlock_irqrestore(&db->lock, flags);
717 debug_print_object(obj, "destroy");
719 EXPORT_SYMBOL_GPL(debug_object_destroy);
722 * debug_object_free - debug checks when an object is freed
723 * @addr: address of the object
724 * @descr: pointer to an object specific debug description structure
726 void debug_object_free(void *addr, struct debug_obj_descr *descr)
728 enum debug_obj_state state;
729 struct debug_bucket *db;
730 struct debug_obj *obj;
733 if (!debug_objects_enabled)
736 db = get_bucket((unsigned long) addr);
738 raw_spin_lock_irqsave(&db->lock, flags);
740 obj = lookup_object(addr, db);
744 switch (obj->state) {
745 case ODEBUG_STATE_ACTIVE:
747 raw_spin_unlock_irqrestore(&db->lock, flags);
748 debug_print_object(obj, "free");
749 debug_object_fixup(descr->fixup_free, addr, state);
752 hlist_del(&obj->node);
753 raw_spin_unlock_irqrestore(&db->lock, flags);
758 raw_spin_unlock_irqrestore(&db->lock, flags);
760 EXPORT_SYMBOL_GPL(debug_object_free);
763 * debug_object_assert_init - debug checks when object should be init-ed
764 * @addr: address of the object
765 * @descr: pointer to an object specific debug description structure
767 void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
769 struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
770 struct debug_bucket *db;
771 struct debug_obj *obj;
774 if (!debug_objects_enabled)
777 debug_objects_fill_pool();
779 db = get_bucket((unsigned long) addr);
781 raw_spin_lock_irqsave(&db->lock, flags);
782 obj = lookup_object_or_alloc(addr, db, descr, false, true);
783 raw_spin_unlock_irqrestore(&db->lock, flags);
784 if (likely(!IS_ERR_OR_NULL(obj)))
787 /* If NULL the allocation has hit OOM */
793 /* Object is neither tracked nor static. It's not initialized. */
794 debug_print_object(&o, "assert_init");
795 debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
797 EXPORT_SYMBOL_GPL(debug_object_assert_init);
800 * debug_object_active_state - debug checks object usage state machine
801 * @addr: address of the object
802 * @descr: pointer to an object specific debug description structure
803 * @expect: expected state
804 * @next: state to move to if expected state is found
807 debug_object_active_state(void *addr, struct debug_obj_descr *descr,
808 unsigned int expect, unsigned int next)
810 struct debug_bucket *db;
811 struct debug_obj *obj;
813 bool print_object = false;
815 if (!debug_objects_enabled)
818 db = get_bucket((unsigned long) addr);
820 raw_spin_lock_irqsave(&db->lock, flags);
822 obj = lookup_object(addr, db);
824 switch (obj->state) {
825 case ODEBUG_STATE_ACTIVE:
826 if (obj->astate == expect)
838 raw_spin_unlock_irqrestore(&db->lock, flags);
840 struct debug_obj o = { .object = addr,
841 .state = ODEBUG_STATE_NOTAVAILABLE,
844 debug_print_object(&o, "active_state");
845 } else if (print_object) {
846 debug_print_object(obj, "active_state");
849 EXPORT_SYMBOL_GPL(debug_object_active_state);
851 #ifdef CONFIG_DEBUG_OBJECTS_FREE
852 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
854 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
855 struct debug_obj_descr *descr;
856 enum debug_obj_state state;
857 struct debug_bucket *db;
858 struct hlist_node *tmp;
859 struct debug_obj *obj;
860 int cnt, objs_checked = 0;
863 saddr = (unsigned long) address;
864 eaddr = saddr + size;
865 paddr = saddr & ODEBUG_CHUNK_MASK;
866 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
867 chunks >>= ODEBUG_CHUNK_SHIFT;
869 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
870 db = get_bucket(paddr);
874 raw_spin_lock_irqsave(&db->lock, flags);
875 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
877 oaddr = (unsigned long) obj->object;
878 if (oaddr < saddr || oaddr >= eaddr)
881 switch (obj->state) {
882 case ODEBUG_STATE_ACTIVE:
885 raw_spin_unlock_irqrestore(&db->lock, flags);
886 debug_print_object(obj, "free");
887 debug_object_fixup(descr->fixup_free,
888 (void *) oaddr, state);
891 hlist_del(&obj->node);
892 work |= __free_object(obj);
896 raw_spin_unlock_irqrestore(&db->lock, flags);
898 if (cnt > debug_objects_maxchain)
899 debug_objects_maxchain = cnt;
904 if (objs_checked > debug_objects_maxchecked)
905 debug_objects_maxchecked = objs_checked;
907 /* Schedule work to actually kmem_cache_free() objects */
909 schedule_work(&debug_obj_work);
912 void debug_check_no_obj_freed(const void *address, unsigned long size)
914 if (debug_objects_enabled)
915 __debug_check_no_obj_freed(address, size);
919 #ifdef CONFIG_DEBUG_FS
921 static int debug_stats_show(struct seq_file *m, void *v)
923 int cpu, obj_percpu_free = 0;
925 for_each_possible_cpu(cpu)
926 obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
928 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
929 seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked);
930 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
931 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
932 seq_printf(m, "pool_free :%d\n", obj_pool_free + obj_percpu_free);
933 seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
934 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
935 seq_printf(m, "pool_used :%d\n", obj_pool_used - obj_percpu_free);
936 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
937 seq_printf(m, "on_free_list :%d\n", obj_nr_tofree);
938 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
939 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
943 static int debug_stats_open(struct inode *inode, struct file *filp)
945 return single_open(filp, debug_stats_show, NULL);
948 static const struct file_operations debug_stats_fops = {
949 .open = debug_stats_open,
952 .release = single_release,
955 static int __init debug_objects_init_debugfs(void)
957 struct dentry *dbgdir, *dbgstats;
959 if (!debug_objects_enabled)
962 dbgdir = debugfs_create_dir("debug_objects", NULL);
966 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
974 debugfs_remove(dbgdir);
978 __initcall(debug_objects_init_debugfs);
981 static inline void debug_objects_init_debugfs(void) { }
984 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
986 /* Random data structure for the self test */
988 unsigned long dummy1[6];
990 unsigned long dummy2[3];
993 static __initdata struct debug_obj_descr descr_type_test;
995 static bool __init is_static_object(void *addr)
997 struct self_test *obj = addr;
999 return obj->static_init;
1003 * fixup_init is called when:
1004 * - an active object is initialized
1006 static bool __init fixup_init(void *addr, enum debug_obj_state state)
1008 struct self_test *obj = addr;
1011 case ODEBUG_STATE_ACTIVE:
1012 debug_object_deactivate(obj, &descr_type_test);
1013 debug_object_init(obj, &descr_type_test);
1021 * fixup_activate is called when:
1022 * - an active object is activated
1023 * - an unknown non-static object is activated
1025 static bool __init fixup_activate(void *addr, enum debug_obj_state state)
1027 struct self_test *obj = addr;
1030 case ODEBUG_STATE_NOTAVAILABLE:
1032 case ODEBUG_STATE_ACTIVE:
1033 debug_object_deactivate(obj, &descr_type_test);
1034 debug_object_activate(obj, &descr_type_test);
1043 * fixup_destroy is called when:
1044 * - an active object is destroyed
1046 static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
1048 struct self_test *obj = addr;
1051 case ODEBUG_STATE_ACTIVE:
1052 debug_object_deactivate(obj, &descr_type_test);
1053 debug_object_destroy(obj, &descr_type_test);
1061 * fixup_free is called when:
1062 * - an active object is freed
1064 static bool __init fixup_free(void *addr, enum debug_obj_state state)
1066 struct self_test *obj = addr;
1069 case ODEBUG_STATE_ACTIVE:
1070 debug_object_deactivate(obj, &descr_type_test);
1071 debug_object_free(obj, &descr_type_test);
1079 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
1081 struct debug_bucket *db;
1082 struct debug_obj *obj;
1083 unsigned long flags;
1086 db = get_bucket((unsigned long) addr);
1088 raw_spin_lock_irqsave(&db->lock, flags);
1090 obj = lookup_object(addr, db);
1091 if (!obj && state != ODEBUG_STATE_NONE) {
1092 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
1095 if (obj && obj->state != state) {
1096 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
1100 if (fixups != debug_objects_fixups) {
1101 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
1102 fixups, debug_objects_fixups);
1105 if (warnings != debug_objects_warnings) {
1106 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
1107 warnings, debug_objects_warnings);
1112 raw_spin_unlock_irqrestore(&db->lock, flags);
1114 debug_objects_enabled = 0;
1118 static __initdata struct debug_obj_descr descr_type_test = {
1120 .is_static_object = is_static_object,
1121 .fixup_init = fixup_init,
1122 .fixup_activate = fixup_activate,
1123 .fixup_destroy = fixup_destroy,
1124 .fixup_free = fixup_free,
1127 static __initdata struct self_test obj = { .static_init = 0 };
1129 static void __init debug_objects_selftest(void)
1131 int fixups, oldfixups, warnings, oldwarnings;
1132 unsigned long flags;
1134 local_irq_save(flags);
1136 fixups = oldfixups = debug_objects_fixups;
1137 warnings = oldwarnings = debug_objects_warnings;
1138 descr_test = &descr_type_test;
1140 debug_object_init(&obj, &descr_type_test);
1141 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1143 debug_object_activate(&obj, &descr_type_test);
1144 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1146 debug_object_activate(&obj, &descr_type_test);
1147 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1149 debug_object_deactivate(&obj, &descr_type_test);
1150 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1152 debug_object_destroy(&obj, &descr_type_test);
1153 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1155 debug_object_init(&obj, &descr_type_test);
1156 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1158 debug_object_activate(&obj, &descr_type_test);
1159 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1161 debug_object_deactivate(&obj, &descr_type_test);
1162 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1164 debug_object_free(&obj, &descr_type_test);
1165 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1168 obj.static_init = 1;
1169 debug_object_activate(&obj, &descr_type_test);
1170 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1172 debug_object_init(&obj, &descr_type_test);
1173 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1175 debug_object_free(&obj, &descr_type_test);
1176 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1179 #ifdef CONFIG_DEBUG_OBJECTS_FREE
1180 debug_object_init(&obj, &descr_type_test);
1181 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1183 debug_object_activate(&obj, &descr_type_test);
1184 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1186 __debug_check_no_obj_freed(&obj, sizeof(obj));
1187 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1190 pr_info("selftest passed\n");
1193 debug_objects_fixups = oldfixups;
1194 debug_objects_warnings = oldwarnings;
1197 local_irq_restore(flags);
1200 static inline void debug_objects_selftest(void) { }
1204 * Called during early boot to initialize the hash buckets and link
1205 * the static object pool objects into the poll list. After this call
1206 * the object tracker is fully operational.
1208 void __init debug_objects_early_init(void)
1212 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1213 raw_spin_lock_init(&obj_hash[i].lock);
1215 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1216 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1220 * Convert the statically allocated objects to dynamic ones:
1222 static int __init debug_objects_replace_static_objects(void)
1224 struct debug_bucket *db = obj_hash;
1225 struct hlist_node *tmp;
1226 struct debug_obj *obj, *new;
1227 HLIST_HEAD(objects);
1230 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1231 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1234 hlist_add_head(&obj->node, &objects);
1238 * When debug_objects_mem_init() is called we know that only
1239 * one CPU is up, so disabling interrupts is enough
1240 * protection. This avoids the lockdep hell of lock ordering.
1242 local_irq_disable();
1244 /* Remove the statically allocated objects from the pool */
1245 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1246 hlist_del(&obj->node);
1247 /* Move the allocated objects to the pool */
1248 hlist_move_list(&objects, &obj_pool);
1250 /* Replace the active object references */
1251 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1252 hlist_move_list(&db->list, &objects);
1254 hlist_for_each_entry(obj, &objects, node) {
1255 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1256 hlist_del(&new->node);
1257 /* copy object data */
1259 hlist_add_head(&new->node, &db->list);
1265 pr_debug("%d of %d active objects replaced\n",
1266 cnt, obj_pool_used);
1269 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1270 hlist_del(&obj->node);
1271 kmem_cache_free(obj_cache, obj);
1277 * Called after the kmem_caches are functional to setup a dedicated
1278 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1279 * prevents that the debug code is called on kmem_cache_free() for the
1280 * debug tracker objects to avoid recursive calls.
1282 void __init debug_objects_mem_init(void)
1286 if (!debug_objects_enabled)
1290 * Initialize the percpu object pools
1292 * Initialization is not strictly necessary, but was done for
1295 for_each_possible_cpu(cpu)
1296 INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
1298 obj_cache = kmem_cache_create("debug_objects_cache",
1299 sizeof (struct debug_obj), 0,
1300 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
1303 if (!obj_cache || debug_objects_replace_static_objects()) {
1304 debug_objects_enabled = 0;
1305 kmem_cache_destroy(obj_cache);
1306 pr_warn("out of memory.\n");
1308 debug_objects_selftest();