debugobjects.c 27 KB

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