debugobjects.c 29 KB

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