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 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. kmemleak_ignore(new);
  114. raw_spin_lock_irqsave(&pool_lock, flags);
  115. hlist_add_head(&new->node, &obj_pool);
  116. debug_objects_allocated++;
  117. obj_pool_free++;
  118. raw_spin_unlock_irqrestore(&pool_lock, flags);
  119. }
  120. }
  121. /*
  122. * Lookup an object in the hash bucket.
  123. */
  124. static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
  125. {
  126. struct debug_obj *obj;
  127. int cnt = 0;
  128. hlist_for_each_entry(obj, &b->list, node) {
  129. cnt++;
  130. if (obj->object == addr)
  131. return obj;
  132. }
  133. if (cnt > debug_objects_maxchain)
  134. debug_objects_maxchain = cnt;
  135. return NULL;
  136. }
  137. /*
  138. * Allocate a new object. If the pool is empty, switch off the debugger.
  139. * Must be called with interrupts disabled.
  140. */
  141. static struct debug_obj *
  142. alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
  143. {
  144. struct debug_obj *obj = NULL;
  145. raw_spin_lock(&pool_lock);
  146. if (obj_pool.first) {
  147. obj = hlist_entry(obj_pool.first, typeof(*obj), node);
  148. obj->object = addr;
  149. obj->descr = descr;
  150. obj->state = ODEBUG_STATE_NONE;
  151. obj->astate = 0;
  152. hlist_del(&obj->node);
  153. hlist_add_head(&obj->node, &b->list);
  154. obj_pool_used++;
  155. if (obj_pool_used > obj_pool_max_used)
  156. obj_pool_max_used = obj_pool_used;
  157. obj_pool_free--;
  158. if (obj_pool_free < obj_pool_min_free)
  159. obj_pool_min_free = obj_pool_free;
  160. }
  161. raw_spin_unlock(&pool_lock);
  162. return obj;
  163. }
  164. /*
  165. * workqueue function to free objects.
  166. *
  167. * To reduce contention on the global pool_lock, the actual freeing of
  168. * debug objects will be delayed if the pool_lock is busy.
  169. */
  170. static void free_obj_work(struct work_struct *work)
  171. {
  172. struct hlist_node *tmp;
  173. struct debug_obj *obj;
  174. unsigned long flags;
  175. HLIST_HEAD(tofree);
  176. if (!raw_spin_trylock_irqsave(&pool_lock, flags))
  177. return;
  178. /*
  179. * The objs on the pool list might be allocated before the work is
  180. * run, so recheck if pool list it full or not, if not fill pool
  181. * list from the global free list
  182. */
  183. while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
  184. obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
  185. hlist_del(&obj->node);
  186. hlist_add_head(&obj->node, &obj_pool);
  187. obj_pool_free++;
  188. obj_nr_tofree--;
  189. }
  190. /*
  191. * Pool list is already full and there are still objs on the free
  192. * list. Move remaining free objs to a temporary list to free the
  193. * memory outside the pool_lock held region.
  194. */
  195. if (obj_nr_tofree) {
  196. hlist_move_list(&obj_to_free, &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 is on stack, but not annotated\n");
  306. else
  307. pr_warn("object is not on stack, but annotated\n");
  308. WARN_ON(1);
  309. }
  310. static void
  311. __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
  312. {
  313. enum debug_obj_state state;
  314. struct debug_bucket *db;
  315. struct debug_obj *obj;
  316. unsigned long flags;
  317. fill_pool();
  318. db = get_bucket((unsigned long) addr);
  319. raw_spin_lock_irqsave(&db->lock, flags);
  320. obj = lookup_object(addr, db);
  321. if (!obj) {
  322. obj = alloc_object(addr, db, descr);
  323. if (!obj) {
  324. debug_objects_enabled = 0;
  325. raw_spin_unlock_irqrestore(&db->lock, flags);
  326. debug_objects_oom();
  327. return;
  328. }
  329. debug_object_is_on_stack(addr, onstack);
  330. }
  331. switch (obj->state) {
  332. case ODEBUG_STATE_NONE:
  333. case ODEBUG_STATE_INIT:
  334. case ODEBUG_STATE_INACTIVE:
  335. obj->state = ODEBUG_STATE_INIT;
  336. break;
  337. case ODEBUG_STATE_ACTIVE:
  338. debug_print_object(obj, "init");
  339. state = obj->state;
  340. raw_spin_unlock_irqrestore(&db->lock, flags);
  341. debug_object_fixup(descr->fixup_init, addr, state);
  342. return;
  343. case ODEBUG_STATE_DESTROYED:
  344. debug_print_object(obj, "init");
  345. break;
  346. default:
  347. break;
  348. }
  349. raw_spin_unlock_irqrestore(&db->lock, flags);
  350. }
  351. /**
  352. * debug_object_init - debug checks when an object is initialized
  353. * @addr: address of the object
  354. * @descr: pointer to an object specific debug description structure
  355. */
  356. void debug_object_init(void *addr, struct debug_obj_descr *descr)
  357. {
  358. if (!debug_objects_enabled)
  359. return;
  360. __debug_object_init(addr, descr, 0);
  361. }
  362. EXPORT_SYMBOL_GPL(debug_object_init);
  363. /**
  364. * debug_object_init_on_stack - debug checks when an object on stack is
  365. * initialized
  366. * @addr: address of the object
  367. * @descr: pointer to an object specific debug description structure
  368. */
  369. void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
  370. {
  371. if (!debug_objects_enabled)
  372. return;
  373. __debug_object_init(addr, descr, 1);
  374. }
  375. EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
  376. /**
  377. * debug_object_activate - debug checks when an object is activated
  378. * @addr: address of the object
  379. * @descr: pointer to an object specific debug description structure
  380. * Returns 0 for success, -EINVAL for check failed.
  381. */
  382. int debug_object_activate(void *addr, struct debug_obj_descr *descr)
  383. {
  384. enum debug_obj_state state;
  385. struct debug_bucket *db;
  386. struct debug_obj *obj;
  387. unsigned long flags;
  388. int ret;
  389. struct debug_obj o = { .object = addr,
  390. .state = ODEBUG_STATE_NOTAVAILABLE,
  391. .descr = descr };
  392. if (!debug_objects_enabled)
  393. return 0;
  394. db = get_bucket((unsigned long) addr);
  395. raw_spin_lock_irqsave(&db->lock, flags);
  396. obj = lookup_object(addr, db);
  397. if (obj) {
  398. switch (obj->state) {
  399. case ODEBUG_STATE_INIT:
  400. case ODEBUG_STATE_INACTIVE:
  401. obj->state = ODEBUG_STATE_ACTIVE;
  402. ret = 0;
  403. break;
  404. case ODEBUG_STATE_ACTIVE:
  405. debug_print_object(obj, "activate");
  406. state = obj->state;
  407. raw_spin_unlock_irqrestore(&db->lock, flags);
  408. ret = debug_object_fixup(descr->fixup_activate, addr, state);
  409. return ret ? 0 : -EINVAL;
  410. case ODEBUG_STATE_DESTROYED:
  411. debug_print_object(obj, "activate");
  412. ret = -EINVAL;
  413. break;
  414. default:
  415. ret = 0;
  416. break;
  417. }
  418. raw_spin_unlock_irqrestore(&db->lock, flags);
  419. return ret;
  420. }
  421. raw_spin_unlock_irqrestore(&db->lock, flags);
  422. /*
  423. * We are here when a static object is activated. We
  424. * let the type specific code confirm whether this is
  425. * true or not. if true, we just make sure that the
  426. * static object is tracked in the object tracker. If
  427. * not, this must be a bug, so we try to fix it up.
  428. */
  429. if (descr->is_static_object && descr->is_static_object(addr)) {
  430. /* track this static object */
  431. debug_object_init(addr, descr);
  432. debug_object_activate(addr, descr);
  433. } else {
  434. debug_print_object(&o, "activate");
  435. ret = debug_object_fixup(descr->fixup_activate, addr,
  436. ODEBUG_STATE_NOTAVAILABLE);
  437. return ret ? 0 : -EINVAL;
  438. }
  439. return 0;
  440. }
  441. EXPORT_SYMBOL_GPL(debug_object_activate);
  442. /**
  443. * debug_object_deactivate - debug checks when an object is deactivated
  444. * @addr: address of the object
  445. * @descr: pointer to an object specific debug description structure
  446. */
  447. void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
  448. {
  449. struct debug_bucket *db;
  450. struct debug_obj *obj;
  451. unsigned long flags;
  452. if (!debug_objects_enabled)
  453. return;
  454. db = get_bucket((unsigned long) addr);
  455. raw_spin_lock_irqsave(&db->lock, flags);
  456. obj = lookup_object(addr, db);
  457. if (obj) {
  458. switch (obj->state) {
  459. case ODEBUG_STATE_INIT:
  460. case ODEBUG_STATE_INACTIVE:
  461. case ODEBUG_STATE_ACTIVE:
  462. if (!obj->astate)
  463. obj->state = ODEBUG_STATE_INACTIVE;
  464. else
  465. debug_print_object(obj, "deactivate");
  466. break;
  467. case ODEBUG_STATE_DESTROYED:
  468. debug_print_object(obj, "deactivate");
  469. break;
  470. default:
  471. break;
  472. }
  473. } else {
  474. struct debug_obj o = { .object = addr,
  475. .state = ODEBUG_STATE_NOTAVAILABLE,
  476. .descr = descr };
  477. debug_print_object(&o, "deactivate");
  478. }
  479. raw_spin_unlock_irqrestore(&db->lock, flags);
  480. }
  481. EXPORT_SYMBOL_GPL(debug_object_deactivate);
  482. /**
  483. * debug_object_destroy - debug checks when an object is destroyed
  484. * @addr: address of the object
  485. * @descr: pointer to an object specific debug description structure
  486. */
  487. void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
  488. {
  489. enum debug_obj_state state;
  490. struct debug_bucket *db;
  491. struct debug_obj *obj;
  492. unsigned long flags;
  493. if (!debug_objects_enabled)
  494. return;
  495. db = get_bucket((unsigned long) addr);
  496. raw_spin_lock_irqsave(&db->lock, flags);
  497. obj = lookup_object(addr, db);
  498. if (!obj)
  499. goto out_unlock;
  500. switch (obj->state) {
  501. case ODEBUG_STATE_NONE:
  502. case ODEBUG_STATE_INIT:
  503. case ODEBUG_STATE_INACTIVE:
  504. obj->state = ODEBUG_STATE_DESTROYED;
  505. break;
  506. case ODEBUG_STATE_ACTIVE:
  507. debug_print_object(obj, "destroy");
  508. state = obj->state;
  509. raw_spin_unlock_irqrestore(&db->lock, flags);
  510. debug_object_fixup(descr->fixup_destroy, addr, state);
  511. return;
  512. case ODEBUG_STATE_DESTROYED:
  513. debug_print_object(obj, "destroy");
  514. break;
  515. default:
  516. break;
  517. }
  518. out_unlock:
  519. raw_spin_unlock_irqrestore(&db->lock, flags);
  520. }
  521. EXPORT_SYMBOL_GPL(debug_object_destroy);
  522. /**
  523. * debug_object_free - debug checks when an object is freed
  524. * @addr: address of the object
  525. * @descr: pointer to an object specific debug description structure
  526. */
  527. void debug_object_free(void *addr, struct debug_obj_descr *descr)
  528. {
  529. enum debug_obj_state state;
  530. struct debug_bucket *db;
  531. struct debug_obj *obj;
  532. unsigned long flags;
  533. if (!debug_objects_enabled)
  534. return;
  535. db = get_bucket((unsigned long) addr);
  536. raw_spin_lock_irqsave(&db->lock, flags);
  537. obj = lookup_object(addr, db);
  538. if (!obj)
  539. goto out_unlock;
  540. switch (obj->state) {
  541. case ODEBUG_STATE_ACTIVE:
  542. debug_print_object(obj, "free");
  543. state = obj->state;
  544. raw_spin_unlock_irqrestore(&db->lock, flags);
  545. debug_object_fixup(descr->fixup_free, addr, state);
  546. return;
  547. default:
  548. hlist_del(&obj->node);
  549. raw_spin_unlock_irqrestore(&db->lock, flags);
  550. free_object(obj);
  551. return;
  552. }
  553. out_unlock:
  554. raw_spin_unlock_irqrestore(&db->lock, flags);
  555. }
  556. EXPORT_SYMBOL_GPL(debug_object_free);
  557. /**
  558. * debug_object_assert_init - debug checks when object should be init-ed
  559. * @addr: address of the object
  560. * @descr: pointer to an object specific debug description structure
  561. */
  562. void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
  563. {
  564. struct debug_bucket *db;
  565. struct debug_obj *obj;
  566. unsigned long flags;
  567. if (!debug_objects_enabled)
  568. return;
  569. db = get_bucket((unsigned long) addr);
  570. raw_spin_lock_irqsave(&db->lock, flags);
  571. obj = lookup_object(addr, db);
  572. if (!obj) {
  573. struct debug_obj o = { .object = addr,
  574. .state = ODEBUG_STATE_NOTAVAILABLE,
  575. .descr = descr };
  576. raw_spin_unlock_irqrestore(&db->lock, flags);
  577. /*
  578. * Maybe the object is static, and we let the type specific
  579. * code confirm. Track this static object if true, else invoke
  580. * fixup.
  581. */
  582. if (descr->is_static_object && descr->is_static_object(addr)) {
  583. /* Track this static object */
  584. debug_object_init(addr, descr);
  585. } else {
  586. debug_print_object(&o, "assert_init");
  587. debug_object_fixup(descr->fixup_assert_init, addr,
  588. ODEBUG_STATE_NOTAVAILABLE);
  589. }
  590. return;
  591. }
  592. raw_spin_unlock_irqrestore(&db->lock, flags);
  593. }
  594. EXPORT_SYMBOL_GPL(debug_object_assert_init);
  595. /**
  596. * debug_object_active_state - debug checks object usage state machine
  597. * @addr: address of the object
  598. * @descr: pointer to an object specific debug description structure
  599. * @expect: expected state
  600. * @next: state to move to if expected state is found
  601. */
  602. void
  603. debug_object_active_state(void *addr, struct debug_obj_descr *descr,
  604. unsigned int expect, unsigned int next)
  605. {
  606. struct debug_bucket *db;
  607. struct debug_obj *obj;
  608. unsigned long flags;
  609. if (!debug_objects_enabled)
  610. return;
  611. db = get_bucket((unsigned long) addr);
  612. raw_spin_lock_irqsave(&db->lock, flags);
  613. obj = lookup_object(addr, db);
  614. if (obj) {
  615. switch (obj->state) {
  616. case ODEBUG_STATE_ACTIVE:
  617. if (obj->astate == expect)
  618. obj->astate = next;
  619. else
  620. debug_print_object(obj, "active_state");
  621. break;
  622. default:
  623. debug_print_object(obj, "active_state");
  624. break;
  625. }
  626. } else {
  627. struct debug_obj o = { .object = addr,
  628. .state = ODEBUG_STATE_NOTAVAILABLE,
  629. .descr = descr };
  630. debug_print_object(&o, "active_state");
  631. }
  632. raw_spin_unlock_irqrestore(&db->lock, flags);
  633. }
  634. EXPORT_SYMBOL_GPL(debug_object_active_state);
  635. #ifdef CONFIG_DEBUG_OBJECTS_FREE
  636. static void __debug_check_no_obj_freed(const void *address, unsigned long size)
  637. {
  638. unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
  639. struct hlist_node *tmp;
  640. HLIST_HEAD(freelist);
  641. struct debug_obj_descr *descr;
  642. enum debug_obj_state state;
  643. struct debug_bucket *db;
  644. struct debug_obj *obj;
  645. int cnt, objs_checked = 0;
  646. saddr = (unsigned long) address;
  647. eaddr = saddr + size;
  648. paddr = saddr & ODEBUG_CHUNK_MASK;
  649. chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
  650. chunks >>= ODEBUG_CHUNK_SHIFT;
  651. for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
  652. db = get_bucket(paddr);
  653. repeat:
  654. cnt = 0;
  655. raw_spin_lock_irqsave(&db->lock, flags);
  656. hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
  657. cnt++;
  658. oaddr = (unsigned long) obj->object;
  659. if (oaddr < saddr || oaddr >= eaddr)
  660. continue;
  661. switch (obj->state) {
  662. case ODEBUG_STATE_ACTIVE:
  663. debug_print_object(obj, "free");
  664. descr = obj->descr;
  665. state = obj->state;
  666. raw_spin_unlock_irqrestore(&db->lock, flags);
  667. debug_object_fixup(descr->fixup_free,
  668. (void *) oaddr, state);
  669. goto repeat;
  670. default:
  671. hlist_del(&obj->node);
  672. hlist_add_head(&obj->node, &freelist);
  673. break;
  674. }
  675. }
  676. raw_spin_unlock_irqrestore(&db->lock, flags);
  677. /* Now free them */
  678. hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
  679. hlist_del(&obj->node);
  680. free_object(obj);
  681. }
  682. if (cnt > debug_objects_maxchain)
  683. debug_objects_maxchain = cnt;
  684. objs_checked += cnt;
  685. }
  686. if (objs_checked > debug_objects_maxchecked)
  687. debug_objects_maxchecked = objs_checked;
  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. kmemleak_ignore(obj);
  961. hlist_add_head(&obj->node, &objects);
  962. }
  963. /*
  964. * When debug_objects_mem_init() is called we know that only
  965. * one CPU is up, so disabling interrupts is enough
  966. * protection. This avoids the lockdep hell of lock ordering.
  967. */
  968. local_irq_disable();
  969. /* Remove the statically allocated objects from the pool */
  970. hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
  971. hlist_del(&obj->node);
  972. /* Move the allocated objects to the pool */
  973. hlist_move_list(&objects, &obj_pool);
  974. /* Replace the active object references */
  975. for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
  976. hlist_move_list(&db->list, &objects);
  977. hlist_for_each_entry(obj, &objects, node) {
  978. new = hlist_entry(obj_pool.first, typeof(*obj), node);
  979. hlist_del(&new->node);
  980. /* copy object data */
  981. *new = *obj;
  982. hlist_add_head(&new->node, &db->list);
  983. cnt++;
  984. }
  985. }
  986. local_irq_enable();
  987. pr_debug("%d of %d active objects replaced\n",
  988. cnt, obj_pool_used);
  989. return 0;
  990. free:
  991. hlist_for_each_entry_safe(obj, tmp, &objects, node) {
  992. hlist_del(&obj->node);
  993. kmem_cache_free(obj_cache, obj);
  994. }
  995. return -ENOMEM;
  996. }
  997. /*
  998. * Called after the kmem_caches are functional to setup a dedicated
  999. * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
  1000. * prevents that the debug code is called on kmem_cache_free() for the
  1001. * debug tracker objects to avoid recursive calls.
  1002. */
  1003. void __init debug_objects_mem_init(void)
  1004. {
  1005. if (!debug_objects_enabled)
  1006. return;
  1007. obj_cache = kmem_cache_create("debug_objects_cache",
  1008. sizeof (struct debug_obj), 0,
  1009. SLAB_DEBUG_OBJECTS, NULL);
  1010. if (!obj_cache || debug_objects_replace_static_objects()) {
  1011. debug_objects_enabled = 0;
  1012. if (obj_cache)
  1013. kmem_cache_destroy(obj_cache);
  1014. pr_warn("out of memory.\n");
  1015. } else
  1016. debug_objects_selftest();
  1017. /*
  1018. * Increase the thresholds for allocating and freeing objects
  1019. * according to the number of possible CPUs available in the system.
  1020. */
  1021. debug_objects_pool_size += num_possible_cpus() * 32;
  1022. debug_objects_pool_min_level += num_possible_cpus() * 4;
  1023. }