debugobjects.c 23 KB

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