debugobjects.c 38 KB

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