debugobjects.c 26 KB

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