debugobjects.c 28 KB

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