debugobjects.c 21 KB

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