1 /* 2 * Generic infrastructure for lifetime debugging of objects. 3 * 4 * Started by Thomas Gleixner 5 * 6 * Copyright (C) 2008, Thomas Gleixner <tglx@linutronix.de> 7 * 8 * For licencing details see kernel-base/COPYING 9 */ 10 #include <linux/debugobjects.h> 11 #include <linux/interrupt.h> 12 #include <linux/seq_file.h> 13 #include <linux/debugfs.h> 14 #include <linux/hash.h> 15 16 #define ODEBUG_HASH_BITS 14 17 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS) 18 19 #define ODEBUG_POOL_SIZE 512 20 #define ODEBUG_POOL_MIN_LEVEL 256 21 22 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT 23 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT) 24 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1)) 25 26 struct debug_bucket { 27 struct hlist_head list; 28 spinlock_t lock; 29 }; 30 31 static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE]; 32 33 static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE]; 34 35 static DEFINE_SPINLOCK(pool_lock); 36 37 static HLIST_HEAD(obj_pool); 38 39 static int obj_pool_min_free = ODEBUG_POOL_SIZE; 40 static int obj_pool_free = ODEBUG_POOL_SIZE; 41 static int obj_pool_used; 42 static int obj_pool_max_used; 43 static struct kmem_cache *obj_cache; 44 45 static int debug_objects_maxchain __read_mostly; 46 static int debug_objects_fixups __read_mostly; 47 static int debug_objects_warnings __read_mostly; 48 static int debug_objects_enabled __read_mostly; 49 static struct debug_obj_descr *descr_test __read_mostly; 50 51 static int __init enable_object_debug(char *str) 52 { 53 debug_objects_enabled = 1; 54 return 0; 55 } 56 early_param("debug_objects", enable_object_debug); 57 58 static const char *obj_states[ODEBUG_STATE_MAX] = { 59 [ODEBUG_STATE_NONE] = "none", 60 [ODEBUG_STATE_INIT] = "initialized", 61 [ODEBUG_STATE_INACTIVE] = "inactive", 62 [ODEBUG_STATE_ACTIVE] = "active", 63 [ODEBUG_STATE_DESTROYED] = "destroyed", 64 [ODEBUG_STATE_NOTAVAILABLE] = "not available", 65 }; 66 67 static int fill_pool(void) 68 { 69 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN; 70 struct debug_obj *new; 71 unsigned long flags; 72 73 if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL)) 74 return obj_pool_free; 75 76 if (unlikely(!obj_cache)) 77 return obj_pool_free; 78 79 while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) { 80 81 new = kmem_cache_zalloc(obj_cache, gfp); 82 if (!new) 83 return obj_pool_free; 84 85 spin_lock_irqsave(&pool_lock, flags); 86 hlist_add_head(&new->node, &obj_pool); 87 obj_pool_free++; 88 spin_unlock_irqrestore(&pool_lock, flags); 89 } 90 return obj_pool_free; 91 } 92 93 /* 94 * Lookup an object in the hash bucket. 95 */ 96 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b) 97 { 98 struct hlist_node *node; 99 struct debug_obj *obj; 100 int cnt = 0; 101 102 hlist_for_each_entry(obj, node, &b->list, node) { 103 cnt++; 104 if (obj->object == addr) 105 return obj; 106 } 107 if (cnt > debug_objects_maxchain) 108 debug_objects_maxchain = cnt; 109 110 return NULL; 111 } 112 113 /* 114 * Allocate a new object. If the pool is empty, switch off the debugger. 115 * Must be called with interrupts disabled. 116 */ 117 static struct debug_obj * 118 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr) 119 { 120 struct debug_obj *obj = NULL; 121 122 spin_lock(&pool_lock); 123 if (obj_pool.first) { 124 obj = hlist_entry(obj_pool.first, typeof(*obj), node); 125 126 obj->object = addr; 127 obj->descr = descr; 128 obj->state = ODEBUG_STATE_NONE; 129 hlist_del(&obj->node); 130 131 hlist_add_head(&obj->node, &b->list); 132 133 obj_pool_used++; 134 if (obj_pool_used > obj_pool_max_used) 135 obj_pool_max_used = obj_pool_used; 136 137 obj_pool_free--; 138 if (obj_pool_free < obj_pool_min_free) 139 obj_pool_min_free = obj_pool_free; 140 } 141 spin_unlock(&pool_lock); 142 143 return obj; 144 } 145 146 /* 147 * Put the object back into the pool or give it back to kmem_cache: 148 */ 149 static void free_object(struct debug_obj *obj) 150 { 151 unsigned long idx = (unsigned long)(obj - obj_static_pool); 152 unsigned long flags; 153 154 if (obj_pool_free < ODEBUG_POOL_SIZE || idx < ODEBUG_POOL_SIZE) { 155 spin_lock_irqsave(&pool_lock, flags); 156 hlist_add_head(&obj->node, &obj_pool); 157 obj_pool_free++; 158 obj_pool_used--; 159 spin_unlock_irqrestore(&pool_lock, flags); 160 } else { 161 spin_lock_irqsave(&pool_lock, flags); 162 obj_pool_used--; 163 spin_unlock_irqrestore(&pool_lock, flags); 164 kmem_cache_free(obj_cache, obj); 165 } 166 } 167 168 /* 169 * We run out of memory. That means we probably have tons of objects 170 * allocated. 171 */ 172 static void debug_objects_oom(void) 173 { 174 struct debug_bucket *db = obj_hash; 175 struct hlist_node *node, *tmp; 176 HLIST_HEAD(freelist); 177 struct debug_obj *obj; 178 unsigned long flags; 179 int i; 180 181 printk(KERN_WARNING "ODEBUG: Out of memory. ODEBUG disabled\n"); 182 183 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) { 184 spin_lock_irqsave(&db->lock, flags); 185 hlist_move_list(&db->list, &freelist); 186 spin_unlock_irqrestore(&db->lock, flags); 187 188 /* Now free them */ 189 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) { 190 hlist_del(&obj->node); 191 free_object(obj); 192 } 193 } 194 } 195 196 /* 197 * We use the pfn of the address for the hash. That way we can check 198 * for freed objects simply by checking the affected bucket. 199 */ 200 static struct debug_bucket *get_bucket(unsigned long addr) 201 { 202 unsigned long hash; 203 204 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS); 205 return &obj_hash[hash]; 206 } 207 208 static void debug_print_object(struct debug_obj *obj, char *msg) 209 { 210 static int limit; 211 212 if (limit < 5 && obj->descr != descr_test) { 213 limit++; 214 WARN(1, KERN_ERR "ODEBUG: %s %s object type: %s\n", msg, 215 obj_states[obj->state], obj->descr->name); 216 } 217 debug_objects_warnings++; 218 } 219 220 /* 221 * Try to repair the damage, so we have a better chance to get useful 222 * debug output. 223 */ 224 static void 225 debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state), 226 void * addr, enum debug_obj_state state) 227 { 228 if (fixup) 229 debug_objects_fixups += fixup(addr, state); 230 } 231 232 static void debug_object_is_on_stack(void *addr, int onstack) 233 { 234 int is_on_stack; 235 static int limit; 236 237 if (limit > 4) 238 return; 239 240 is_on_stack = object_is_on_stack(addr); 241 if (is_on_stack == onstack) 242 return; 243 244 limit++; 245 if (is_on_stack) 246 printk(KERN_WARNING 247 "ODEBUG: object is on stack, but not annotated\n"); 248 else 249 printk(KERN_WARNING 250 "ODEBUG: object is not on stack, but annotated\n"); 251 WARN_ON(1); 252 } 253 254 static void 255 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack) 256 { 257 enum debug_obj_state state; 258 struct debug_bucket *db; 259 struct debug_obj *obj; 260 unsigned long flags; 261 262 fill_pool(); 263 264 db = get_bucket((unsigned long) addr); 265 266 spin_lock_irqsave(&db->lock, flags); 267 268 obj = lookup_object(addr, db); 269 if (!obj) { 270 obj = alloc_object(addr, db, descr); 271 if (!obj) { 272 debug_objects_enabled = 0; 273 spin_unlock_irqrestore(&db->lock, flags); 274 debug_objects_oom(); 275 return; 276 } 277 debug_object_is_on_stack(addr, onstack); 278 } 279 280 switch (obj->state) { 281 case ODEBUG_STATE_NONE: 282 case ODEBUG_STATE_INIT: 283 case ODEBUG_STATE_INACTIVE: 284 obj->state = ODEBUG_STATE_INIT; 285 break; 286 287 case ODEBUG_STATE_ACTIVE: 288 debug_print_object(obj, "init"); 289 state = obj->state; 290 spin_unlock_irqrestore(&db->lock, flags); 291 debug_object_fixup(descr->fixup_init, addr, state); 292 return; 293 294 case ODEBUG_STATE_DESTROYED: 295 debug_print_object(obj, "init"); 296 break; 297 default: 298 break; 299 } 300 301 spin_unlock_irqrestore(&db->lock, flags); 302 } 303 304 /** 305 * debug_object_init - debug checks when an object is initialized 306 * @addr: address of the object 307 * @descr: pointer to an object specific debug description structure 308 */ 309 void debug_object_init(void *addr, struct debug_obj_descr *descr) 310 { 311 if (!debug_objects_enabled) 312 return; 313 314 __debug_object_init(addr, descr, 0); 315 } 316 317 /** 318 * debug_object_init_on_stack - debug checks when an object on stack is 319 * initialized 320 * @addr: address of the object 321 * @descr: pointer to an object specific debug description structure 322 */ 323 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr) 324 { 325 if (!debug_objects_enabled) 326 return; 327 328 __debug_object_init(addr, descr, 1); 329 } 330 331 /** 332 * debug_object_activate - debug checks when an object is activated 333 * @addr: address of the object 334 * @descr: pointer to an object specific debug description structure 335 */ 336 void debug_object_activate(void *addr, struct debug_obj_descr *descr) 337 { 338 enum debug_obj_state state; 339 struct debug_bucket *db; 340 struct debug_obj *obj; 341 unsigned long flags; 342 343 if (!debug_objects_enabled) 344 return; 345 346 db = get_bucket((unsigned long) addr); 347 348 spin_lock_irqsave(&db->lock, flags); 349 350 obj = lookup_object(addr, db); 351 if (obj) { 352 switch (obj->state) { 353 case ODEBUG_STATE_INIT: 354 case ODEBUG_STATE_INACTIVE: 355 obj->state = ODEBUG_STATE_ACTIVE; 356 break; 357 358 case ODEBUG_STATE_ACTIVE: 359 debug_print_object(obj, "activate"); 360 state = obj->state; 361 spin_unlock_irqrestore(&db->lock, flags); 362 debug_object_fixup(descr->fixup_activate, addr, state); 363 return; 364 365 case ODEBUG_STATE_DESTROYED: 366 debug_print_object(obj, "activate"); 367 break; 368 default: 369 break; 370 } 371 spin_unlock_irqrestore(&db->lock, flags); 372 return; 373 } 374 375 spin_unlock_irqrestore(&db->lock, flags); 376 /* 377 * This happens when a static object is activated. We 378 * let the type specific code decide whether this is 379 * true or not. 380 */ 381 debug_object_fixup(descr->fixup_activate, addr, 382 ODEBUG_STATE_NOTAVAILABLE); 383 } 384 385 /** 386 * debug_object_deactivate - debug checks when an object is deactivated 387 * @addr: address of the object 388 * @descr: pointer to an object specific debug description structure 389 */ 390 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr) 391 { 392 struct debug_bucket *db; 393 struct debug_obj *obj; 394 unsigned long flags; 395 396 if (!debug_objects_enabled) 397 return; 398 399 db = get_bucket((unsigned long) addr); 400 401 spin_lock_irqsave(&db->lock, flags); 402 403 obj = lookup_object(addr, db); 404 if (obj) { 405 switch (obj->state) { 406 case ODEBUG_STATE_INIT: 407 case ODEBUG_STATE_INACTIVE: 408 case ODEBUG_STATE_ACTIVE: 409 obj->state = ODEBUG_STATE_INACTIVE; 410 break; 411 412 case ODEBUG_STATE_DESTROYED: 413 debug_print_object(obj, "deactivate"); 414 break; 415 default: 416 break; 417 } 418 } else { 419 struct debug_obj o = { .object = addr, 420 .state = ODEBUG_STATE_NOTAVAILABLE, 421 .descr = descr }; 422 423 debug_print_object(&o, "deactivate"); 424 } 425 426 spin_unlock_irqrestore(&db->lock, flags); 427 } 428 429 /** 430 * debug_object_destroy - debug checks when an object is destroyed 431 * @addr: address of the object 432 * @descr: pointer to an object specific debug description structure 433 */ 434 void debug_object_destroy(void *addr, struct debug_obj_descr *descr) 435 { 436 enum debug_obj_state state; 437 struct debug_bucket *db; 438 struct debug_obj *obj; 439 unsigned long flags; 440 441 if (!debug_objects_enabled) 442 return; 443 444 db = get_bucket((unsigned long) addr); 445 446 spin_lock_irqsave(&db->lock, flags); 447 448 obj = lookup_object(addr, db); 449 if (!obj) 450 goto out_unlock; 451 452 switch (obj->state) { 453 case ODEBUG_STATE_NONE: 454 case ODEBUG_STATE_INIT: 455 case ODEBUG_STATE_INACTIVE: 456 obj->state = ODEBUG_STATE_DESTROYED; 457 break; 458 case ODEBUG_STATE_ACTIVE: 459 debug_print_object(obj, "destroy"); 460 state = obj->state; 461 spin_unlock_irqrestore(&db->lock, flags); 462 debug_object_fixup(descr->fixup_destroy, addr, state); 463 return; 464 465 case ODEBUG_STATE_DESTROYED: 466 debug_print_object(obj, "destroy"); 467 break; 468 default: 469 break; 470 } 471 out_unlock: 472 spin_unlock_irqrestore(&db->lock, flags); 473 } 474 475 /** 476 * debug_object_free - debug checks when an object is freed 477 * @addr: address of the object 478 * @descr: pointer to an object specific debug description structure 479 */ 480 void debug_object_free(void *addr, struct debug_obj_descr *descr) 481 { 482 enum debug_obj_state state; 483 struct debug_bucket *db; 484 struct debug_obj *obj; 485 unsigned long flags; 486 487 if (!debug_objects_enabled) 488 return; 489 490 db = get_bucket((unsigned long) addr); 491 492 spin_lock_irqsave(&db->lock, flags); 493 494 obj = lookup_object(addr, db); 495 if (!obj) 496 goto out_unlock; 497 498 switch (obj->state) { 499 case ODEBUG_STATE_ACTIVE: 500 debug_print_object(obj, "free"); 501 state = obj->state; 502 spin_unlock_irqrestore(&db->lock, flags); 503 debug_object_fixup(descr->fixup_free, addr, state); 504 return; 505 default: 506 hlist_del(&obj->node); 507 spin_unlock_irqrestore(&db->lock, flags); 508 free_object(obj); 509 return; 510 } 511 out_unlock: 512 spin_unlock_irqrestore(&db->lock, flags); 513 } 514 515 #ifdef CONFIG_DEBUG_OBJECTS_FREE 516 static void __debug_check_no_obj_freed(const void *address, unsigned long size) 517 { 518 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks; 519 struct hlist_node *node, *tmp; 520 HLIST_HEAD(freelist); 521 struct debug_obj_descr *descr; 522 enum debug_obj_state state; 523 struct debug_bucket *db; 524 struct debug_obj *obj; 525 int cnt; 526 527 saddr = (unsigned long) address; 528 eaddr = saddr + size; 529 paddr = saddr & ODEBUG_CHUNK_MASK; 530 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1)); 531 chunks >>= ODEBUG_CHUNK_SHIFT; 532 533 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) { 534 db = get_bucket(paddr); 535 536 repeat: 537 cnt = 0; 538 spin_lock_irqsave(&db->lock, flags); 539 hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) { 540 cnt++; 541 oaddr = (unsigned long) obj->object; 542 if (oaddr < saddr || oaddr >= eaddr) 543 continue; 544 545 switch (obj->state) { 546 case ODEBUG_STATE_ACTIVE: 547 debug_print_object(obj, "free"); 548 descr = obj->descr; 549 state = obj->state; 550 spin_unlock_irqrestore(&db->lock, flags); 551 debug_object_fixup(descr->fixup_free, 552 (void *) oaddr, state); 553 goto repeat; 554 default: 555 hlist_del(&obj->node); 556 hlist_add_head(&obj->node, &freelist); 557 break; 558 } 559 } 560 spin_unlock_irqrestore(&db->lock, flags); 561 562 /* Now free them */ 563 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) { 564 hlist_del(&obj->node); 565 free_object(obj); 566 } 567 568 if (cnt > debug_objects_maxchain) 569 debug_objects_maxchain = cnt; 570 } 571 } 572 573 void debug_check_no_obj_freed(const void *address, unsigned long size) 574 { 575 if (debug_objects_enabled) 576 __debug_check_no_obj_freed(address, size); 577 } 578 #endif 579 580 #ifdef CONFIG_DEBUG_FS 581 582 static int debug_stats_show(struct seq_file *m, void *v) 583 { 584 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain); 585 seq_printf(m, "warnings :%d\n", debug_objects_warnings); 586 seq_printf(m, "fixups :%d\n", debug_objects_fixups); 587 seq_printf(m, "pool_free :%d\n", obj_pool_free); 588 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free); 589 seq_printf(m, "pool_used :%d\n", obj_pool_used); 590 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used); 591 return 0; 592 } 593 594 static int debug_stats_open(struct inode *inode, struct file *filp) 595 { 596 return single_open(filp, debug_stats_show, NULL); 597 } 598 599 static const struct file_operations debug_stats_fops = { 600 .open = debug_stats_open, 601 .read = seq_read, 602 .llseek = seq_lseek, 603 .release = single_release, 604 }; 605 606 static int __init debug_objects_init_debugfs(void) 607 { 608 struct dentry *dbgdir, *dbgstats; 609 610 if (!debug_objects_enabled) 611 return 0; 612 613 dbgdir = debugfs_create_dir("debug_objects", NULL); 614 if (!dbgdir) 615 return -ENOMEM; 616 617 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL, 618 &debug_stats_fops); 619 if (!dbgstats) 620 goto err; 621 622 return 0; 623 624 err: 625 debugfs_remove(dbgdir); 626 627 return -ENOMEM; 628 } 629 __initcall(debug_objects_init_debugfs); 630 631 #else 632 static inline void debug_objects_init_debugfs(void) { } 633 #endif 634 635 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST 636 637 /* Random data structure for the self test */ 638 struct self_test { 639 unsigned long dummy1[6]; 640 int static_init; 641 unsigned long dummy2[3]; 642 }; 643 644 static __initdata struct debug_obj_descr descr_type_test; 645 646 /* 647 * fixup_init is called when: 648 * - an active object is initialized 649 */ 650 static int __init fixup_init(void *addr, enum debug_obj_state state) 651 { 652 struct self_test *obj = addr; 653 654 switch (state) { 655 case ODEBUG_STATE_ACTIVE: 656 debug_object_deactivate(obj, &descr_type_test); 657 debug_object_init(obj, &descr_type_test); 658 return 1; 659 default: 660 return 0; 661 } 662 } 663 664 /* 665 * fixup_activate is called when: 666 * - an active object is activated 667 * - an unknown object is activated (might be a statically initialized object) 668 */ 669 static int __init fixup_activate(void *addr, enum debug_obj_state state) 670 { 671 struct self_test *obj = addr; 672 673 switch (state) { 674 case ODEBUG_STATE_NOTAVAILABLE: 675 if (obj->static_init == 1) { 676 debug_object_init(obj, &descr_type_test); 677 debug_object_activate(obj, &descr_type_test); 678 /* 679 * Real code should return 0 here ! This is 680 * not a fixup of some bad behaviour. We 681 * merily call the debug_init function to keep 682 * track of the object. 683 */ 684 return 1; 685 } else { 686 /* Real code needs to emit a warning here */ 687 } 688 return 0; 689 690 case ODEBUG_STATE_ACTIVE: 691 debug_object_deactivate(obj, &descr_type_test); 692 debug_object_activate(obj, &descr_type_test); 693 return 1; 694 695 default: 696 return 0; 697 } 698 } 699 700 /* 701 * fixup_destroy is called when: 702 * - an active object is destroyed 703 */ 704 static int __init fixup_destroy(void *addr, enum debug_obj_state state) 705 { 706 struct self_test *obj = addr; 707 708 switch (state) { 709 case ODEBUG_STATE_ACTIVE: 710 debug_object_deactivate(obj, &descr_type_test); 711 debug_object_destroy(obj, &descr_type_test); 712 return 1; 713 default: 714 return 0; 715 } 716 } 717 718 /* 719 * fixup_free is called when: 720 * - an active object is freed 721 */ 722 static int __init fixup_free(void *addr, enum debug_obj_state state) 723 { 724 struct self_test *obj = addr; 725 726 switch (state) { 727 case ODEBUG_STATE_ACTIVE: 728 debug_object_deactivate(obj, &descr_type_test); 729 debug_object_free(obj, &descr_type_test); 730 return 1; 731 default: 732 return 0; 733 } 734 } 735 736 static int 737 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings) 738 { 739 struct debug_bucket *db; 740 struct debug_obj *obj; 741 unsigned long flags; 742 int res = -EINVAL; 743 744 db = get_bucket((unsigned long) addr); 745 746 spin_lock_irqsave(&db->lock, flags); 747 748 obj = lookup_object(addr, db); 749 if (!obj && state != ODEBUG_STATE_NONE) { 750 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n"); 751 goto out; 752 } 753 if (obj && obj->state != state) { 754 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n", 755 obj->state, state); 756 goto out; 757 } 758 if (fixups != debug_objects_fixups) { 759 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n", 760 fixups, debug_objects_fixups); 761 goto out; 762 } 763 if (warnings != debug_objects_warnings) { 764 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n", 765 warnings, debug_objects_warnings); 766 goto out; 767 } 768 res = 0; 769 out: 770 spin_unlock_irqrestore(&db->lock, flags); 771 if (res) 772 debug_objects_enabled = 0; 773 return res; 774 } 775 776 static __initdata struct debug_obj_descr descr_type_test = { 777 .name = "selftest", 778 .fixup_init = fixup_init, 779 .fixup_activate = fixup_activate, 780 .fixup_destroy = fixup_destroy, 781 .fixup_free = fixup_free, 782 }; 783 784 static __initdata struct self_test obj = { .static_init = 0 }; 785 786 static void __init debug_objects_selftest(void) 787 { 788 int fixups, oldfixups, warnings, oldwarnings; 789 unsigned long flags; 790 791 local_irq_save(flags); 792 793 fixups = oldfixups = debug_objects_fixups; 794 warnings = oldwarnings = debug_objects_warnings; 795 descr_test = &descr_type_test; 796 797 debug_object_init(&obj, &descr_type_test); 798 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings)) 799 goto out; 800 debug_object_activate(&obj, &descr_type_test); 801 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) 802 goto out; 803 debug_object_activate(&obj, &descr_type_test); 804 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings)) 805 goto out; 806 debug_object_deactivate(&obj, &descr_type_test); 807 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings)) 808 goto out; 809 debug_object_destroy(&obj, &descr_type_test); 810 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings)) 811 goto out; 812 debug_object_init(&obj, &descr_type_test); 813 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) 814 goto out; 815 debug_object_activate(&obj, &descr_type_test); 816 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) 817 goto out; 818 debug_object_deactivate(&obj, &descr_type_test); 819 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) 820 goto out; 821 debug_object_free(&obj, &descr_type_test); 822 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings)) 823 goto out; 824 825 obj.static_init = 1; 826 debug_object_activate(&obj, &descr_type_test); 827 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings)) 828 goto out; 829 debug_object_init(&obj, &descr_type_test); 830 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings)) 831 goto out; 832 debug_object_free(&obj, &descr_type_test); 833 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings)) 834 goto out; 835 836 #ifdef CONFIG_DEBUG_OBJECTS_FREE 837 debug_object_init(&obj, &descr_type_test); 838 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings)) 839 goto out; 840 debug_object_activate(&obj, &descr_type_test); 841 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) 842 goto out; 843 __debug_check_no_obj_freed(&obj, sizeof(obj)); 844 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings)) 845 goto out; 846 #endif 847 printk(KERN_INFO "ODEBUG: selftest passed\n"); 848 849 out: 850 debug_objects_fixups = oldfixups; 851 debug_objects_warnings = oldwarnings; 852 descr_test = NULL; 853 854 local_irq_restore(flags); 855 } 856 #else 857 static inline void debug_objects_selftest(void) { } 858 #endif 859 860 /* 861 * Called during early boot to initialize the hash buckets and link 862 * the static object pool objects into the poll list. After this call 863 * the object tracker is fully operational. 864 */ 865 void __init debug_objects_early_init(void) 866 { 867 int i; 868 869 for (i = 0; i < ODEBUG_HASH_SIZE; i++) 870 spin_lock_init(&obj_hash[i].lock); 871 872 for (i = 0; i < ODEBUG_POOL_SIZE; i++) 873 hlist_add_head(&obj_static_pool[i].node, &obj_pool); 874 } 875 876 /* 877 * Called after the kmem_caches are functional to setup a dedicated 878 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag 879 * prevents that the debug code is called on kmem_cache_free() for the 880 * debug tracker objects to avoid recursive calls. 881 */ 882 void __init debug_objects_mem_init(void) 883 { 884 if (!debug_objects_enabled) 885 return; 886 887 obj_cache = kmem_cache_create("debug_objects_cache", 888 sizeof (struct debug_obj), 0, 889 SLAB_DEBUG_OBJECTS, NULL); 890 891 if (!obj_cache) 892 debug_objects_enabled = 0; 893 else 894 debug_objects_selftest(); 895 } 896