debugobjects.c 21 KB

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