debugobjects.c 21 KB

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