12 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/hash.h>
18 #define ODEBUG_HASH_BITS 14
19 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
21 #define ODEBUG_POOL_SIZE 512
22 #define ODEBUG_POOL_MIN_LEVEL 256
24 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
25 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
26 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
43 static int obj_pool_used;
44 static int obj_pool_max_used;
51 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
58 static int __init enable_object_debug(
char *
str)
60 debug_objects_enabled = 1;
64 static int __init disable_object_debug(
char *
str)
66 debug_objects_enabled = 0;
71 early_param(
"no_debug_objects", disable_object_debug);
82 static void fill_pool(
void)
96 new = kmem_cache_zalloc(obj_cache, gfp);
101 hlist_add_head(&new->node, &obj_pool);
121 if (cnt > debug_objects_maxchain)
122 debug_objects_maxchain =
cnt;
137 if (obj_pool.first) {
144 hlist_del(&obj->
node);
146 hlist_add_head(&obj->
node, &b->
list);
149 if (obj_pool_used > obj_pool_max_used)
150 obj_pool_max_used = obj_pool_used;
153 if (obj_pool_free < obj_pool_min_free)
154 obj_pool_min_free = obj_pool_free;
172 hlist_del(&obj->
node);
189 static void free_object(
struct debug_obj *obj)
201 hlist_add_head(&obj->
node, &obj_pool);
213 static void debug_objects_oom(
void)
226 hlist_move_list(&db->
list, &freelist);
231 hlist_del(&obj->
node);
241 static struct debug_bucket *get_bucket(
unsigned long addr)
246 return &obj_hash[
hash];
249 static void debug_print_object(
struct debug_obj *obj,
char *
msg)
254 if (limit < 5 && descr != descr_test) {
259 "object type: %s hint: %pS\n",
263 debug_objects_warnings++;
277 fixed =
fixup(addr, state);
278 debug_objects_fixups +=
fixed;
282 static void debug_object_is_on_stack(
void *addr,
int onstack)
290 is_on_stack = object_is_on_stack(addr);
291 if (is_on_stack == onstack)
297 "ODEBUG: object is on stack, but not annotated\n");
300 "ODEBUG: object is not on stack, but annotated\n");
305 __debug_object_init(
void *addr,
struct debug_obj_descr *descr,
int onstack)
314 db = get_bucket((
unsigned long) addr);
318 obj = lookup_object(addr, db);
320 obj = alloc_object(addr, db, descr);
322 debug_objects_enabled = 0;
327 debug_object_is_on_stack(addr, onstack);
330 switch (obj->
state) {
338 debug_print_object(obj,
"init");
341 debug_object_fixup(descr->
fixup_init, addr, state);
345 debug_print_object(obj,
"init");
361 if (!debug_objects_enabled)
364 __debug_object_init(addr, descr, 0);
375 if (!debug_objects_enabled)
378 __debug_object_init(addr, descr, 1);
396 if (!debug_objects_enabled)
399 db = get_bucket((
unsigned long) addr);
403 obj = lookup_object(addr, db);
405 switch (obj->
state) {
412 debug_print_object(obj,
"activate");
419 debug_print_object(obj,
"activate");
436 debug_print_object(&o,
"activate");
450 if (!debug_objects_enabled)
453 db = get_bucket((
unsigned long) addr);
457 obj = lookup_object(addr, db);
459 switch (obj->
state) {
466 debug_print_object(obj,
"deactivate");
470 debug_print_object(obj,
"deactivate");
480 debug_print_object(&o,
"deactivate");
498 if (!debug_objects_enabled)
501 db = get_bucket((
unsigned long) addr);
505 obj = lookup_object(addr, db);
509 switch (obj->
state) {
516 debug_print_object(obj,
"destroy");
523 debug_print_object(obj,
"destroy");
544 if (!debug_objects_enabled)
547 db = get_bucket((
unsigned long) addr);
551 obj = lookup_object(addr, db);
555 switch (obj->
state) {
557 debug_print_object(obj,
"free");
560 debug_object_fixup(descr->
fixup_free, addr, state);
563 hlist_del(&obj->
node);
583 if (!debug_objects_enabled)
586 db = get_bucket((
unsigned long) addr);
590 obj = lookup_object(addr, db);
603 debug_print_object(&o,
"assert_init");
619 unsigned int expect,
unsigned int next)
625 if (!debug_objects_enabled)
628 db = get_bucket((
unsigned long) addr);
632 obj = lookup_object(addr, db);
634 switch (obj->
state) {
636 if (obj->
astate == expect)
639 debug_print_object(obj,
"active_state");
643 debug_print_object(obj,
"active_state");
651 debug_print_object(&o,
"active_state");
657 #ifdef CONFIG_DEBUG_OBJECTS_FREE
658 static void __debug_check_no_obj_freed(
const void *
address,
unsigned long size)
669 saddr = (
unsigned long) address;
670 eaddr = saddr +
size;
676 db = get_bucket(paddr);
684 if (oaddr < saddr || oaddr >= eaddr)
687 switch (obj->
state) {
689 debug_print_object(obj,
"free");
694 (
void *) oaddr, state);
697 hlist_del(&obj->
node);
698 hlist_add_head(&obj->
node, &freelist);
706 hlist_del(&obj->
node);
710 if (cnt > debug_objects_maxchain)
711 debug_objects_maxchain =
cnt;
715 void debug_check_no_obj_freed(
const void *address,
unsigned long size)
717 if (debug_objects_enabled)
718 __debug_check_no_obj_freed(address, size);
722 #ifdef CONFIG_DEBUG_FS
724 static int debug_stats_show(
struct seq_file *
m,
void *
v)
726 seq_printf(m,
"max_chain :%d\n", debug_objects_maxchain);
727 seq_printf(m,
"warnings :%d\n", debug_objects_warnings);
728 seq_printf(m,
"fixups :%d\n", debug_objects_fixups);
729 seq_printf(m,
"pool_free :%d\n", obj_pool_free);
730 seq_printf(m,
"pool_min_free :%d\n", obj_pool_min_free);
731 seq_printf(m,
"pool_used :%d\n", obj_pool_used);
732 seq_printf(m,
"pool_max_used :%d\n", obj_pool_max_used);
736 static int debug_stats_open(
struct inode *
inode,
struct file *filp)
742 .
open = debug_stats_open,
748 static int __init debug_objects_init_debugfs(
void)
750 struct dentry *dbgdir, *dbgstats;
752 if (!debug_objects_enabled)
774 static inline void debug_objects_init_debugfs(
void) { }
777 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
781 unsigned long dummy1[6];
783 unsigned long dummy2[3];
794 struct self_test *obj =
addr;
813 struct self_test *obj =
addr;
817 if (obj->static_init == 1) {
840 struct self_test *obj =
addr;
858 struct self_test *obj =
addr;
871 check_results(
void *addr,
enum debug_obj_state state,
int fixups,
int warnings)
878 db = get_bucket((
unsigned long) addr);
882 obj = lookup_object(addr, db);
884 WARN(1,
KERN_ERR "ODEBUG: selftest object not found\n");
887 if (obj && obj->
state != state) {
888 WARN(1,
KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
892 if (fixups != debug_objects_fixups) {
893 WARN(1,
KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
894 fixups, debug_objects_fixups);
897 if (warnings != debug_objects_warnings) {
898 WARN(1,
KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
899 warnings, debug_objects_warnings);
906 debug_objects_enabled = 0;
918 static __initdata struct self_test obj = { .static_init = 0 };
920 static void __init debug_objects_selftest(
void)
922 int fixups, oldfixups, warnings, oldwarnings;
927 fixups = oldfixups = debug_objects_fixups;
928 warnings = oldwarnings = debug_objects_warnings;
929 descr_test = &descr_type_test;
970 #ifdef CONFIG_DEBUG_OBJECTS_FREE
977 __debug_check_no_obj_freed(&obj,
sizeof(obj));
984 debug_objects_fixups = oldfixups;
985 debug_objects_warnings = oldwarnings;
991 static inline void debug_objects_selftest(
void) { }
1007 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1013 static int __init debug_objects_replace_static_objects(
void)
1022 obj = kmem_cache_zalloc(obj_cache,
GFP_KERNEL);
1025 hlist_add_head(&obj->
node, &objects);
1037 hlist_del(&obj->node);
1039 hlist_move_list(&objects, &obj_pool);
1043 hlist_move_list(&db->
list, &objects);
1047 hlist_del(&new->node);
1050 hlist_add_head(&new->node, &db->
list);
1061 hlist_del(&obj->
node);
1075 if (!debug_objects_enabled)
1082 if (!obj_cache || debug_objects_replace_static_objects()) {
1083 debug_objects_enabled = 0;
1088 debug_objects_selftest();