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