debugobjects.c 29 KB

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