debugobjects.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic infrastructure for lifetime debugging of objects.
  4. *
  5. * Copyright (C) 2008, Thomas Gleixner <[email protected]>
  6. */
  7. #define pr_fmt(fmt) "ODEBUG: " fmt
  8. #include <linux/debugobjects.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/sched.h>
  11. #include <linux/sched/task_stack.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/slab.h>
  15. #include <linux/hash.h>
  16. #include <linux/kmemleak.h>
  17. #include <linux/cpu.h>
  18. #define ODEBUG_HASH_BITS 14
  19. #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
  20. #define ODEBUG_POOL_SIZE 1024
  21. #define ODEBUG_POOL_MIN_LEVEL 256
  22. #define ODEBUG_POOL_PERCPU_SIZE 64
  23. #define ODEBUG_BATCH_SIZE 16
  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. /*
  28. * We limit the freeing of debug objects via workqueue at a maximum
  29. * frequency of 10Hz and about 1024 objects for each freeing operation.
  30. * So it is freeing at most 10k debug objects per second.
  31. */
  32. #define ODEBUG_FREE_WORK_MAX 1024
  33. #define ODEBUG_FREE_WORK_DELAY DIV_ROUND_UP(HZ, 10)
  34. struct debug_bucket {
  35. struct hlist_head list;
  36. raw_spinlock_t lock;
  37. };
  38. /*
  39. * Debug object percpu free list
  40. * Access is protected by disabling irq
  41. */
  42. struct debug_percpu_free {
  43. struct hlist_head free_objs;
  44. int obj_free;
  45. };
  46. static DEFINE_PER_CPU(struct debug_percpu_free, percpu_obj_pool);
  47. static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
  48. static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
  49. static DEFINE_RAW_SPINLOCK(pool_lock);
  50. static HLIST_HEAD(obj_pool);
  51. static HLIST_HEAD(obj_to_free);
  52. /*
  53. * Because of the presence of percpu free pools, obj_pool_free will
  54. * under-count those in the percpu free pools. Similarly, obj_pool_used
  55. * will over-count those in the percpu free pools. Adjustments will be
  56. * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
  57. * can be off.
  58. */
  59. static int obj_pool_min_free = ODEBUG_POOL_SIZE;
  60. static int obj_pool_free = ODEBUG_POOL_SIZE;
  61. static int obj_pool_used;
  62. static int obj_pool_max_used;
  63. static bool obj_freeing;
  64. /* The number of objs on the global free list */
  65. static int obj_nr_tofree;
  66. static int debug_objects_maxchain __read_mostly;
  67. static int __maybe_unused debug_objects_maxchecked __read_mostly;
  68. static int debug_objects_fixups __read_mostly;
  69. static int debug_objects_warnings __read_mostly;
  70. static int debug_objects_enabled __read_mostly
  71. = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
  72. static int debug_objects_pool_size __read_mostly
  73. = ODEBUG_POOL_SIZE;
  74. static int debug_objects_pool_min_level __read_mostly
  75. = ODEBUG_POOL_MIN_LEVEL;
  76. static const struct debug_obj_descr *descr_test __read_mostly;
  77. static struct kmem_cache *obj_cache __read_mostly;
  78. /*
  79. * Track numbers of kmem_cache_alloc()/free() calls done.
  80. */
  81. static int debug_objects_allocated;
  82. static int debug_objects_freed;
  83. static void free_obj_work(struct work_struct *work);
  84. static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work);
  85. static int __init enable_object_debug(char *str)
  86. {
  87. debug_objects_enabled = 1;
  88. return 0;
  89. }
  90. static int __init disable_object_debug(char *str)
  91. {
  92. debug_objects_enabled = 0;
  93. return 0;
  94. }
  95. early_param("debug_objects", enable_object_debug);
  96. early_param("no_debug_objects", disable_object_debug);
  97. static const char *obj_states[ODEBUG_STATE_MAX] = {
  98. [ODEBUG_STATE_NONE] = "none",
  99. [ODEBUG_STATE_INIT] = "initialized",
  100. [ODEBUG_STATE_INACTIVE] = "inactive",
  101. [ODEBUG_STATE_ACTIVE] = "active",
  102. [ODEBUG_STATE_DESTROYED] = "destroyed",
  103. [ODEBUG_STATE_NOTAVAILABLE] = "not available",
  104. };
  105. static void fill_pool(void)
  106. {
  107. gfp_t gfp = __GFP_HIGH | __GFP_NOWARN;
  108. struct debug_obj *obj;
  109. unsigned long flags;
  110. if (likely(READ_ONCE(obj_pool_free) >= debug_objects_pool_min_level))
  111. return;
  112. /*
  113. * Reuse objs from the global free list; they will be reinitialized
  114. * when allocating.
  115. *
  116. * Both obj_nr_tofree and obj_pool_free are checked locklessly; the
  117. * READ_ONCE()s pair with the WRITE_ONCE()s in pool_lock critical
  118. * sections.
  119. */
  120. while (READ_ONCE(obj_nr_tofree) && (READ_ONCE(obj_pool_free) < obj_pool_min_free)) {
  121. raw_spin_lock_irqsave(&pool_lock, flags);
  122. /*
  123. * Recheck with the lock held as the worker thread might have
  124. * won the race and freed the global free list already.
  125. */
  126. while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
  127. obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
  128. hlist_del(&obj->node);
  129. WRITE_ONCE(obj_nr_tofree, obj_nr_tofree - 1);
  130. hlist_add_head(&obj->node, &obj_pool);
  131. WRITE_ONCE(obj_pool_free, obj_pool_free + 1);
  132. }
  133. raw_spin_unlock_irqrestore(&pool_lock, flags);
  134. }
  135. if (unlikely(!obj_cache))
  136. return;
  137. while (READ_ONCE(obj_pool_free) < debug_objects_pool_min_level) {
  138. struct debug_obj *new[ODEBUG_BATCH_SIZE];
  139. int cnt;
  140. for (cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
  141. new[cnt] = kmem_cache_zalloc(obj_cache, gfp);
  142. if (!new[cnt])
  143. break;
  144. }
  145. if (!cnt)
  146. return;
  147. raw_spin_lock_irqsave(&pool_lock, flags);
  148. while (cnt) {
  149. hlist_add_head(&new[--cnt]->node, &obj_pool);
  150. debug_objects_allocated++;
  151. WRITE_ONCE(obj_pool_free, obj_pool_free + 1);
  152. }
  153. raw_spin_unlock_irqrestore(&pool_lock, flags);
  154. }
  155. }
  156. /*
  157. * Lookup an object in the hash bucket.
  158. */
  159. static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
  160. {
  161. struct debug_obj *obj;
  162. int cnt = 0;
  163. hlist_for_each_entry(obj, &b->list, node) {
  164. cnt++;
  165. if (obj->object == addr)
  166. return obj;
  167. }
  168. if (cnt > debug_objects_maxchain)
  169. debug_objects_maxchain = cnt;
  170. return NULL;
  171. }
  172. /*
  173. * Allocate a new object from the hlist
  174. */
  175. static struct debug_obj *__alloc_object(struct hlist_head *list)
  176. {
  177. struct debug_obj *obj = NULL;
  178. if (list->first) {
  179. obj = hlist_entry(list->first, typeof(*obj), node);
  180. hlist_del(&obj->node);
  181. }
  182. return obj;
  183. }
  184. static struct debug_obj *
  185. alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
  186. {
  187. struct debug_percpu_free *percpu_pool = this_cpu_ptr(&percpu_obj_pool);
  188. struct debug_obj *obj;
  189. if (likely(obj_cache)) {
  190. obj = __alloc_object(&percpu_pool->free_objs);
  191. if (obj) {
  192. percpu_pool->obj_free--;
  193. goto init_obj;
  194. }
  195. }
  196. raw_spin_lock(&pool_lock);
  197. obj = __alloc_object(&obj_pool);
  198. if (obj) {
  199. obj_pool_used++;
  200. WRITE_ONCE(obj_pool_free, obj_pool_free - 1);
  201. /*
  202. * Looking ahead, allocate one batch of debug objects and
  203. * put them into the percpu free pool.
  204. */
  205. if (likely(obj_cache)) {
  206. int i;
  207. for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
  208. struct debug_obj *obj2;
  209. obj2 = __alloc_object(&obj_pool);
  210. if (!obj2)
  211. break;
  212. hlist_add_head(&obj2->node,
  213. &percpu_pool->free_objs);
  214. percpu_pool->obj_free++;
  215. obj_pool_used++;
  216. WRITE_ONCE(obj_pool_free, obj_pool_free - 1);
  217. }
  218. }
  219. if (obj_pool_used > obj_pool_max_used)
  220. obj_pool_max_used = obj_pool_used;
  221. if (obj_pool_free < obj_pool_min_free)
  222. obj_pool_min_free = obj_pool_free;
  223. }
  224. raw_spin_unlock(&pool_lock);
  225. init_obj:
  226. if (obj) {
  227. obj->object = addr;
  228. obj->descr = descr;
  229. obj->state = ODEBUG_STATE_NONE;
  230. obj->astate = 0;
  231. hlist_add_head(&obj->node, &b->list);
  232. }
  233. return obj;
  234. }
  235. /*
  236. * workqueue function to free objects.
  237. *
  238. * To reduce contention on the global pool_lock, the actual freeing of
  239. * debug objects will be delayed if the pool_lock is busy.
  240. */
  241. static void free_obj_work(struct work_struct *work)
  242. {
  243. struct hlist_node *tmp;
  244. struct debug_obj *obj;
  245. unsigned long flags;
  246. HLIST_HEAD(tofree);
  247. WRITE_ONCE(obj_freeing, false);
  248. if (!raw_spin_trylock_irqsave(&pool_lock, flags))
  249. return;
  250. if (obj_pool_free >= debug_objects_pool_size)
  251. goto free_objs;
  252. /*
  253. * The objs on the pool list might be allocated before the work is
  254. * run, so recheck if pool list it full or not, if not fill pool
  255. * list from the global free list. As it is likely that a workload
  256. * may be gearing up to use more and more objects, don't free any
  257. * of them until the next round.
  258. */
  259. while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
  260. obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
  261. hlist_del(&obj->node);
  262. hlist_add_head(&obj->node, &obj_pool);
  263. WRITE_ONCE(obj_pool_free, obj_pool_free + 1);
  264. WRITE_ONCE(obj_nr_tofree, obj_nr_tofree - 1);
  265. }
  266. raw_spin_unlock_irqrestore(&pool_lock, flags);
  267. return;
  268. free_objs:
  269. /*
  270. * Pool list is already full and there are still objs on the free
  271. * list. Move remaining free objs to a temporary list to free the
  272. * memory outside the pool_lock held region.
  273. */
  274. if (obj_nr_tofree) {
  275. hlist_move_list(&obj_to_free, &tofree);
  276. debug_objects_freed += obj_nr_tofree;
  277. WRITE_ONCE(obj_nr_tofree, 0);
  278. }
  279. raw_spin_unlock_irqrestore(&pool_lock, flags);
  280. hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
  281. hlist_del(&obj->node);
  282. kmem_cache_free(obj_cache, obj);
  283. }
  284. }
  285. static void __free_object(struct debug_obj *obj)
  286. {
  287. struct debug_obj *objs[ODEBUG_BATCH_SIZE];
  288. struct debug_percpu_free *percpu_pool;
  289. int lookahead_count = 0;
  290. unsigned long flags;
  291. bool work;
  292. local_irq_save(flags);
  293. if (!obj_cache)
  294. goto free_to_obj_pool;
  295. /*
  296. * Try to free it into the percpu pool first.
  297. */
  298. percpu_pool = this_cpu_ptr(&percpu_obj_pool);
  299. if (percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
  300. hlist_add_head(&obj->node, &percpu_pool->free_objs);
  301. percpu_pool->obj_free++;
  302. local_irq_restore(flags);
  303. return;
  304. }
  305. /*
  306. * As the percpu pool is full, look ahead and pull out a batch
  307. * of objects from the percpu pool and free them as well.
  308. */
  309. for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) {
  310. objs[lookahead_count] = __alloc_object(&percpu_pool->free_objs);
  311. if (!objs[lookahead_count])
  312. break;
  313. percpu_pool->obj_free--;
  314. }
  315. free_to_obj_pool:
  316. raw_spin_lock(&pool_lock);
  317. work = (obj_pool_free > debug_objects_pool_size) && obj_cache &&
  318. (obj_nr_tofree < ODEBUG_FREE_WORK_MAX);
  319. obj_pool_used--;
  320. if (work) {
  321. WRITE_ONCE(obj_nr_tofree, obj_nr_tofree + 1);
  322. hlist_add_head(&obj->node, &obj_to_free);
  323. if (lookahead_count) {
  324. WRITE_ONCE(obj_nr_tofree, obj_nr_tofree + lookahead_count);
  325. obj_pool_used -= lookahead_count;
  326. while (lookahead_count) {
  327. hlist_add_head(&objs[--lookahead_count]->node,
  328. &obj_to_free);
  329. }
  330. }
  331. if ((obj_pool_free > debug_objects_pool_size) &&
  332. (obj_nr_tofree < ODEBUG_FREE_WORK_MAX)) {
  333. int i;
  334. /*
  335. * Free one more batch of objects from obj_pool.
  336. */
  337. for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
  338. obj = __alloc_object(&obj_pool);
  339. hlist_add_head(&obj->node, &obj_to_free);
  340. WRITE_ONCE(obj_pool_free, obj_pool_free - 1);
  341. WRITE_ONCE(obj_nr_tofree, obj_nr_tofree + 1);
  342. }
  343. }
  344. } else {
  345. WRITE_ONCE(obj_pool_free, obj_pool_free + 1);
  346. hlist_add_head(&obj->node, &obj_pool);
  347. if (lookahead_count) {
  348. WRITE_ONCE(obj_pool_free, obj_pool_free + lookahead_count);
  349. obj_pool_used -= lookahead_count;
  350. while (lookahead_count) {
  351. hlist_add_head(&objs[--lookahead_count]->node,
  352. &obj_pool);
  353. }
  354. }
  355. }
  356. raw_spin_unlock(&pool_lock);
  357. local_irq_restore(flags);
  358. }
  359. /*
  360. * Put the object back into the pool and schedule work to free objects
  361. * if necessary.
  362. */
  363. static void free_object(struct debug_obj *obj)
  364. {
  365. __free_object(obj);
  366. if (!READ_ONCE(obj_freeing) && READ_ONCE(obj_nr_tofree)) {
  367. WRITE_ONCE(obj_freeing, true);
  368. schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
  369. }
  370. }
  371. #ifdef CONFIG_HOTPLUG_CPU
  372. static int object_cpu_offline(unsigned int cpu)
  373. {
  374. struct debug_percpu_free *percpu_pool;
  375. struct hlist_node *tmp;
  376. struct debug_obj *obj;
  377. unsigned long flags;
  378. /* Remote access is safe as the CPU is dead already */
  379. percpu_pool = per_cpu_ptr(&percpu_obj_pool, cpu);
  380. hlist_for_each_entry_safe(obj, tmp, &percpu_pool->free_objs, node) {
  381. hlist_del(&obj->node);
  382. kmem_cache_free(obj_cache, obj);
  383. }
  384. raw_spin_lock_irqsave(&pool_lock, flags);
  385. obj_pool_used -= percpu_pool->obj_free;
  386. debug_objects_freed += percpu_pool->obj_free;
  387. raw_spin_unlock_irqrestore(&pool_lock, flags);
  388. percpu_pool->obj_free = 0;
  389. return 0;
  390. }
  391. #endif
  392. /*
  393. * We run out of memory. That means we probably have tons of objects
  394. * allocated.
  395. */
  396. static void debug_objects_oom(void)
  397. {
  398. struct debug_bucket *db = obj_hash;
  399. struct hlist_node *tmp;
  400. HLIST_HEAD(freelist);
  401. struct debug_obj *obj;
  402. unsigned long flags;
  403. int i;
  404. pr_warn("Out of memory. ODEBUG disabled\n");
  405. for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
  406. raw_spin_lock_irqsave(&db->lock, flags);
  407. hlist_move_list(&db->list, &freelist);
  408. raw_spin_unlock_irqrestore(&db->lock, flags);
  409. /* Now free them */
  410. hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
  411. hlist_del(&obj->node);
  412. free_object(obj);
  413. }
  414. }
  415. }
  416. /*
  417. * We use the pfn of the address for the hash. That way we can check
  418. * for freed objects simply by checking the affected bucket.
  419. */
  420. static struct debug_bucket *get_bucket(unsigned long addr)
  421. {
  422. unsigned long hash;
  423. hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
  424. return &obj_hash[hash];
  425. }
  426. static void debug_print_object(struct debug_obj *obj, char *msg)
  427. {
  428. const struct debug_obj_descr *descr = obj->descr;
  429. static int limit;
  430. /*
  431. * Don't report if lookup_object_or_alloc() by the current thread
  432. * failed because lookup_object_or_alloc()/debug_objects_oom() by a
  433. * concurrent thread turned off debug_objects_enabled and cleared
  434. * the hash buckets.
  435. */
  436. if (!debug_objects_enabled)
  437. return;
  438. if (limit < 5 && descr != descr_test) {
  439. void *hint = descr->debug_hint ?
  440. descr->debug_hint(obj->object) : NULL;
  441. limit++;
  442. WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
  443. "object type: %s hint: %pS\n",
  444. msg, obj_states[obj->state], obj->astate,
  445. descr->name, hint);
  446. }
  447. debug_objects_warnings++;
  448. }
  449. /*
  450. * Try to repair the damage, so we have a better chance to get useful
  451. * debug output.
  452. */
  453. static bool
  454. debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
  455. void * addr, enum debug_obj_state state)
  456. {
  457. if (fixup && fixup(addr, state)) {
  458. debug_objects_fixups++;
  459. return true;
  460. }
  461. return false;
  462. }
  463. static void debug_object_is_on_stack(void *addr, int onstack)
  464. {
  465. int is_on_stack;
  466. static int limit;
  467. if (limit > 4)
  468. return;
  469. is_on_stack = object_is_on_stack(addr);
  470. if (is_on_stack == onstack)
  471. return;
  472. limit++;
  473. if (is_on_stack)
  474. pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
  475. task_stack_page(current));
  476. else
  477. pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
  478. task_stack_page(current));
  479. WARN_ON(1);
  480. }
  481. static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
  482. const struct debug_obj_descr *descr,
  483. bool onstack, bool alloc_ifstatic)
  484. {
  485. struct debug_obj *obj = lookup_object(addr, b);
  486. enum debug_obj_state state = ODEBUG_STATE_NONE;
  487. if (likely(obj))
  488. return obj;
  489. /*
  490. * debug_object_init() unconditionally allocates untracked
  491. * objects. It does not matter whether it is a static object or
  492. * not.
  493. *
  494. * debug_object_assert_init() and debug_object_activate() allow
  495. * allocation only if the descriptor callback confirms that the
  496. * object is static and considered initialized. For non-static
  497. * objects the allocation needs to be done from the fixup callback.
  498. */
  499. if (unlikely(alloc_ifstatic)) {
  500. if (!descr->is_static_object || !descr->is_static_object(addr))
  501. return ERR_PTR(-ENOENT);
  502. /* Statically allocated objects are considered initialized */
  503. state = ODEBUG_STATE_INIT;
  504. }
  505. obj = alloc_object(addr, b, descr);
  506. if (likely(obj)) {
  507. obj->state = state;
  508. debug_object_is_on_stack(addr, onstack);
  509. return obj;
  510. }
  511. /* Out of memory. Do the cleanup outside of the locked region */
  512. debug_objects_enabled = 0;
  513. return NULL;
  514. }
  515. static void debug_objects_fill_pool(void)
  516. {
  517. /*
  518. * On RT enabled kernels the pool refill must happen in preemptible
  519. * context:
  520. */
  521. if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible())
  522. fill_pool();
  523. }
  524. static void
  525. __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
  526. {
  527. enum debug_obj_state state;
  528. struct debug_bucket *db;
  529. struct debug_obj *obj;
  530. unsigned long flags;
  531. debug_objects_fill_pool();
  532. db = get_bucket((unsigned long) addr);
  533. raw_spin_lock_irqsave(&db->lock, flags);
  534. obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
  535. if (unlikely(!obj)) {
  536. raw_spin_unlock_irqrestore(&db->lock, flags);
  537. debug_objects_oom();
  538. return;
  539. }
  540. switch (obj->state) {
  541. case ODEBUG_STATE_NONE:
  542. case ODEBUG_STATE_INIT:
  543. case ODEBUG_STATE_INACTIVE:
  544. obj->state = ODEBUG_STATE_INIT;
  545. break;
  546. case ODEBUG_STATE_ACTIVE:
  547. state = obj->state;
  548. raw_spin_unlock_irqrestore(&db->lock, flags);
  549. debug_print_object(obj, "init");
  550. debug_object_fixup(descr->fixup_init, addr, state);
  551. return;
  552. case ODEBUG_STATE_DESTROYED:
  553. raw_spin_unlock_irqrestore(&db->lock, flags);
  554. debug_print_object(obj, "init");
  555. return;
  556. default:
  557. break;
  558. }
  559. raw_spin_unlock_irqrestore(&db->lock, flags);
  560. }
  561. /**
  562. * debug_object_init - debug checks when an object is initialized
  563. * @addr: address of the object
  564. * @descr: pointer to an object specific debug description structure
  565. */
  566. void debug_object_init(void *addr, const struct debug_obj_descr *descr)
  567. {
  568. if (!debug_objects_enabled)
  569. return;
  570. __debug_object_init(addr, descr, 0);
  571. }
  572. EXPORT_SYMBOL_GPL(debug_object_init);
  573. /**
  574. * debug_object_init_on_stack - debug checks when an object on stack is
  575. * initialized
  576. * @addr: address of the object
  577. * @descr: pointer to an object specific debug description structure
  578. */
  579. void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
  580. {
  581. if (!debug_objects_enabled)
  582. return;
  583. __debug_object_init(addr, descr, 1);
  584. }
  585. EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
  586. /**
  587. * debug_object_activate - debug checks when an object is activated
  588. * @addr: address of the object
  589. * @descr: pointer to an object specific debug description structure
  590. * Returns 0 for success, -EINVAL for check failed.
  591. */
  592. int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
  593. {
  594. struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
  595. enum debug_obj_state state;
  596. struct debug_bucket *db;
  597. struct debug_obj *obj;
  598. unsigned long flags;
  599. int ret;
  600. if (!debug_objects_enabled)
  601. return 0;
  602. debug_objects_fill_pool();
  603. db = get_bucket((unsigned long) addr);
  604. raw_spin_lock_irqsave(&db->lock, flags);
  605. obj = lookup_object_or_alloc(addr, db, descr, false, true);
  606. if (likely(!IS_ERR_OR_NULL(obj))) {
  607. bool print_object = false;
  608. switch (obj->state) {
  609. case ODEBUG_STATE_INIT:
  610. case ODEBUG_STATE_INACTIVE:
  611. obj->state = ODEBUG_STATE_ACTIVE;
  612. ret = 0;
  613. break;
  614. case ODEBUG_STATE_ACTIVE:
  615. state = obj->state;
  616. raw_spin_unlock_irqrestore(&db->lock, flags);
  617. debug_print_object(obj, "activate");
  618. ret = debug_object_fixup(descr->fixup_activate, addr, state);
  619. return ret ? 0 : -EINVAL;
  620. case ODEBUG_STATE_DESTROYED:
  621. print_object = true;
  622. ret = -EINVAL;
  623. break;
  624. default:
  625. ret = 0;
  626. break;
  627. }
  628. raw_spin_unlock_irqrestore(&db->lock, flags);
  629. if (print_object)
  630. debug_print_object(obj, "activate");
  631. return ret;
  632. }
  633. raw_spin_unlock_irqrestore(&db->lock, flags);
  634. /* If NULL the allocation has hit OOM */
  635. if (!obj) {
  636. debug_objects_oom();
  637. return 0;
  638. }
  639. /* Object is neither static nor tracked. It's not initialized */
  640. debug_print_object(&o, "activate");
  641. ret = debug_object_fixup(descr->fixup_activate, addr, ODEBUG_STATE_NOTAVAILABLE);
  642. return ret ? 0 : -EINVAL;
  643. }
  644. EXPORT_SYMBOL_GPL(debug_object_activate);
  645. /**
  646. * debug_object_deactivate - debug checks when an object is deactivated
  647. * @addr: address of the object
  648. * @descr: pointer to an object specific debug description structure
  649. */
  650. void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr)
  651. {
  652. struct debug_bucket *db;
  653. struct debug_obj *obj;
  654. unsigned long flags;
  655. bool print_object = false;
  656. if (!debug_objects_enabled)
  657. return;
  658. db = get_bucket((unsigned long) addr);
  659. raw_spin_lock_irqsave(&db->lock, flags);
  660. obj = lookup_object(addr, db);
  661. if (obj) {
  662. switch (obj->state) {
  663. case ODEBUG_STATE_INIT:
  664. case ODEBUG_STATE_INACTIVE:
  665. case ODEBUG_STATE_ACTIVE:
  666. if (!obj->astate)
  667. obj->state = ODEBUG_STATE_INACTIVE;
  668. else
  669. print_object = true;
  670. break;
  671. case ODEBUG_STATE_DESTROYED:
  672. print_object = true;
  673. break;
  674. default:
  675. break;
  676. }
  677. }
  678. raw_spin_unlock_irqrestore(&db->lock, flags);
  679. if (!obj) {
  680. struct debug_obj o = { .object = addr,
  681. .state = ODEBUG_STATE_NOTAVAILABLE,
  682. .descr = descr };
  683. debug_print_object(&o, "deactivate");
  684. } else if (print_object) {
  685. debug_print_object(obj, "deactivate");
  686. }
  687. }
  688. EXPORT_SYMBOL_GPL(debug_object_deactivate);
  689. /**
  690. * debug_object_destroy - debug checks when an object is destroyed
  691. * @addr: address of the object
  692. * @descr: pointer to an object specific debug description structure
  693. */
  694. void debug_object_destroy(void *addr, const struct debug_obj_descr *descr)
  695. {
  696. enum debug_obj_state state;
  697. struct debug_bucket *db;
  698. struct debug_obj *obj;
  699. unsigned long flags;
  700. bool print_object = false;
  701. if (!debug_objects_enabled)
  702. return;
  703. db = get_bucket((unsigned long) addr);
  704. raw_spin_lock_irqsave(&db->lock, flags);
  705. obj = lookup_object(addr, db);
  706. if (!obj)
  707. goto out_unlock;
  708. switch (obj->state) {
  709. case ODEBUG_STATE_NONE:
  710. case ODEBUG_STATE_INIT:
  711. case ODEBUG_STATE_INACTIVE:
  712. obj->state = ODEBUG_STATE_DESTROYED;
  713. break;
  714. case ODEBUG_STATE_ACTIVE:
  715. state = obj->state;
  716. raw_spin_unlock_irqrestore(&db->lock, flags);
  717. debug_print_object(obj, "destroy");
  718. debug_object_fixup(descr->fixup_destroy, addr, state);
  719. return;
  720. case ODEBUG_STATE_DESTROYED:
  721. print_object = true;
  722. break;
  723. default:
  724. break;
  725. }
  726. out_unlock:
  727. raw_spin_unlock_irqrestore(&db->lock, flags);
  728. if (print_object)
  729. debug_print_object(obj, "destroy");
  730. }
  731. EXPORT_SYMBOL_GPL(debug_object_destroy);
  732. /**
  733. * debug_object_free - debug checks when an object is freed
  734. * @addr: address of the object
  735. * @descr: pointer to an object specific debug description structure
  736. */
  737. void debug_object_free(void *addr, const struct debug_obj_descr *descr)
  738. {
  739. enum debug_obj_state state;
  740. struct debug_bucket *db;
  741. struct debug_obj *obj;
  742. unsigned long flags;
  743. if (!debug_objects_enabled)
  744. return;
  745. db = get_bucket((unsigned long) addr);
  746. raw_spin_lock_irqsave(&db->lock, flags);
  747. obj = lookup_object(addr, db);
  748. if (!obj)
  749. goto out_unlock;
  750. switch (obj->state) {
  751. case ODEBUG_STATE_ACTIVE:
  752. state = obj->state;
  753. raw_spin_unlock_irqrestore(&db->lock, flags);
  754. debug_print_object(obj, "free");
  755. debug_object_fixup(descr->fixup_free, addr, state);
  756. return;
  757. default:
  758. hlist_del(&obj->node);
  759. raw_spin_unlock_irqrestore(&db->lock, flags);
  760. free_object(obj);
  761. return;
  762. }
  763. out_unlock:
  764. raw_spin_unlock_irqrestore(&db->lock, flags);
  765. }
  766. EXPORT_SYMBOL_GPL(debug_object_free);
  767. /**
  768. * debug_object_assert_init - debug checks when object should be init-ed
  769. * @addr: address of the object
  770. * @descr: pointer to an object specific debug description structure
  771. */
  772. void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
  773. {
  774. struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
  775. struct debug_bucket *db;
  776. struct debug_obj *obj;
  777. unsigned long flags;
  778. if (!debug_objects_enabled)
  779. return;
  780. debug_objects_fill_pool();
  781. db = get_bucket((unsigned long) addr);
  782. raw_spin_lock_irqsave(&db->lock, flags);
  783. obj = lookup_object_or_alloc(addr, db, descr, false, true);
  784. raw_spin_unlock_irqrestore(&db->lock, flags);
  785. if (likely(!IS_ERR_OR_NULL(obj)))
  786. return;
  787. /* If NULL the allocation has hit OOM */
  788. if (!obj) {
  789. debug_objects_oom();
  790. return;
  791. }
  792. /* Object is neither tracked nor static. It's not initialized. */
  793. debug_print_object(&o, "assert_init");
  794. debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
  795. }
  796. EXPORT_SYMBOL_GPL(debug_object_assert_init);
  797. /**
  798. * debug_object_active_state - debug checks object usage state machine
  799. * @addr: address of the object
  800. * @descr: pointer to an object specific debug description structure
  801. * @expect: expected state
  802. * @next: state to move to if expected state is found
  803. */
  804. void
  805. debug_object_active_state(void *addr, const struct debug_obj_descr *descr,
  806. unsigned int expect, unsigned int next)
  807. {
  808. struct debug_bucket *db;
  809. struct debug_obj *obj;
  810. unsigned long flags;
  811. bool print_object = false;
  812. if (!debug_objects_enabled)
  813. return;
  814. db = get_bucket((unsigned long) addr);
  815. raw_spin_lock_irqsave(&db->lock, flags);
  816. obj = lookup_object(addr, db);
  817. if (obj) {
  818. switch (obj->state) {
  819. case ODEBUG_STATE_ACTIVE:
  820. if (obj->astate == expect)
  821. obj->astate = next;
  822. else
  823. print_object = true;
  824. break;
  825. default:
  826. print_object = true;
  827. break;
  828. }
  829. }
  830. raw_spin_unlock_irqrestore(&db->lock, flags);
  831. if (!obj) {
  832. struct debug_obj o = { .object = addr,
  833. .state = ODEBUG_STATE_NOTAVAILABLE,
  834. .descr = descr };
  835. debug_print_object(&o, "active_state");
  836. } else if (print_object) {
  837. debug_print_object(obj, "active_state");
  838. }
  839. }
  840. EXPORT_SYMBOL_GPL(debug_object_active_state);
  841. #ifdef CONFIG_DEBUG_OBJECTS_FREE
  842. static void __debug_check_no_obj_freed(const void *address, unsigned long size)
  843. {
  844. unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
  845. const struct debug_obj_descr *descr;
  846. enum debug_obj_state state;
  847. struct debug_bucket *db;
  848. struct hlist_node *tmp;
  849. struct debug_obj *obj;
  850. int cnt, objs_checked = 0;
  851. saddr = (unsigned long) address;
  852. eaddr = saddr + size;
  853. paddr = saddr & ODEBUG_CHUNK_MASK;
  854. chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
  855. chunks >>= ODEBUG_CHUNK_SHIFT;
  856. for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
  857. db = get_bucket(paddr);
  858. repeat:
  859. cnt = 0;
  860. raw_spin_lock_irqsave(&db->lock, flags);
  861. hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
  862. cnt++;
  863. oaddr = (unsigned long) obj->object;
  864. if (oaddr < saddr || oaddr >= eaddr)
  865. continue;
  866. switch (obj->state) {
  867. case ODEBUG_STATE_ACTIVE:
  868. descr = obj->descr;
  869. state = obj->state;
  870. raw_spin_unlock_irqrestore(&db->lock, flags);
  871. debug_print_object(obj, "free");
  872. debug_object_fixup(descr->fixup_free,
  873. (void *) oaddr, state);
  874. goto repeat;
  875. default:
  876. hlist_del(&obj->node);
  877. __free_object(obj);
  878. break;
  879. }
  880. }
  881. raw_spin_unlock_irqrestore(&db->lock, flags);
  882. if (cnt > debug_objects_maxchain)
  883. debug_objects_maxchain = cnt;
  884. objs_checked += cnt;
  885. }
  886. if (objs_checked > debug_objects_maxchecked)
  887. debug_objects_maxchecked = objs_checked;
  888. /* Schedule work to actually kmem_cache_free() objects */
  889. if (!READ_ONCE(obj_freeing) && READ_ONCE(obj_nr_tofree)) {
  890. WRITE_ONCE(obj_freeing, true);
  891. schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
  892. }
  893. }
  894. void debug_check_no_obj_freed(const void *address, unsigned long size)
  895. {
  896. if (debug_objects_enabled)
  897. __debug_check_no_obj_freed(address, size);
  898. }
  899. #endif
  900. #ifdef CONFIG_DEBUG_FS
  901. static int debug_stats_show(struct seq_file *m, void *v)
  902. {
  903. int cpu, obj_percpu_free = 0;
  904. for_each_possible_cpu(cpu)
  905. obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
  906. seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
  907. seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked);
  908. seq_printf(m, "warnings :%d\n", debug_objects_warnings);
  909. seq_printf(m, "fixups :%d\n", debug_objects_fixups);
  910. seq_printf(m, "pool_free :%d\n", READ_ONCE(obj_pool_free) + obj_percpu_free);
  911. seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
  912. seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
  913. seq_printf(m, "pool_used :%d\n", obj_pool_used - obj_percpu_free);
  914. seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
  915. seq_printf(m, "on_free_list :%d\n", READ_ONCE(obj_nr_tofree));
  916. seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
  917. seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
  918. return 0;
  919. }
  920. DEFINE_SHOW_ATTRIBUTE(debug_stats);
  921. static int __init debug_objects_init_debugfs(void)
  922. {
  923. struct dentry *dbgdir;
  924. if (!debug_objects_enabled)
  925. return 0;
  926. dbgdir = debugfs_create_dir("debug_objects", NULL);
  927. debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
  928. return 0;
  929. }
  930. __initcall(debug_objects_init_debugfs);
  931. #else
  932. static inline void debug_objects_init_debugfs(void) { }
  933. #endif
  934. #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
  935. /* Random data structure for the self test */
  936. struct self_test {
  937. unsigned long dummy1[6];
  938. int static_init;
  939. unsigned long dummy2[3];
  940. };
  941. static __initconst const struct debug_obj_descr descr_type_test;
  942. static bool __init is_static_object(void *addr)
  943. {
  944. struct self_test *obj = addr;
  945. return obj->static_init;
  946. }
  947. /*
  948. * fixup_init is called when:
  949. * - an active object is initialized
  950. */
  951. static bool __init fixup_init(void *addr, enum debug_obj_state state)
  952. {
  953. struct self_test *obj = addr;
  954. switch (state) {
  955. case ODEBUG_STATE_ACTIVE:
  956. debug_object_deactivate(obj, &descr_type_test);
  957. debug_object_init(obj, &descr_type_test);
  958. return true;
  959. default:
  960. return false;
  961. }
  962. }
  963. /*
  964. * fixup_activate is called when:
  965. * - an active object is activated
  966. * - an unknown non-static object is activated
  967. */
  968. static bool __init fixup_activate(void *addr, enum debug_obj_state state)
  969. {
  970. struct self_test *obj = addr;
  971. switch (state) {
  972. case ODEBUG_STATE_NOTAVAILABLE:
  973. return true;
  974. case ODEBUG_STATE_ACTIVE:
  975. debug_object_deactivate(obj, &descr_type_test);
  976. debug_object_activate(obj, &descr_type_test);
  977. return true;
  978. default:
  979. return false;
  980. }
  981. }
  982. /*
  983. * fixup_destroy is called when:
  984. * - an active object is destroyed
  985. */
  986. static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
  987. {
  988. struct self_test *obj = addr;
  989. switch (state) {
  990. case ODEBUG_STATE_ACTIVE:
  991. debug_object_deactivate(obj, &descr_type_test);
  992. debug_object_destroy(obj, &descr_type_test);
  993. return true;
  994. default:
  995. return false;
  996. }
  997. }
  998. /*
  999. * fixup_free is called when:
  1000. * - an active object is freed
  1001. */
  1002. static bool __init fixup_free(void *addr, enum debug_obj_state state)
  1003. {
  1004. struct self_test *obj = addr;
  1005. switch (state) {
  1006. case ODEBUG_STATE_ACTIVE:
  1007. debug_object_deactivate(obj, &descr_type_test);
  1008. debug_object_free(obj, &descr_type_test);
  1009. return true;
  1010. default:
  1011. return false;
  1012. }
  1013. }
  1014. static int __init
  1015. check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
  1016. {
  1017. struct debug_bucket *db;
  1018. struct debug_obj *obj;
  1019. unsigned long flags;
  1020. int res = -EINVAL;
  1021. db = get_bucket((unsigned long) addr);
  1022. raw_spin_lock_irqsave(&db->lock, flags);
  1023. obj = lookup_object(addr, db);
  1024. if (!obj && state != ODEBUG_STATE_NONE) {
  1025. WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
  1026. goto out;
  1027. }
  1028. if (obj && obj->state != state) {
  1029. WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
  1030. obj->state, state);
  1031. goto out;
  1032. }
  1033. if (fixups != debug_objects_fixups) {
  1034. WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
  1035. fixups, debug_objects_fixups);
  1036. goto out;
  1037. }
  1038. if (warnings != debug_objects_warnings) {
  1039. WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
  1040. warnings, debug_objects_warnings);
  1041. goto out;
  1042. }
  1043. res = 0;
  1044. out:
  1045. raw_spin_unlock_irqrestore(&db->lock, flags);
  1046. if (res)
  1047. debug_objects_enabled = 0;
  1048. return res;
  1049. }
  1050. static __initconst const struct debug_obj_descr descr_type_test = {
  1051. .name = "selftest",
  1052. .is_static_object = is_static_object,
  1053. .fixup_init = fixup_init,
  1054. .fixup_activate = fixup_activate,
  1055. .fixup_destroy = fixup_destroy,
  1056. .fixup_free = fixup_free,
  1057. };
  1058. static __initdata struct self_test obj = { .static_init = 0 };
  1059. static void __init debug_objects_selftest(void)
  1060. {
  1061. int fixups, oldfixups, warnings, oldwarnings;
  1062. unsigned long flags;
  1063. local_irq_save(flags);
  1064. fixups = oldfixups = debug_objects_fixups;
  1065. warnings = oldwarnings = debug_objects_warnings;
  1066. descr_test = &descr_type_test;
  1067. debug_object_init(&obj, &descr_type_test);
  1068. if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
  1069. goto out;
  1070. debug_object_activate(&obj, &descr_type_test);
  1071. if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  1072. goto out;
  1073. debug_object_activate(&obj, &descr_type_test);
  1074. if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
  1075. goto out;
  1076. debug_object_deactivate(&obj, &descr_type_test);
  1077. if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
  1078. goto out;
  1079. debug_object_destroy(&obj, &descr_type_test);
  1080. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
  1081. goto out;
  1082. debug_object_init(&obj, &descr_type_test);
  1083. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  1084. goto out;
  1085. debug_object_activate(&obj, &descr_type_test);
  1086. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  1087. goto out;
  1088. debug_object_deactivate(&obj, &descr_type_test);
  1089. if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  1090. goto out;
  1091. debug_object_free(&obj, &descr_type_test);
  1092. if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
  1093. goto out;
  1094. obj.static_init = 1;
  1095. debug_object_activate(&obj, &descr_type_test);
  1096. if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  1097. goto out;
  1098. debug_object_init(&obj, &descr_type_test);
  1099. if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
  1100. goto out;
  1101. debug_object_free(&obj, &descr_type_test);
  1102. if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
  1103. goto out;
  1104. #ifdef CONFIG_DEBUG_OBJECTS_FREE
  1105. debug_object_init(&obj, &descr_type_test);
  1106. if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
  1107. goto out;
  1108. debug_object_activate(&obj, &descr_type_test);
  1109. if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  1110. goto out;
  1111. __debug_check_no_obj_freed(&obj, sizeof(obj));
  1112. if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
  1113. goto out;
  1114. #endif
  1115. pr_info("selftest passed\n");
  1116. out:
  1117. debug_objects_fixups = oldfixups;
  1118. debug_objects_warnings = oldwarnings;
  1119. descr_test = NULL;
  1120. local_irq_restore(flags);
  1121. }
  1122. #else
  1123. static inline void debug_objects_selftest(void) { }
  1124. #endif
  1125. /*
  1126. * Called during early boot to initialize the hash buckets and link
  1127. * the static object pool objects into the poll list. After this call
  1128. * the object tracker is fully operational.
  1129. */
  1130. void __init debug_objects_early_init(void)
  1131. {
  1132. int i;
  1133. for (i = 0; i < ODEBUG_HASH_SIZE; i++)
  1134. raw_spin_lock_init(&obj_hash[i].lock);
  1135. for (i = 0; i < ODEBUG_POOL_SIZE; i++)
  1136. hlist_add_head(&obj_static_pool[i].node, &obj_pool);
  1137. }
  1138. /*
  1139. * Convert the statically allocated objects to dynamic ones:
  1140. */
  1141. static int __init debug_objects_replace_static_objects(void)
  1142. {
  1143. struct debug_bucket *db = obj_hash;
  1144. struct hlist_node *tmp;
  1145. struct debug_obj *obj, *new;
  1146. HLIST_HEAD(objects);
  1147. int i, cnt = 0;
  1148. for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
  1149. obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
  1150. if (!obj)
  1151. goto free;
  1152. hlist_add_head(&obj->node, &objects);
  1153. }
  1154. debug_objects_allocated += i;
  1155. /*
  1156. * debug_objects_mem_init() is now called early that only one CPU is up
  1157. * and interrupts have been disabled, so it is safe to replace the
  1158. * active object references.
  1159. */
  1160. /* Remove the statically allocated objects from the pool */
  1161. hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
  1162. hlist_del(&obj->node);
  1163. /* Move the allocated objects to the pool */
  1164. hlist_move_list(&objects, &obj_pool);
  1165. /* Replace the active object references */
  1166. for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
  1167. hlist_move_list(&db->list, &objects);
  1168. hlist_for_each_entry(obj, &objects, node) {
  1169. new = hlist_entry(obj_pool.first, typeof(*obj), node);
  1170. hlist_del(&new->node);
  1171. /* copy object data */
  1172. *new = *obj;
  1173. hlist_add_head(&new->node, &db->list);
  1174. cnt++;
  1175. }
  1176. }
  1177. pr_debug("%d of %d active objects replaced\n",
  1178. cnt, obj_pool_used);
  1179. return 0;
  1180. free:
  1181. hlist_for_each_entry_safe(obj, tmp, &objects, node) {
  1182. hlist_del(&obj->node);
  1183. kmem_cache_free(obj_cache, obj);
  1184. }
  1185. return -ENOMEM;
  1186. }
  1187. /*
  1188. * Called after the kmem_caches are functional to setup a dedicated
  1189. * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
  1190. * prevents that the debug code is called on kmem_cache_free() for the
  1191. * debug tracker objects to avoid recursive calls.
  1192. */
  1193. void __init debug_objects_mem_init(void)
  1194. {
  1195. int cpu, extras;
  1196. if (!debug_objects_enabled)
  1197. return;
  1198. /*
  1199. * Initialize the percpu object pools
  1200. *
  1201. * Initialization is not strictly necessary, but was done for
  1202. * completeness.
  1203. */
  1204. for_each_possible_cpu(cpu)
  1205. INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
  1206. obj_cache = kmem_cache_create("debug_objects_cache",
  1207. sizeof (struct debug_obj), 0,
  1208. SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
  1209. NULL);
  1210. if (!obj_cache || debug_objects_replace_static_objects()) {
  1211. debug_objects_enabled = 0;
  1212. kmem_cache_destroy(obj_cache);
  1213. pr_warn("out of memory.\n");
  1214. return;
  1215. } else
  1216. debug_objects_selftest();
  1217. #ifdef CONFIG_HOTPLUG_CPU
  1218. cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
  1219. object_cpu_offline);
  1220. #endif
  1221. /*
  1222. * Increase the thresholds for allocating and freeing objects
  1223. * according to the number of possible CPUs available in the system.
  1224. */
  1225. extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
  1226. debug_objects_pool_size += extras;
  1227. debug_objects_pool_min_level += extras;
  1228. }