debugobjects.c 30 KB

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