debugobjects.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. int is_on_stack;
  191. static int limit;
  192. if (limit > 4)
  193. return;
  194. is_on_stack = object_is_on_stack(addr);
  195. if (is_on_stack == onstack)
  196. return;
  197. limit++;
  198. if (is_on_stack)
  199. printk(KERN_WARNING
  200. "ODEBUG: object is on stack, but not annotated\n");
  201. else
  202. printk(KERN_WARNING
  203. "ODEBUG: object is not on stack, but annotated\n");
  204. WARN_ON(1);
  205. }
  206. static void
  207. __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
  208. {
  209. enum debug_obj_state state;
  210. struct debug_bucket *db;
  211. struct debug_obj *obj;
  212. unsigned long flags;
  213. fill_pool();
  214. db = get_bucket((unsigned long) addr);
  215. spin_lock_irqsave(&db->lock, flags);
  216. obj = lookup_object(addr, db);
  217. if (!obj) {
  218. obj = alloc_object(addr, db, descr);
  219. if (!obj) {
  220. debug_objects_enabled = 0;
  221. spin_unlock_irqrestore(&db->lock, flags);
  222. debug_objects_oom();
  223. return;
  224. }
  225. debug_object_is_on_stack(addr, onstack);
  226. }
  227. switch (obj->state) {
  228. case ODEBUG_STATE_NONE:
  229. case ODEBUG_STATE_INIT:
  230. case ODEBUG_STATE_INACTIVE:
  231. obj->state = ODEBUG_STATE_INIT;
  232. break;
  233. case ODEBUG_STATE_ACTIVE:
  234. debug_print_object(obj, "init");
  235. state = obj->state;
  236. spin_unlock_irqrestore(&db->lock, flags);
  237. debug_object_fixup(descr->fixup_init, addr, state);
  238. return;
  239. case ODEBUG_STATE_DESTROYED:
  240. debug_print_object(obj, "init");
  241. break;
  242. default:
  243. break;
  244. }
  245. spin_unlock_irqrestore(&db->lock, flags);
  246. }
  247. /**
  248. * debug_object_init - debug checks when an object is initialized
  249. * @addr: address of the object
  250. * @descr: pointer to an object specific debug description structure
  251. */
  252. void debug_object_init(void *addr, struct debug_obj_descr *descr)
  253. {
  254. if (!debug_objects_enabled)
  255. return;
  256. __debug_object_init(addr, descr, 0);
  257. }
  258. /**
  259. * debug_object_init_on_stack - debug checks when an object on stack is
  260. * initialized
  261. * @addr: address of the object
  262. * @descr: pointer to an object specific debug description structure
  263. */
  264. void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
  265. {
  266. if (!debug_objects_enabled)
  267. return;
  268. __debug_object_init(addr, descr, 1);
  269. }
  270. /**
  271. * debug_object_activate - debug checks when an object is activated
  272. * @addr: address of the object
  273. * @descr: pointer to an object specific debug description structure
  274. */
  275. void debug_object_activate(void *addr, struct debug_obj_descr *descr)
  276. {
  277. enum debug_obj_state state;
  278. struct debug_bucket *db;
  279. struct debug_obj *obj;
  280. unsigned long flags;
  281. if (!debug_objects_enabled)
  282. return;
  283. db = get_bucket((unsigned long) addr);
  284. spin_lock_irqsave(&db->lock, flags);
  285. obj = lookup_object(addr, db);
  286. if (obj) {
  287. switch (obj->state) {
  288. case ODEBUG_STATE_INIT:
  289. case ODEBUG_STATE_INACTIVE:
  290. obj->state = ODEBUG_STATE_ACTIVE;
  291. break;
  292. case ODEBUG_STATE_ACTIVE:
  293. debug_print_object(obj, "activate");
  294. state = obj->state;
  295. spin_unlock_irqrestore(&db->lock, flags);
  296. debug_object_fixup(descr->fixup_activate, addr, state);
  297. return;
  298. case ODEBUG_STATE_DESTROYED:
  299. debug_print_object(obj, "activate");
  300. break;
  301. default:
  302. break;
  303. }
  304. spin_unlock_irqrestore(&db->lock, flags);
  305. return;
  306. }
  307. spin_unlock_irqrestore(&db->lock, flags);
  308. /*
  309. * This happens when a static object is activated. We
  310. * let the type specific code decide whether this is
  311. * true or not.
  312. */
  313. debug_object_fixup(descr->fixup_activate, addr,
  314. ODEBUG_STATE_NOTAVAILABLE);
  315. }
  316. /**
  317. * debug_object_deactivate - debug checks when an object is deactivated
  318. * @addr: address of the object
  319. * @descr: pointer to an object specific debug description structure
  320. */
  321. void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
  322. {
  323. struct debug_bucket *db;
  324. struct debug_obj *obj;
  325. unsigned long flags;
  326. if (!debug_objects_enabled)
  327. return;
  328. db = get_bucket((unsigned long) addr);
  329. spin_lock_irqsave(&db->lock, flags);
  330. obj = lookup_object(addr, db);
  331. if (obj) {
  332. switch (obj->state) {
  333. case ODEBUG_STATE_INIT:
  334. case ODEBUG_STATE_INACTIVE:
  335. case ODEBUG_STATE_ACTIVE:
  336. obj->state = ODEBUG_STATE_INACTIVE;
  337. break;
  338. case ODEBUG_STATE_DESTROYED:
  339. debug_print_object(obj, "deactivate");
  340. break;
  341. default:
  342. break;
  343. }
  344. } else {
  345. struct debug_obj o = { .object = addr,
  346. .state = ODEBUG_STATE_NOTAVAILABLE,
  347. .descr = descr };
  348. debug_print_object(&o, "deactivate");
  349. }
  350. spin_unlock_irqrestore(&db->lock, flags);
  351. }
  352. /**
  353. * debug_object_destroy - debug checks when an object is destroyed
  354. * @addr: address of the object
  355. * @descr: pointer to an object specific debug description structure
  356. */
  357. void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
  358. {
  359. enum debug_obj_state state;
  360. struct debug_bucket *db;
  361. struct debug_obj *obj;
  362. unsigned long flags;
  363. if (!debug_objects_enabled)
  364. return;
  365. db = get_bucket((unsigned long) addr);
  366. spin_lock_irqsave(&db->lock, flags);
  367. obj = lookup_object(addr, db);
  368. if (!obj)
  369. goto out_unlock;
  370. switch (obj->state) {
  371. case ODEBUG_STATE_NONE:
  372. case ODEBUG_STATE_INIT:
  373. case ODEBUG_STATE_INACTIVE:
  374. obj->state = ODEBUG_STATE_DESTROYED;
  375. break;
  376. case ODEBUG_STATE_ACTIVE:
  377. debug_print_object(obj, "destroy");
  378. state = obj->state;
  379. spin_unlock_irqrestore(&db->lock, flags);
  380. debug_object_fixup(descr->fixup_destroy, addr, state);
  381. return;
  382. case ODEBUG_STATE_DESTROYED:
  383. debug_print_object(obj, "destroy");
  384. break;
  385. default:
  386. break;
  387. }
  388. out_unlock:
  389. spin_unlock_irqrestore(&db->lock, flags);
  390. }
  391. /**
  392. * debug_object_free - debug checks when an object is freed
  393. * @addr: address of the object
  394. * @descr: pointer to an object specific debug description structure
  395. */
  396. void debug_object_free(void *addr, struct debug_obj_descr *descr)
  397. {
  398. enum debug_obj_state state;
  399. struct debug_bucket *db;
  400. struct debug_obj *obj;
  401. unsigned long flags;
  402. if (!debug_objects_enabled)
  403. return;
  404. db = get_bucket((unsigned long) addr);
  405. spin_lock_irqsave(&db->lock, flags);
  406. obj = lookup_object(addr, db);
  407. if (!obj)
  408. goto out_unlock;
  409. switch (obj->state) {
  410. case ODEBUG_STATE_ACTIVE:
  411. debug_print_object(obj, "free");
  412. state = obj->state;
  413. spin_unlock_irqrestore(&db->lock, flags);
  414. debug_object_fixup(descr->fixup_free, addr, state);
  415. return;
  416. default:
  417. hlist_del(&obj->node);
  418. free_object(obj);
  419. break;
  420. }
  421. out_unlock:
  422. spin_unlock_irqrestore(&db->lock, flags);
  423. }
  424. #ifdef CONFIG_DEBUG_OBJECTS_FREE
  425. static void __debug_check_no_obj_freed(const void *address, unsigned long size)
  426. {
  427. unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
  428. struct hlist_node *node, *tmp;
  429. struct debug_obj_descr *descr;
  430. enum debug_obj_state state;
  431. struct debug_bucket *db;
  432. struct debug_obj *obj;
  433. int cnt;
  434. saddr = (unsigned long) address;
  435. eaddr = saddr + size;
  436. paddr = saddr & ODEBUG_CHUNK_MASK;
  437. chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
  438. chunks >>= ODEBUG_CHUNK_SHIFT;
  439. for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
  440. db = get_bucket(paddr);
  441. repeat:
  442. cnt = 0;
  443. spin_lock_irqsave(&db->lock, flags);
  444. hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
  445. cnt++;
  446. oaddr = (unsigned long) obj->object;
  447. if (oaddr < saddr || oaddr >= eaddr)
  448. continue;
  449. switch (obj->state) {
  450. case ODEBUG_STATE_ACTIVE:
  451. debug_print_object(obj, "free");
  452. descr = obj->descr;
  453. state = obj->state;
  454. spin_unlock_irqrestore(&db->lock, flags);
  455. debug_object_fixup(descr->fixup_free,
  456. (void *) oaddr, state);
  457. goto repeat;
  458. default:
  459. hlist_del(&obj->node);
  460. free_object(obj);
  461. break;
  462. }
  463. }
  464. spin_unlock_irqrestore(&db->lock, flags);
  465. if (cnt > debug_objects_maxchain)
  466. debug_objects_maxchain = cnt;
  467. }
  468. }
  469. void debug_check_no_obj_freed(const void *address, unsigned long size)
  470. {
  471. if (debug_objects_enabled)
  472. __debug_check_no_obj_freed(address, size);
  473. }
  474. #endif
  475. #ifdef CONFIG_DEBUG_FS
  476. static int debug_stats_show(struct seq_file *m, void *v)
  477. {
  478. seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
  479. seq_printf(m, "warnings :%d\n", debug_objects_warnings);
  480. seq_printf(m, "fixups :%d\n", debug_objects_fixups);
  481. seq_printf(m, "pool_free :%d\n", obj_pool_free);
  482. seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
  483. seq_printf(m, "pool_used :%d\n", obj_pool_used);
  484. seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
  485. return 0;
  486. }
  487. static int debug_stats_open(struct inode *inode, struct file *filp)
  488. {
  489. return single_open(filp, debug_stats_show, NULL);
  490. }
  491. static const struct file_operations debug_stats_fops = {
  492. .open = debug_stats_open,
  493. .read = seq_read,
  494. .llseek = seq_lseek,
  495. .release = single_release,
  496. };
  497. static int __init debug_objects_init_debugfs(void)
  498. {
  499. struct dentry *dbgdir, *dbgstats;
  500. if (!debug_objects_enabled)
  501. return 0;
  502. dbgdir = debugfs_create_dir("debug_objects", NULL);
  503. if (!dbgdir)
  504. return -ENOMEM;
  505. dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
  506. &debug_stats_fops);
  507. if (!dbgstats)
  508. goto err;
  509. return 0;
  510. err:
  511. debugfs_remove(dbgdir);
  512. return -ENOMEM;
  513. }
  514. __initcall(debug_objects_init_debugfs);
  515. #else
  516. static inline void debug_objects_init_debugfs(void) { }
  517. #endif
  518. #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
  519. /* Random data structure for the self test */
  520. struct self_test {
  521. unsigned long dummy1[6];
  522. int static_init;
  523. unsigned long dummy2[3];
  524. };
  525. static __initdata struct debug_obj_descr descr_type_test;
  526. /*
  527. * fixup_init is called when:
  528. * - an active object is initialized
  529. */
  530. static int __init fixup_init(void *addr, enum debug_obj_state state)
  531. {
  532. struct self_test *obj = addr;
  533. switch (state) {
  534. case ODEBUG_STATE_ACTIVE:
  535. debug_object_deactivate(obj, &descr_type_test);
  536. debug_object_init(obj, &descr_type_test);
  537. return 1;
  538. default:
  539. return 0;
  540. }
  541. }
  542. /*
  543. * fixup_activate is called when:
  544. * - an active object is activated
  545. * - an unknown object is activated (might be a statically initialized object)
  546. */
  547. static int __init fixup_activate(void *addr, enum debug_obj_state state)
  548. {
  549. struct self_test *obj = addr;
  550. switch (state) {
  551. case ODEBUG_STATE_NOTAVAILABLE:
  552. if (obj->static_init == 1) {
  553. debug_object_init(obj, &descr_type_test);
  554. debug_object_activate(obj, &descr_type_test);
  555. /*
  556. * Real code should return 0 here ! This is
  557. * not a fixup of some bad behaviour. We
  558. * merily call the debug_init function to keep
  559. * track of the object.
  560. */
  561. return 1;
  562. } else {
  563. /* Real code needs to emit a warning here */
  564. }
  565. return 0;
  566. case ODEBUG_STATE_ACTIVE:
  567. debug_object_deactivate(obj, &descr_type_test);
  568. debug_object_activate(obj, &descr_type_test);
  569. return 1;
  570. default:
  571. return 0;
  572. }
  573. }
  574. /*
  575. * fixup_destroy is called when:
  576. * - an active object is destroyed
  577. */
  578. static int __init fixup_destroy(void *addr, enum debug_obj_state state)
  579. {
  580. struct self_test *obj = addr;
  581. switch (state) {
  582. case ODEBUG_STATE_ACTIVE:
  583. debug_object_deactivate(obj, &descr_type_test);
  584. debug_object_destroy(obj, &descr_type_test);
  585. return 1;
  586. default:
  587. return 0;
  588. }
  589. }
  590. /*
  591. * fixup_free is called when:
  592. * - an active object is freed
  593. */
  594. static int __init fixup_free(void *addr, enum debug_obj_state state)
  595. {
  596. struct self_test *obj = addr;
  597. switch (state) {
  598. case ODEBUG_STATE_ACTIVE:
  599. debug_object_deactivate(obj, &descr_type_test);
  600. debug_object_free(obj, &descr_type_test);
  601. return 1;
  602. default:
  603. return 0;
  604. }
  605. }
  606. static int
  607. check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
  608. {
  609. struct debug_bucket *db;
  610. struct debug_obj *obj;
  611. unsigned long flags;
  612. int res = -EINVAL;
  613. db = get_bucket((unsigned long) addr);
  614. spin_lock_irqsave(&db->lock, flags);
  615. obj = lookup_object(addr, db);
  616. if (!obj && state != ODEBUG_STATE_NONE) {
  617. printk(KERN_ERR "ODEBUG: selftest object not found\n");
  618. WARN_ON(1);
  619. goto out;
  620. }
  621. if (obj && obj->state != state) {
  622. printk(KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
  623. obj->state, state);
  624. WARN_ON(1);
  625. goto out;
  626. }
  627. if (fixups != debug_objects_fixups) {
  628. printk(KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
  629. fixups, debug_objects_fixups);
  630. WARN_ON(1);
  631. goto out;
  632. }
  633. if (warnings != debug_objects_warnings) {
  634. printk(KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
  635. warnings, debug_objects_warnings);
  636. WARN_ON(1);
  637. goto out;
  638. }
  639. res = 0;
  640. out:
  641. spin_unlock_irqrestore(&db->lock, flags);
  642. if (res)
  643. debug_objects_enabled = 0;
  644. return res;
  645. }
  646. static __initdata struct debug_obj_descr descr_type_test = {
  647. .name = "selftest",
  648. .fixup_init = fixup_init,
  649. .fixup_activate = fixup_activate,
  650. .fixup_destroy = fixup_destroy,
  651. .fixup_free = fixup_free,
  652. };
  653. static __initdata struct self_test obj = { .static_init = 0 };
  654. static void __init debug_objects_selftest(void)
  655. {
  656. int fixups, oldfixups, warnings, oldwarnings;
  657. unsigned long flags;
  658. local_irq_save(flags);
  659. fixups = oldfixups = debug_objects_fixups;
  660. warnings = oldwarnings = debug_objects_warnings;
  661. descr_test = &descr_type_test;
  662. debug_object_init(&obj, &descr_type_test);
  663. if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
  664. goto out;
  665. debug_object_activate(&obj, &descr_type_test);
  666. if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  667. goto out;
  668. debug_object_activate(&obj, &descr_type_test);
  669. if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
  670. goto out;
  671. debug_object_deactivate(&obj, &descr_type_test);
  672. if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
  673. goto out;
  674. debug_object_destroy(&obj, &descr_type_test);
  675. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
  676. goto out;
  677. debug_object_init(&obj, &descr_type_test);
  678. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  679. goto out;
  680. debug_object_activate(&obj, &descr_type_test);
  681. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  682. goto out;
  683. debug_object_deactivate(&obj, &descr_type_test);
  684. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  685. goto out;
  686. debug_object_free(&obj, &descr_type_test);
  687. if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
  688. goto out;
  689. obj.static_init = 1;
  690. debug_object_activate(&obj, &descr_type_test);
  691. if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
  692. goto out;
  693. debug_object_init(&obj, &descr_type_test);
  694. if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
  695. goto out;
  696. debug_object_free(&obj, &descr_type_test);
  697. if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
  698. goto out;
  699. #ifdef CONFIG_DEBUG_OBJECTS_FREE
  700. debug_object_init(&obj, &descr_type_test);
  701. if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
  702. goto out;
  703. debug_object_activate(&obj, &descr_type_test);
  704. if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  705. goto out;
  706. __debug_check_no_obj_freed(&obj, sizeof(obj));
  707. if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
  708. goto out;
  709. #endif
  710. printk(KERN_INFO "ODEBUG: selftest passed\n");
  711. out:
  712. debug_objects_fixups = oldfixups;
  713. debug_objects_warnings = oldwarnings;
  714. descr_test = NULL;
  715. local_irq_restore(flags);
  716. }
  717. #else
  718. static inline void debug_objects_selftest(void) { }
  719. #endif
  720. /*
  721. * Called during early boot to initialize the hash buckets and link
  722. * the static object pool objects into the poll list. After this call
  723. * the object tracker is fully operational.
  724. */
  725. void __init debug_objects_early_init(void)
  726. {
  727. int i;
  728. for (i = 0; i < ODEBUG_HASH_SIZE; i++)
  729. spin_lock_init(&obj_hash[i].lock);
  730. for (i = 0; i < ODEBUG_POOL_SIZE; i++)
  731. hlist_add_head(&obj_static_pool[i].node, &obj_pool);
  732. }
  733. /*
  734. * Called after the kmem_caches are functional to setup a dedicated
  735. * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
  736. * prevents that the debug code is called on kmem_cache_free() for the
  737. * debug tracker objects to avoid recursive calls.
  738. */
  739. void __init debug_objects_mem_init(void)
  740. {
  741. if (!debug_objects_enabled)
  742. return;
  743. obj_cache = kmem_cache_create("debug_objects_cache",
  744. sizeof (struct debug_obj), 0,
  745. SLAB_DEBUG_OBJECTS, NULL);
  746. if (!obj_cache)
  747. debug_objects_enabled = 0;
  748. else
  749. debug_objects_selftest();
  750. }