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
29 #define ODEBUG_BATCH_SIZE 16
31 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
32 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
33 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
36 * We limit the freeing of debug objects via workqueue at a maximum
37 * frequency of 10Hz and about 1024 objects for each freeing operation.
38 * So it is freeing at most 10k debug objects per second.
40 #define ODEBUG_FREE_WORK_MAX 1024
41 #define ODEBUG_FREE_WORK_DELAY DIV_ROUND_UP(HZ, 10)
44 struct hlist_head list
;
49 * Debug object percpu free list
50 * Access is protected by disabling irq
52 struct debug_percpu_free
{
53 struct hlist_head free_objs
;
57 static DEFINE_PER_CPU(struct debug_percpu_free
, percpu_obj_pool
);
59 static struct debug_bucket obj_hash
[ODEBUG_HASH_SIZE
];
61 static struct debug_obj obj_static_pool
[ODEBUG_POOL_SIZE
] __initdata
;
63 static DEFINE_RAW_SPINLOCK(pool_lock
);
65 static HLIST_HEAD(obj_pool
);
66 static HLIST_HEAD(obj_to_free
);
69 * Because of the presence of percpu free pools, obj_pool_free will
70 * under-count those in the percpu free pools. Similarly, obj_pool_used
71 * will over-count those in the percpu free pools. Adjustments will be
72 * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
75 static int obj_pool_min_free
= ODEBUG_POOL_SIZE
;
76 static int obj_pool_free
= ODEBUG_POOL_SIZE
;
77 static int obj_pool_used
;
78 static int obj_pool_max_used
;
79 static bool obj_freeing
;
80 /* The number of objs on the global free list */
81 static int obj_nr_tofree
;
83 static int debug_objects_maxchain __read_mostly
;
84 static int __maybe_unused debug_objects_maxchecked __read_mostly
;
85 static int debug_objects_fixups __read_mostly
;
86 static int debug_objects_warnings __read_mostly
;
87 static int debug_objects_enabled __read_mostly
88 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT
;
89 static int debug_objects_pool_size __read_mostly
91 static int debug_objects_pool_min_level __read_mostly
92 = ODEBUG_POOL_MIN_LEVEL
;
93 static struct debug_obj_descr
*descr_test __read_mostly
;
94 static struct kmem_cache
*obj_cache __read_mostly
;
97 * Track numbers of kmem_cache_alloc()/free() calls done.
99 static int debug_objects_allocated
;
100 static int debug_objects_freed
;
102 static void free_obj_work(struct work_struct
*work
);
103 static DECLARE_DELAYED_WORK(debug_obj_work
, free_obj_work
);
105 static int __init
enable_object_debug(char *str
)
107 debug_objects_enabled
= 1;
111 static int __init
disable_object_debug(char *str
)
113 debug_objects_enabled
= 0;
117 early_param("debug_objects", enable_object_debug
);
118 early_param("no_debug_objects", disable_object_debug
);
120 static const char *obj_states
[ODEBUG_STATE_MAX
] = {
121 [ODEBUG_STATE_NONE
] = "none",
122 [ODEBUG_STATE_INIT
] = "initialized",
123 [ODEBUG_STATE_INACTIVE
] = "inactive",
124 [ODEBUG_STATE_ACTIVE
] = "active",
125 [ODEBUG_STATE_DESTROYED
] = "destroyed",
126 [ODEBUG_STATE_NOTAVAILABLE
] = "not available",
129 static void fill_pool(void)
131 gfp_t gfp
= GFP_ATOMIC
| __GFP_NORETRY
| __GFP_NOWARN
;
132 struct debug_obj
*obj
;
135 if (likely(obj_pool_free
>= debug_objects_pool_min_level
))
139 * Reuse objs from the global free list; they will be reinitialized
142 while (obj_nr_tofree
&& (obj_pool_free
< obj_pool_min_free
)) {
143 raw_spin_lock_irqsave(&pool_lock
, flags
);
145 * Recheck with the lock held as the worker thread might have
146 * won the race and freed the global free list already.
148 while (obj_nr_tofree
&& (obj_pool_free
< obj_pool_min_free
)) {
149 obj
= hlist_entry(obj_to_free
.first
, typeof(*obj
), node
);
150 hlist_del(&obj
->node
);
152 hlist_add_head(&obj
->node
, &obj_pool
);
155 raw_spin_unlock_irqrestore(&pool_lock
, flags
);
158 if (unlikely(!obj_cache
))
161 while (obj_pool_free
< debug_objects_pool_min_level
) {
162 struct debug_obj
*new[ODEBUG_BATCH_SIZE
];
165 for (cnt
= 0; cnt
< ODEBUG_BATCH_SIZE
; cnt
++) {
166 new[cnt
] = kmem_cache_zalloc(obj_cache
, gfp
);
173 raw_spin_lock_irqsave(&pool_lock
, flags
);
175 hlist_add_head(&new[--cnt
]->node
, &obj_pool
);
176 debug_objects_allocated
++;
179 raw_spin_unlock_irqrestore(&pool_lock
, flags
);
184 * Lookup an object in the hash bucket.
186 static struct debug_obj
*lookup_object(void *addr
, struct debug_bucket
*b
)
188 struct debug_obj
*obj
;
191 hlist_for_each_entry(obj
, &b
->list
, node
) {
193 if (obj
->object
== addr
)
196 if (cnt
> debug_objects_maxchain
)
197 debug_objects_maxchain
= cnt
;
203 * Allocate a new object from the hlist
205 static struct debug_obj
*__alloc_object(struct hlist_head
*list
)
207 struct debug_obj
*obj
= NULL
;
210 obj
= hlist_entry(list
->first
, typeof(*obj
), node
);
211 hlist_del(&obj
->node
);
218 * Allocate a new object. If the pool is empty, switch off the debugger.
219 * Must be called with interrupts disabled.
221 static struct debug_obj
*
222 alloc_object(void *addr
, struct debug_bucket
*b
, struct debug_obj_descr
*descr
)
224 struct debug_percpu_free
*percpu_pool
= this_cpu_ptr(&percpu_obj_pool
);
225 struct debug_obj
*obj
;
227 if (likely(obj_cache
)) {
228 obj
= __alloc_object(&percpu_pool
->free_objs
);
230 percpu_pool
->obj_free
--;
235 raw_spin_lock(&pool_lock
);
236 obj
= __alloc_object(&obj_pool
);
242 * Looking ahead, allocate one batch of debug objects and
243 * put them into the percpu free pool.
245 if (likely(obj_cache
)) {
248 for (i
= 0; i
< ODEBUG_BATCH_SIZE
; i
++) {
249 struct debug_obj
*obj2
;
251 obj2
= __alloc_object(&obj_pool
);
254 hlist_add_head(&obj2
->node
,
255 &percpu_pool
->free_objs
);
256 percpu_pool
->obj_free
++;
262 if (obj_pool_used
> obj_pool_max_used
)
263 obj_pool_max_used
= obj_pool_used
;
265 if (obj_pool_free
< obj_pool_min_free
)
266 obj_pool_min_free
= obj_pool_free
;
268 raw_spin_unlock(&pool_lock
);
274 obj
->state
= ODEBUG_STATE_NONE
;
276 hlist_add_head(&obj
->node
, &b
->list
);
282 * workqueue function to free objects.
284 * To reduce contention on the global pool_lock, the actual freeing of
285 * debug objects will be delayed if the pool_lock is busy.
287 static void free_obj_work(struct work_struct
*work
)
289 struct hlist_node
*tmp
;
290 struct debug_obj
*obj
;
294 WRITE_ONCE(obj_freeing
, false);
295 if (!raw_spin_trylock_irqsave(&pool_lock
, flags
))
298 if (obj_pool_free
>= debug_objects_pool_size
)
302 * The objs on the pool list might be allocated before the work is
303 * run, so recheck if pool list it full or not, if not fill pool
304 * list from the global free list. As it is likely that a workload
305 * may be gearing up to use more and more objects, don't free any
306 * of them until the next round.
308 while (obj_nr_tofree
&& obj_pool_free
< debug_objects_pool_size
) {
309 obj
= hlist_entry(obj_to_free
.first
, typeof(*obj
), node
);
310 hlist_del(&obj
->node
);
311 hlist_add_head(&obj
->node
, &obj_pool
);
315 raw_spin_unlock_irqrestore(&pool_lock
, flags
);
320 * Pool list is already full and there are still objs on the free
321 * list. Move remaining free objs to a temporary list to free the
322 * memory outside the pool_lock held region.
325 hlist_move_list(&obj_to_free
, &tofree
);
326 debug_objects_freed
+= obj_nr_tofree
;
329 raw_spin_unlock_irqrestore(&pool_lock
, flags
);
331 hlist_for_each_entry_safe(obj
, tmp
, &tofree
, node
) {
332 hlist_del(&obj
->node
);
333 kmem_cache_free(obj_cache
, obj
);
337 static void __free_object(struct debug_obj
*obj
)
339 struct debug_obj
*objs
[ODEBUG_BATCH_SIZE
];
340 struct debug_percpu_free
*percpu_pool
;
341 int lookahead_count
= 0;
345 local_irq_save(flags
);
347 goto free_to_obj_pool
;
350 * Try to free it into the percpu pool first.
352 percpu_pool
= this_cpu_ptr(&percpu_obj_pool
);
353 if (percpu_pool
->obj_free
< ODEBUG_POOL_PERCPU_SIZE
) {
354 hlist_add_head(&obj
->node
, &percpu_pool
->free_objs
);
355 percpu_pool
->obj_free
++;
356 local_irq_restore(flags
);
361 * As the percpu pool is full, look ahead and pull out a batch
362 * of objects from the percpu pool and free them as well.
364 for (; lookahead_count
< ODEBUG_BATCH_SIZE
; lookahead_count
++) {
365 objs
[lookahead_count
] = __alloc_object(&percpu_pool
->free_objs
);
366 if (!objs
[lookahead_count
])
368 percpu_pool
->obj_free
--;
372 raw_spin_lock(&pool_lock
);
373 work
= (obj_pool_free
> debug_objects_pool_size
) && obj_cache
&&
374 (obj_nr_tofree
< ODEBUG_FREE_WORK_MAX
);
379 hlist_add_head(&obj
->node
, &obj_to_free
);
380 if (lookahead_count
) {
381 obj_nr_tofree
+= lookahead_count
;
382 obj_pool_used
-= lookahead_count
;
383 while (lookahead_count
) {
384 hlist_add_head(&objs
[--lookahead_count
]->node
,
389 if ((obj_pool_free
> debug_objects_pool_size
) &&
390 (obj_nr_tofree
< ODEBUG_FREE_WORK_MAX
)) {
394 * Free one more batch of objects from obj_pool.
396 for (i
= 0; i
< ODEBUG_BATCH_SIZE
; i
++) {
397 obj
= __alloc_object(&obj_pool
);
398 hlist_add_head(&obj
->node
, &obj_to_free
);
405 hlist_add_head(&obj
->node
, &obj_pool
);
406 if (lookahead_count
) {
407 obj_pool_free
+= lookahead_count
;
408 obj_pool_used
-= lookahead_count
;
409 while (lookahead_count
) {
410 hlist_add_head(&objs
[--lookahead_count
]->node
,
415 raw_spin_unlock(&pool_lock
);
416 local_irq_restore(flags
);
420 * Put the object back into the pool and schedule work to free objects
423 static void free_object(struct debug_obj
*obj
)
426 if (!obj_freeing
&& obj_nr_tofree
) {
427 WRITE_ONCE(obj_freeing
, true);
428 schedule_delayed_work(&debug_obj_work
, ODEBUG_FREE_WORK_DELAY
);
433 * We run out of memory. That means we probably have tons of objects
436 static void debug_objects_oom(void)
438 struct debug_bucket
*db
= obj_hash
;
439 struct hlist_node
*tmp
;
440 HLIST_HEAD(freelist
);
441 struct debug_obj
*obj
;
445 pr_warn("Out of memory. ODEBUG disabled\n");
447 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++, db
++) {
448 raw_spin_lock_irqsave(&db
->lock
, flags
);
449 hlist_move_list(&db
->list
, &freelist
);
450 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
453 hlist_for_each_entry_safe(obj
, tmp
, &freelist
, node
) {
454 hlist_del(&obj
->node
);
461 * We use the pfn of the address for the hash. That way we can check
462 * for freed objects simply by checking the affected bucket.
464 static struct debug_bucket
*get_bucket(unsigned long addr
)
468 hash
= hash_long((addr
>> ODEBUG_CHUNK_SHIFT
), ODEBUG_HASH_BITS
);
469 return &obj_hash
[hash
];
472 static void debug_print_object(struct debug_obj
*obj
, char *msg
)
474 struct debug_obj_descr
*descr
= obj
->descr
;
477 if (limit
< 5 && descr
!= descr_test
) {
478 void *hint
= descr
->debug_hint
?
479 descr
->debug_hint(obj
->object
) : NULL
;
481 WARN(1, KERN_ERR
"ODEBUG: %s %s (active state %u) "
482 "object type: %s hint: %pS\n",
483 msg
, obj_states
[obj
->state
], obj
->astate
,
486 debug_objects_warnings
++;
490 * Try to repair the damage, so we have a better chance to get useful
494 debug_object_fixup(bool (*fixup
)(void *addr
, enum debug_obj_state state
),
495 void * addr
, enum debug_obj_state state
)
497 if (fixup
&& fixup(addr
, state
)) {
498 debug_objects_fixups
++;
504 static void debug_object_is_on_stack(void *addr
, int onstack
)
512 is_on_stack
= object_is_on_stack(addr
);
513 if (is_on_stack
== onstack
)
518 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr
,
519 task_stack_page(current
));
521 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr
,
522 task_stack_page(current
));
528 __debug_object_init(void *addr
, struct debug_obj_descr
*descr
, int onstack
)
530 enum debug_obj_state state
;
531 bool check_stack
= false;
532 struct debug_bucket
*db
;
533 struct debug_obj
*obj
;
538 db
= get_bucket((unsigned long) addr
);
540 raw_spin_lock_irqsave(&db
->lock
, flags
);
542 obj
= lookup_object(addr
, db
);
544 obj
= alloc_object(addr
, db
, descr
);
546 debug_objects_enabled
= 0;
547 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
554 switch (obj
->state
) {
555 case ODEBUG_STATE_NONE
:
556 case ODEBUG_STATE_INIT
:
557 case ODEBUG_STATE_INACTIVE
:
558 obj
->state
= ODEBUG_STATE_INIT
;
561 case ODEBUG_STATE_ACTIVE
:
563 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
564 debug_print_object(obj
, "init");
565 debug_object_fixup(descr
->fixup_init
, addr
, state
);
568 case ODEBUG_STATE_DESTROYED
:
569 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
570 debug_print_object(obj
, "init");
576 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
578 debug_object_is_on_stack(addr
, onstack
);
582 * debug_object_init - debug checks when an object is initialized
583 * @addr: address of the object
584 * @descr: pointer to an object specific debug description structure
586 void debug_object_init(void *addr
, struct debug_obj_descr
*descr
)
588 if (!debug_objects_enabled
)
591 __debug_object_init(addr
, descr
, 0);
593 EXPORT_SYMBOL_GPL(debug_object_init
);
596 * debug_object_init_on_stack - debug checks when an object on stack is
598 * @addr: address of the object
599 * @descr: pointer to an object specific debug description structure
601 void debug_object_init_on_stack(void *addr
, struct debug_obj_descr
*descr
)
603 if (!debug_objects_enabled
)
606 __debug_object_init(addr
, descr
, 1);
608 EXPORT_SYMBOL_GPL(debug_object_init_on_stack
);
611 * debug_object_activate - debug checks when an object is activated
612 * @addr: address of the object
613 * @descr: pointer to an object specific debug description structure
614 * Returns 0 for success, -EINVAL for check failed.
616 int debug_object_activate(void *addr
, struct debug_obj_descr
*descr
)
618 enum debug_obj_state state
;
619 struct debug_bucket
*db
;
620 struct debug_obj
*obj
;
623 struct debug_obj o
= { .object
= addr
,
624 .state
= ODEBUG_STATE_NOTAVAILABLE
,
627 if (!debug_objects_enabled
)
630 db
= get_bucket((unsigned long) addr
);
632 raw_spin_lock_irqsave(&db
->lock
, flags
);
634 obj
= lookup_object(addr
, db
);
636 bool print_object
= false;
638 switch (obj
->state
) {
639 case ODEBUG_STATE_INIT
:
640 case ODEBUG_STATE_INACTIVE
:
641 obj
->state
= ODEBUG_STATE_ACTIVE
;
645 case ODEBUG_STATE_ACTIVE
:
647 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
648 debug_print_object(obj
, "activate");
649 ret
= debug_object_fixup(descr
->fixup_activate
, addr
, state
);
650 return ret
? 0 : -EINVAL
;
652 case ODEBUG_STATE_DESTROYED
:
660 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
662 debug_print_object(obj
, "activate");
666 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
669 * We are here when a static object is activated. We
670 * let the type specific code confirm whether this is
671 * true or not. if true, we just make sure that the
672 * static object is tracked in the object tracker. If
673 * not, this must be a bug, so we try to fix it up.
675 if (descr
->is_static_object
&& descr
->is_static_object(addr
)) {
676 /* track this static object */
677 debug_object_init(addr
, descr
);
678 debug_object_activate(addr
, descr
);
680 debug_print_object(&o
, "activate");
681 ret
= debug_object_fixup(descr
->fixup_activate
, addr
,
682 ODEBUG_STATE_NOTAVAILABLE
);
683 return ret
? 0 : -EINVAL
;
687 EXPORT_SYMBOL_GPL(debug_object_activate
);
690 * debug_object_deactivate - debug checks when an object is deactivated
691 * @addr: address of the object
692 * @descr: pointer to an object specific debug description structure
694 void debug_object_deactivate(void *addr
, struct debug_obj_descr
*descr
)
696 struct debug_bucket
*db
;
697 struct debug_obj
*obj
;
699 bool print_object
= false;
701 if (!debug_objects_enabled
)
704 db
= get_bucket((unsigned long) addr
);
706 raw_spin_lock_irqsave(&db
->lock
, flags
);
708 obj
= lookup_object(addr
, db
);
710 switch (obj
->state
) {
711 case ODEBUG_STATE_INIT
:
712 case ODEBUG_STATE_INACTIVE
:
713 case ODEBUG_STATE_ACTIVE
:
715 obj
->state
= ODEBUG_STATE_INACTIVE
;
720 case ODEBUG_STATE_DESTROYED
:
728 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
730 struct debug_obj o
= { .object
= addr
,
731 .state
= ODEBUG_STATE_NOTAVAILABLE
,
734 debug_print_object(&o
, "deactivate");
735 } else if (print_object
) {
736 debug_print_object(obj
, "deactivate");
739 EXPORT_SYMBOL_GPL(debug_object_deactivate
);
742 * debug_object_destroy - debug checks when an object is destroyed
743 * @addr: address of the object
744 * @descr: pointer to an object specific debug description structure
746 void debug_object_destroy(void *addr
, struct debug_obj_descr
*descr
)
748 enum debug_obj_state state
;
749 struct debug_bucket
*db
;
750 struct debug_obj
*obj
;
752 bool print_object
= false;
754 if (!debug_objects_enabled
)
757 db
= get_bucket((unsigned long) addr
);
759 raw_spin_lock_irqsave(&db
->lock
, flags
);
761 obj
= lookup_object(addr
, db
);
765 switch (obj
->state
) {
766 case ODEBUG_STATE_NONE
:
767 case ODEBUG_STATE_INIT
:
768 case ODEBUG_STATE_INACTIVE
:
769 obj
->state
= ODEBUG_STATE_DESTROYED
;
771 case ODEBUG_STATE_ACTIVE
:
773 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
774 debug_print_object(obj
, "destroy");
775 debug_object_fixup(descr
->fixup_destroy
, addr
, state
);
778 case ODEBUG_STATE_DESTROYED
:
785 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
787 debug_print_object(obj
, "destroy");
789 EXPORT_SYMBOL_GPL(debug_object_destroy
);
792 * debug_object_free - debug checks when an object is freed
793 * @addr: address of the object
794 * @descr: pointer to an object specific debug description structure
796 void debug_object_free(void *addr
, struct debug_obj_descr
*descr
)
798 enum debug_obj_state state
;
799 struct debug_bucket
*db
;
800 struct debug_obj
*obj
;
803 if (!debug_objects_enabled
)
806 db
= get_bucket((unsigned long) addr
);
808 raw_spin_lock_irqsave(&db
->lock
, flags
);
810 obj
= lookup_object(addr
, db
);
814 switch (obj
->state
) {
815 case ODEBUG_STATE_ACTIVE
:
817 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
818 debug_print_object(obj
, "free");
819 debug_object_fixup(descr
->fixup_free
, addr
, state
);
822 hlist_del(&obj
->node
);
823 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
828 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
830 EXPORT_SYMBOL_GPL(debug_object_free
);
833 * debug_object_assert_init - debug checks when object should be init-ed
834 * @addr: address of the object
835 * @descr: pointer to an object specific debug description structure
837 void debug_object_assert_init(void *addr
, struct debug_obj_descr
*descr
)
839 struct debug_bucket
*db
;
840 struct debug_obj
*obj
;
843 if (!debug_objects_enabled
)
846 db
= get_bucket((unsigned long) addr
);
848 raw_spin_lock_irqsave(&db
->lock
, flags
);
850 obj
= lookup_object(addr
, db
);
852 struct debug_obj o
= { .object
= addr
,
853 .state
= ODEBUG_STATE_NOTAVAILABLE
,
856 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
858 * Maybe the object is static, and we let the type specific
859 * code confirm. Track this static object if true, else invoke
862 if (descr
->is_static_object
&& descr
->is_static_object(addr
)) {
863 /* Track this static object */
864 debug_object_init(addr
, descr
);
866 debug_print_object(&o
, "assert_init");
867 debug_object_fixup(descr
->fixup_assert_init
, addr
,
868 ODEBUG_STATE_NOTAVAILABLE
);
873 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
875 EXPORT_SYMBOL_GPL(debug_object_assert_init
);
878 * debug_object_active_state - debug checks object usage state machine
879 * @addr: address of the object
880 * @descr: pointer to an object specific debug description structure
881 * @expect: expected state
882 * @next: state to move to if expected state is found
885 debug_object_active_state(void *addr
, struct debug_obj_descr
*descr
,
886 unsigned int expect
, unsigned int next
)
888 struct debug_bucket
*db
;
889 struct debug_obj
*obj
;
891 bool print_object
= false;
893 if (!debug_objects_enabled
)
896 db
= get_bucket((unsigned long) addr
);
898 raw_spin_lock_irqsave(&db
->lock
, flags
);
900 obj
= lookup_object(addr
, db
);
902 switch (obj
->state
) {
903 case ODEBUG_STATE_ACTIVE
:
904 if (obj
->astate
== expect
)
916 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
918 struct debug_obj o
= { .object
= addr
,
919 .state
= ODEBUG_STATE_NOTAVAILABLE
,
922 debug_print_object(&o
, "active_state");
923 } else if (print_object
) {
924 debug_print_object(obj
, "active_state");
927 EXPORT_SYMBOL_GPL(debug_object_active_state
);
929 #ifdef CONFIG_DEBUG_OBJECTS_FREE
930 static void __debug_check_no_obj_freed(const void *address
, unsigned long size
)
932 unsigned long flags
, oaddr
, saddr
, eaddr
, paddr
, chunks
;
933 struct debug_obj_descr
*descr
;
934 enum debug_obj_state state
;
935 struct debug_bucket
*db
;
936 struct hlist_node
*tmp
;
937 struct debug_obj
*obj
;
938 int cnt
, objs_checked
= 0;
940 saddr
= (unsigned long) address
;
941 eaddr
= saddr
+ size
;
942 paddr
= saddr
& ODEBUG_CHUNK_MASK
;
943 chunks
= ((eaddr
- paddr
) + (ODEBUG_CHUNK_SIZE
- 1));
944 chunks
>>= ODEBUG_CHUNK_SHIFT
;
946 for (;chunks
> 0; chunks
--, paddr
+= ODEBUG_CHUNK_SIZE
) {
947 db
= get_bucket(paddr
);
951 raw_spin_lock_irqsave(&db
->lock
, flags
);
952 hlist_for_each_entry_safe(obj
, tmp
, &db
->list
, node
) {
954 oaddr
= (unsigned long) obj
->object
;
955 if (oaddr
< saddr
|| oaddr
>= eaddr
)
958 switch (obj
->state
) {
959 case ODEBUG_STATE_ACTIVE
:
962 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
963 debug_print_object(obj
, "free");
964 debug_object_fixup(descr
->fixup_free
,
965 (void *) oaddr
, state
);
968 hlist_del(&obj
->node
);
973 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
975 if (cnt
> debug_objects_maxchain
)
976 debug_objects_maxchain
= cnt
;
981 if (objs_checked
> debug_objects_maxchecked
)
982 debug_objects_maxchecked
= objs_checked
;
984 /* Schedule work to actually kmem_cache_free() objects */
985 if (!obj_freeing
&& obj_nr_tofree
) {
986 WRITE_ONCE(obj_freeing
, true);
987 schedule_delayed_work(&debug_obj_work
, ODEBUG_FREE_WORK_DELAY
);
991 void debug_check_no_obj_freed(const void *address
, unsigned long size
)
993 if (debug_objects_enabled
)
994 __debug_check_no_obj_freed(address
, size
);
998 #ifdef CONFIG_DEBUG_FS
1000 static int debug_stats_show(struct seq_file
*m
, void *v
)
1002 int cpu
, obj_percpu_free
= 0;
1004 for_each_possible_cpu(cpu
)
1005 obj_percpu_free
+= per_cpu(percpu_obj_pool
.obj_free
, cpu
);
1007 seq_printf(m
, "max_chain :%d\n", debug_objects_maxchain
);
1008 seq_printf(m
, "max_checked :%d\n", debug_objects_maxchecked
);
1009 seq_printf(m
, "warnings :%d\n", debug_objects_warnings
);
1010 seq_printf(m
, "fixups :%d\n", debug_objects_fixups
);
1011 seq_printf(m
, "pool_free :%d\n", obj_pool_free
+ obj_percpu_free
);
1012 seq_printf(m
, "pool_pcp_free :%d\n", obj_percpu_free
);
1013 seq_printf(m
, "pool_min_free :%d\n", obj_pool_min_free
);
1014 seq_printf(m
, "pool_used :%d\n", obj_pool_used
- obj_percpu_free
);
1015 seq_printf(m
, "pool_max_used :%d\n", obj_pool_max_used
);
1016 seq_printf(m
, "on_free_list :%d\n", obj_nr_tofree
);
1017 seq_printf(m
, "objs_allocated:%d\n", debug_objects_allocated
);
1018 seq_printf(m
, "objs_freed :%d\n", debug_objects_freed
);
1022 static int debug_stats_open(struct inode
*inode
, struct file
*filp
)
1024 return single_open(filp
, debug_stats_show
, NULL
);
1027 static const struct file_operations debug_stats_fops
= {
1028 .open
= debug_stats_open
,
1030 .llseek
= seq_lseek
,
1031 .release
= single_release
,
1034 static int __init
debug_objects_init_debugfs(void)
1036 struct dentry
*dbgdir
;
1038 if (!debug_objects_enabled
)
1041 dbgdir
= debugfs_create_dir("debug_objects", NULL
);
1043 debugfs_create_file("stats", 0444, dbgdir
, NULL
, &debug_stats_fops
);
1047 __initcall(debug_objects_init_debugfs
);
1050 static inline void debug_objects_init_debugfs(void) { }
1053 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
1055 /* Random data structure for the self test */
1057 unsigned long dummy1
[6];
1059 unsigned long dummy2
[3];
1062 static __initdata
struct debug_obj_descr descr_type_test
;
1064 static bool __init
is_static_object(void *addr
)
1066 struct self_test
*obj
= addr
;
1068 return obj
->static_init
;
1072 * fixup_init is called when:
1073 * - an active object is initialized
1075 static bool __init
fixup_init(void *addr
, enum debug_obj_state state
)
1077 struct self_test
*obj
= addr
;
1080 case ODEBUG_STATE_ACTIVE
:
1081 debug_object_deactivate(obj
, &descr_type_test
);
1082 debug_object_init(obj
, &descr_type_test
);
1090 * fixup_activate is called when:
1091 * - an active object is activated
1092 * - an unknown non-static object is activated
1094 static bool __init
fixup_activate(void *addr
, enum debug_obj_state state
)
1096 struct self_test
*obj
= addr
;
1099 case ODEBUG_STATE_NOTAVAILABLE
:
1101 case ODEBUG_STATE_ACTIVE
:
1102 debug_object_deactivate(obj
, &descr_type_test
);
1103 debug_object_activate(obj
, &descr_type_test
);
1112 * fixup_destroy is called when:
1113 * - an active object is destroyed
1115 static bool __init
fixup_destroy(void *addr
, enum debug_obj_state state
)
1117 struct self_test
*obj
= addr
;
1120 case ODEBUG_STATE_ACTIVE
:
1121 debug_object_deactivate(obj
, &descr_type_test
);
1122 debug_object_destroy(obj
, &descr_type_test
);
1130 * fixup_free is called when:
1131 * - an active object is freed
1133 static bool __init
fixup_free(void *addr
, enum debug_obj_state state
)
1135 struct self_test
*obj
= addr
;
1138 case ODEBUG_STATE_ACTIVE
:
1139 debug_object_deactivate(obj
, &descr_type_test
);
1140 debug_object_free(obj
, &descr_type_test
);
1148 check_results(void *addr
, enum debug_obj_state state
, int fixups
, int warnings
)
1150 struct debug_bucket
*db
;
1151 struct debug_obj
*obj
;
1152 unsigned long flags
;
1155 db
= get_bucket((unsigned long) addr
);
1157 raw_spin_lock_irqsave(&db
->lock
, flags
);
1159 obj
= lookup_object(addr
, db
);
1160 if (!obj
&& state
!= ODEBUG_STATE_NONE
) {
1161 WARN(1, KERN_ERR
"ODEBUG: selftest object not found\n");
1164 if (obj
&& obj
->state
!= state
) {
1165 WARN(1, KERN_ERR
"ODEBUG: selftest wrong state: %d != %d\n",
1169 if (fixups
!= debug_objects_fixups
) {
1170 WARN(1, KERN_ERR
"ODEBUG: selftest fixups failed %d != %d\n",
1171 fixups
, debug_objects_fixups
);
1174 if (warnings
!= debug_objects_warnings
) {
1175 WARN(1, KERN_ERR
"ODEBUG: selftest warnings failed %d != %d\n",
1176 warnings
, debug_objects_warnings
);
1181 raw_spin_unlock_irqrestore(&db
->lock
, flags
);
1183 debug_objects_enabled
= 0;
1187 static __initdata
struct debug_obj_descr descr_type_test
= {
1189 .is_static_object
= is_static_object
,
1190 .fixup_init
= fixup_init
,
1191 .fixup_activate
= fixup_activate
,
1192 .fixup_destroy
= fixup_destroy
,
1193 .fixup_free
= fixup_free
,
1196 static __initdata
struct self_test obj
= { .static_init
= 0 };
1198 static void __init
debug_objects_selftest(void)
1200 int fixups
, oldfixups
, warnings
, oldwarnings
;
1201 unsigned long flags
;
1203 local_irq_save(flags
);
1205 fixups
= oldfixups
= debug_objects_fixups
;
1206 warnings
= oldwarnings
= debug_objects_warnings
;
1207 descr_test
= &descr_type_test
;
1209 debug_object_init(&obj
, &descr_type_test
);
1210 if (check_results(&obj
, ODEBUG_STATE_INIT
, fixups
, warnings
))
1212 debug_object_activate(&obj
, &descr_type_test
);
1213 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, fixups
, warnings
))
1215 debug_object_activate(&obj
, &descr_type_test
);
1216 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, ++fixups
, ++warnings
))
1218 debug_object_deactivate(&obj
, &descr_type_test
);
1219 if (check_results(&obj
, ODEBUG_STATE_INACTIVE
, fixups
, warnings
))
1221 debug_object_destroy(&obj
, &descr_type_test
);
1222 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, warnings
))
1224 debug_object_init(&obj
, &descr_type_test
);
1225 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
1227 debug_object_activate(&obj
, &descr_type_test
);
1228 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
1230 debug_object_deactivate(&obj
, &descr_type_test
);
1231 if (check_results(&obj
, ODEBUG_STATE_DESTROYED
, fixups
, ++warnings
))
1233 debug_object_free(&obj
, &descr_type_test
);
1234 if (check_results(&obj
, ODEBUG_STATE_NONE
, fixups
, warnings
))
1237 obj
.static_init
= 1;
1238 debug_object_activate(&obj
, &descr_type_test
);
1239 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, fixups
, warnings
))
1241 debug_object_init(&obj
, &descr_type_test
);
1242 if (check_results(&obj
, ODEBUG_STATE_INIT
, ++fixups
, ++warnings
))
1244 debug_object_free(&obj
, &descr_type_test
);
1245 if (check_results(&obj
, ODEBUG_STATE_NONE
, fixups
, warnings
))
1248 #ifdef CONFIG_DEBUG_OBJECTS_FREE
1249 debug_object_init(&obj
, &descr_type_test
);
1250 if (check_results(&obj
, ODEBUG_STATE_INIT
, fixups
, warnings
))
1252 debug_object_activate(&obj
, &descr_type_test
);
1253 if (check_results(&obj
, ODEBUG_STATE_ACTIVE
, fixups
, warnings
))
1255 __debug_check_no_obj_freed(&obj
, sizeof(obj
));
1256 if (check_results(&obj
, ODEBUG_STATE_NONE
, ++fixups
, ++warnings
))
1259 pr_info("selftest passed\n");
1262 debug_objects_fixups
= oldfixups
;
1263 debug_objects_warnings
= oldwarnings
;
1266 local_irq_restore(flags
);
1269 static inline void debug_objects_selftest(void) { }
1273 * Called during early boot to initialize the hash buckets and link
1274 * the static object pool objects into the poll list. After this call
1275 * the object tracker is fully operational.
1277 void __init
debug_objects_early_init(void)
1281 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++)
1282 raw_spin_lock_init(&obj_hash
[i
].lock
);
1284 for (i
= 0; i
< ODEBUG_POOL_SIZE
; i
++)
1285 hlist_add_head(&obj_static_pool
[i
].node
, &obj_pool
);
1289 * Convert the statically allocated objects to dynamic ones:
1291 static int __init
debug_objects_replace_static_objects(void)
1293 struct debug_bucket
*db
= obj_hash
;
1294 struct hlist_node
*tmp
;
1295 struct debug_obj
*obj
, *new;
1296 HLIST_HEAD(objects
);
1299 for (i
= 0; i
< ODEBUG_POOL_SIZE
; i
++) {
1300 obj
= kmem_cache_zalloc(obj_cache
, GFP_KERNEL
);
1303 hlist_add_head(&obj
->node
, &objects
);
1307 * debug_objects_mem_init() is now called early that only one CPU is up
1308 * and interrupts have been disabled, so it is safe to replace the
1309 * active object references.
1312 /* Remove the statically allocated objects from the pool */
1313 hlist_for_each_entry_safe(obj
, tmp
, &obj_pool
, node
)
1314 hlist_del(&obj
->node
);
1315 /* Move the allocated objects to the pool */
1316 hlist_move_list(&objects
, &obj_pool
);
1318 /* Replace the active object references */
1319 for (i
= 0; i
< ODEBUG_HASH_SIZE
; i
++, db
++) {
1320 hlist_move_list(&db
->list
, &objects
);
1322 hlist_for_each_entry(obj
, &objects
, node
) {
1323 new = hlist_entry(obj_pool
.first
, typeof(*obj
), node
);
1324 hlist_del(&new->node
);
1325 /* copy object data */
1327 hlist_add_head(&new->node
, &db
->list
);
1332 pr_debug("%d of %d active objects replaced\n",
1333 cnt
, obj_pool_used
);
1336 hlist_for_each_entry_safe(obj
, tmp
, &objects
, node
) {
1337 hlist_del(&obj
->node
);
1338 kmem_cache_free(obj_cache
, obj
);
1344 * Called after the kmem_caches are functional to setup a dedicated
1345 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1346 * prevents that the debug code is called on kmem_cache_free() for the
1347 * debug tracker objects to avoid recursive calls.
1349 void __init
debug_objects_mem_init(void)
1353 if (!debug_objects_enabled
)
1357 * Initialize the percpu object pools
1359 * Initialization is not strictly necessary, but was done for
1362 for_each_possible_cpu(cpu
)
1363 INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool
.free_objs
, cpu
));
1365 obj_cache
= kmem_cache_create("debug_objects_cache",
1366 sizeof (struct debug_obj
), 0,
1367 SLAB_DEBUG_OBJECTS
| SLAB_NOLEAKTRACE
,
1370 if (!obj_cache
|| debug_objects_replace_static_objects()) {
1371 debug_objects_enabled
= 0;
1372 kmem_cache_destroy(obj_cache
);
1373 pr_warn("out of memory.\n");
1375 debug_objects_selftest();
1378 * Increase the thresholds for allocating and freeing objects
1379 * according to the number of possible CPUs available in the system.
1381 extras
= num_possible_cpus() * ODEBUG_BATCH_SIZE
;
1382 debug_objects_pool_size
+= extras
;
1383 debug_objects_pool_min_level
+= extras
;