debugobjects.c 27 KB

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