debugobjects.c 28 KB

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