debugobjects.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. * Must be called with interrupts disabled.
  94. */
  95. static struct debug_obj *
  96. alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
  97. {
  98. struct debug_obj *obj = NULL;
  99. spin_lock(&pool_lock);
  100. if (obj_pool.first) {
  101. obj = hlist_entry(obj_pool.first, typeof(*obj), node);
  102. obj->object = addr;
  103. obj->descr = descr;
  104. obj->state = ODEBUG_STATE_NONE;
  105. hlist_del(&obj->node);
  106. hlist_add_head(&obj->node, &b->list);
  107. obj_pool_used++;
  108. if (obj_pool_used > obj_pool_max_used)
  109. obj_pool_max_used = obj_pool_used;
  110. obj_pool_free--;
  111. if (obj_pool_free < obj_pool_min_free)
  112. obj_pool_min_free = obj_pool_free;
  113. }
  114. spin_unlock(&pool_lock);
  115. return obj;
  116. }
  117. /*
  118. * Put the object back into the pool or give it back to kmem_cache:
  119. */
  120. static void free_object(struct debug_obj *obj)
  121. {
  122. unsigned long idx = (unsigned long)(obj - obj_static_pool);
  123. unsigned long flags;
  124. if (obj_pool_free < ODEBUG_POOL_SIZE || idx < ODEBUG_POOL_SIZE) {
  125. spin_lock_irqsave(&pool_lock, flags);
  126. hlist_add_head(&obj->node, &obj_pool);
  127. obj_pool_free++;
  128. obj_pool_used--;
  129. spin_unlock_irqrestore(&pool_lock, flags);
  130. } else {
  131. spin_lock_irqsave(&pool_lock, flags);
  132. obj_pool_used--;
  133. spin_unlock_irqrestore(&pool_lock, flags);
  134. kmem_cache_free(obj_cache, obj);
  135. }
  136. }
  137. /*
  138. * We run out of memory. That means we probably have tons of objects
  139. * allocated.
  140. */
  141. static void debug_objects_oom(void)
  142. {
  143. struct debug_bucket *db = obj_hash;
  144. struct hlist_node *node, *tmp;
  145. HLIST_HEAD(freelist);
  146. struct debug_obj *obj;
  147. unsigned long flags;
  148. int i;
  149. printk(KERN_WARNING "ODEBUG: Out of memory. ODEBUG disabled\n");
  150. for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
  151. spin_lock_irqsave(&db->lock, flags);
  152. hlist_move_list(&db->list, &freelist);
  153. spin_unlock_irqrestore(&db->lock, flags);
  154. /* Now free them */
  155. hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
  156. hlist_del(&obj->node);
  157. free_object(obj);
  158. }
  159. }
  160. }
  161. /*
  162. * We use the pfn of the address for the hash. That way we can check
  163. * for freed objects simply by checking the affected bucket.
  164. */
  165. static struct debug_bucket *get_bucket(unsigned long addr)
  166. {
  167. unsigned long hash;
  168. hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
  169. return &obj_hash[hash];
  170. }
  171. static void debug_print_object(struct debug_obj *obj, char *msg)
  172. {
  173. static int limit;
  174. if (limit < 5 && obj->descr != descr_test) {
  175. limit++;
  176. WARN(1, KERN_ERR "ODEBUG: %s %s object type: %s\n", msg,
  177. obj_states[obj->state], obj->descr->name);
  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. int is_on_stack;
  195. static int limit;
  196. if (limit > 4)
  197. return;
  198. is_on_stack = object_is_on_stack(addr);
  199. if (is_on_stack == onstack)
  200. return;
  201. limit++;
  202. if (is_on_stack)
  203. printk(KERN_WARNING
  204. "ODEBUG: object is on stack, but not annotated\n");
  205. else
  206. printk(KERN_WARNING
  207. "ODEBUG: object is not on stack, but annotated\n");
  208. WARN_ON(1);
  209. }
  210. static void
  211. __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
  212. {
  213. enum debug_obj_state state;
  214. struct debug_bucket *db;
  215. struct debug_obj *obj;
  216. unsigned long flags;
  217. fill_pool();
  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. spin_unlock_irqrestore(&db->lock, flags);
  423. free_object(obj);
  424. return;
  425. }
  426. out_unlock:
  427. spin_unlock_irqrestore(&db->lock, flags);
  428. }
  429. #ifdef CONFIG_DEBUG_OBJECTS_FREE
  430. static void __debug_check_no_obj_freed(const void *address, unsigned long size)
  431. {
  432. unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
  433. struct hlist_node *node, *tmp;
  434. HLIST_HEAD(freelist);
  435. struct debug_obj_descr *descr;
  436. enum debug_obj_state state;
  437. struct debug_bucket *db;
  438. struct debug_obj *obj;
  439. int cnt;
  440. saddr = (unsigned long) address;
  441. eaddr = saddr + size;
  442. paddr = saddr & ODEBUG_CHUNK_MASK;
  443. chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
  444. chunks >>= ODEBUG_CHUNK_SHIFT;
  445. for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
  446. db = get_bucket(paddr);
  447. repeat:
  448. cnt = 0;
  449. spin_lock_irqsave(&db->lock, flags);
  450. hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
  451. cnt++;
  452. oaddr = (unsigned long) obj->object;
  453. if (oaddr < saddr || oaddr >= eaddr)
  454. continue;
  455. switch (obj->state) {
  456. case ODEBUG_STATE_ACTIVE:
  457. debug_print_object(obj, "free");
  458. descr = obj->descr;
  459. state = obj->state;
  460. spin_unlock_irqrestore(&db->lock, flags);
  461. debug_object_fixup(descr->fixup_free,
  462. (void *) oaddr, state);
  463. goto repeat;
  464. default:
  465. hlist_del(&obj->node);
  466. hlist_add_head(&obj->node, &freelist);
  467. break;
  468. }
  469. }
  470. spin_unlock_irqrestore(&db->lock, flags);
  471. /* Now free them */
  472. hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
  473. hlist_del(&obj->node);
  474. free_object(obj);
  475. }
  476. if (cnt > debug_objects_maxchain)
  477. debug_objects_maxchain = cnt;
  478. }
  479. }
  480. void debug_check_no_obj_freed(const void *address, unsigned long size)
  481. {
  482. if (debug_objects_enabled)
  483. __debug_check_no_obj_freed(address, size);
  484. }
  485. #endif
  486. #ifdef CONFIG_DEBUG_FS
  487. static int debug_stats_show(struct seq_file *m, void *v)
  488. {
  489. seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
  490. seq_printf(m, "warnings :%d\n", debug_objects_warnings);
  491. seq_printf(m, "fixups :%d\n", debug_objects_fixups);
  492. seq_printf(m, "pool_free :%d\n", obj_pool_free);
  493. seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
  494. seq_printf(m, "pool_used :%d\n", obj_pool_used);
  495. seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
  496. return 0;
  497. }
  498. static int debug_stats_open(struct inode *inode, struct file *filp)
  499. {
  500. return single_open(filp, debug_stats_show, NULL);
  501. }
  502. static const struct file_operations debug_stats_fops = {
  503. .open = debug_stats_open,
  504. .read = seq_read,
  505. .llseek = seq_lseek,
  506. .release = single_release,
  507. };
  508. static int __init debug_objects_init_debugfs(void)
  509. {
  510. struct dentry *dbgdir, *dbgstats;
  511. if (!debug_objects_enabled)
  512. return 0;
  513. dbgdir = debugfs_create_dir("debug_objects", NULL);
  514. if (!dbgdir)
  515. return -ENOMEM;
  516. dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
  517. &debug_stats_fops);
  518. if (!dbgstats)
  519. goto err;
  520. return 0;
  521. err:
  522. debugfs_remove(dbgdir);
  523. return -ENOMEM;
  524. }
  525. __initcall(debug_objects_init_debugfs);
  526. #else
  527. static inline void debug_objects_init_debugfs(void) { }
  528. #endif
  529. #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
  530. /* Random data structure for the self test */
  531. struct self_test {
  532. unsigned long dummy1[6];
  533. int static_init;
  534. unsigned long dummy2[3];
  535. };
  536. static __initdata struct debug_obj_descr descr_type_test;
  537. /*
  538. * fixup_init is called when:
  539. * - an active object is initialized
  540. */
  541. static int __init fixup_init(void *addr, enum debug_obj_state state)
  542. {
  543. struct self_test *obj = addr;
  544. switch (state) {
  545. case ODEBUG_STATE_ACTIVE:
  546. debug_object_deactivate(obj, &descr_type_test);
  547. debug_object_init(obj, &descr_type_test);
  548. return 1;
  549. default:
  550. return 0;
  551. }
  552. }
  553. /*
  554. * fixup_activate is called when:
  555. * - an active object is activated
  556. * - an unknown object is activated (might be a statically initialized object)
  557. */
  558. static int __init fixup_activate(void *addr, enum debug_obj_state state)
  559. {
  560. struct self_test *obj = addr;
  561. switch (state) {
  562. case ODEBUG_STATE_NOTAVAILABLE:
  563. if (obj->static_init == 1) {
  564. debug_object_init(obj, &descr_type_test);
  565. debug_object_activate(obj, &descr_type_test);
  566. /*
  567. * Real code should return 0 here ! This is
  568. * not a fixup of some bad behaviour. We
  569. * merily call the debug_init function to keep
  570. * track of the object.
  571. */
  572. return 1;
  573. } else {
  574. /* Real code needs to emit a warning here */
  575. }
  576. return 0;
  577. case ODEBUG_STATE_ACTIVE:
  578. debug_object_deactivate(obj, &descr_type_test);
  579. debug_object_activate(obj, &descr_type_test);
  580. return 1;
  581. default:
  582. return 0;
  583. }
  584. }
  585. /*
  586. * fixup_destroy is called when:
  587. * - an active object is destroyed
  588. */
  589. static int __init fixup_destroy(void *addr, enum debug_obj_state state)
  590. {
  591. struct self_test *obj = addr;
  592. switch (state) {
  593. case ODEBUG_STATE_ACTIVE:
  594. debug_object_deactivate(obj, &descr_type_test);
  595. debug_object_destroy(obj, &descr_type_test);
  596. return 1;
  597. default:
  598. return 0;
  599. }
  600. }
  601. /*
  602. * fixup_free is called when:
  603. * - an active object is freed
  604. */
  605. static int __init fixup_free(void *addr, enum debug_obj_state state)
  606. {
  607. struct self_test *obj = addr;
  608. switch (state) {
  609. case ODEBUG_STATE_ACTIVE:
  610. debug_object_deactivate(obj, &descr_type_test);
  611. debug_object_free(obj, &descr_type_test);
  612. return 1;
  613. default:
  614. return 0;
  615. }
  616. }
  617. static int
  618. check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
  619. {
  620. struct debug_bucket *db;
  621. struct debug_obj *obj;
  622. unsigned long flags;
  623. int res = -EINVAL;
  624. db = get_bucket((unsigned long) addr);
  625. spin_lock_irqsave(&db->lock, flags);
  626. obj = lookup_object(addr, db);
  627. if (!obj && state != ODEBUG_STATE_NONE) {
  628. WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
  629. goto out;
  630. }
  631. if (obj && obj->state != state) {
  632. WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
  633. obj->state, state);
  634. goto out;
  635. }
  636. if (fixups != debug_objects_fixups) {
  637. WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
  638. fixups, debug_objects_fixups);
  639. goto out;
  640. }
  641. if (warnings != debug_objects_warnings) {
  642. WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
  643. warnings, debug_objects_warnings);
  644. goto out;
  645. }
  646. res = 0;
  647. out:
  648. spin_unlock_irqrestore(&db->lock, flags);
  649. if (res)
  650. debug_objects_enabled = 0;
  651. return res;
  652. }
  653. static __initdata struct debug_obj_descr descr_type_test = {
  654. .name = "selftest",
  655. .fixup_init = fixup_init,
  656. .fixup_activate = fixup_activate,
  657. .fixup_destroy = fixup_destroy,
  658. .fixup_free = fixup_free,
  659. };
  660. static __initdata struct self_test obj = { .static_init = 0 };
  661. static void __init debug_objects_selftest(void)
  662. {
  663. int fixups, oldfixups, warnings, oldwarnings;
  664. unsigned long flags;
  665. local_irq_save(flags);
  666. fixups = oldfixups = debug_objects_fixups;
  667. warnings = oldwarnings = debug_objects_warnings;
  668. descr_test = &descr_type_test;
  669. debug_object_init(&obj, &descr_type_test);
  670. if (check_results(&obj, ODEBUG_STATE_INIT, 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_activate(&obj, &descr_type_test);
  676. if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
  677. goto out;
  678. debug_object_deactivate(&obj, &descr_type_test);
  679. if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
  680. goto out;
  681. debug_object_destroy(&obj, &descr_type_test);
  682. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
  683. goto out;
  684. debug_object_init(&obj, &descr_type_test);
  685. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  686. goto out;
  687. debug_object_activate(&obj, &descr_type_test);
  688. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  689. goto out;
  690. debug_object_deactivate(&obj, &descr_type_test);
  691. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  692. goto out;
  693. debug_object_free(&obj, &descr_type_test);
  694. if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
  695. goto out;
  696. obj.static_init = 1;
  697. debug_object_activate(&obj, &descr_type_test);
  698. if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
  699. goto out;
  700. debug_object_init(&obj, &descr_type_test);
  701. if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
  702. goto out;
  703. debug_object_free(&obj, &descr_type_test);
  704. if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
  705. goto out;
  706. #ifdef CONFIG_DEBUG_OBJECTS_FREE
  707. debug_object_init(&obj, &descr_type_test);
  708. if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
  709. goto out;
  710. debug_object_activate(&obj, &descr_type_test);
  711. if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  712. goto out;
  713. __debug_check_no_obj_freed(&obj, sizeof(obj));
  714. if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
  715. goto out;
  716. #endif
  717. printk(KERN_INFO "ODEBUG: selftest passed\n");
  718. out:
  719. debug_objects_fixups = oldfixups;
  720. debug_objects_warnings = oldwarnings;
  721. descr_test = NULL;
  722. local_irq_restore(flags);
  723. }
  724. #else
  725. static inline void debug_objects_selftest(void) { }
  726. #endif
  727. /*
  728. * Called during early boot to initialize the hash buckets and link
  729. * the static object pool objects into the poll list. After this call
  730. * the object tracker is fully operational.
  731. */
  732. void __init debug_objects_early_init(void)
  733. {
  734. int i;
  735. for (i = 0; i < ODEBUG_HASH_SIZE; i++)
  736. spin_lock_init(&obj_hash[i].lock);
  737. for (i = 0; i < ODEBUG_POOL_SIZE; i++)
  738. hlist_add_head(&obj_static_pool[i].node, &obj_pool);
  739. }
  740. /*
  741. * Called after the kmem_caches are functional to setup a dedicated
  742. * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
  743. * prevents that the debug code is called on kmem_cache_free() for the
  744. * debug tracker objects to avoid recursive calls.
  745. */
  746. void __init debug_objects_mem_init(void)
  747. {
  748. if (!debug_objects_enabled)
  749. return;
  750. obj_cache = kmem_cache_create("debug_objects_cache",
  751. sizeof (struct debug_obj), 0,
  752. SLAB_DEBUG_OBJECTS, NULL);
  753. if (!obj_cache)
  754. debug_objects_enabled = 0;
  755. else
  756. debug_objects_selftest();
  757. }