debugobjects.c 21 KB

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