xref: /linux-6.15/lib/debugobjects.c (revision ff8d523c)
19e4a51adSThomas Gleixner // SPDX-License-Identifier: GPL-2.0
23ac7fe5aSThomas Gleixner /*
33ac7fe5aSThomas Gleixner  * Generic infrastructure for lifetime debugging of objects.
43ac7fe5aSThomas Gleixner  *
53ac7fe5aSThomas Gleixner  * Copyright (C) 2008, Thomas Gleixner <[email protected]>
63ac7fe5aSThomas Gleixner  */
7719e4843SFabian Frederick 
8719e4843SFabian Frederick #define pr_fmt(fmt) "ODEBUG: " fmt
9719e4843SFabian Frederick 
1014077b9eSThomas Gleixner #include <linux/cpu.h>
113ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
1214077b9eSThomas Gleixner #include <linux/debugfs.h>
1314077b9eSThomas Gleixner #include <linux/hash.h>
1414077b9eSThomas Gleixner #include <linux/kmemleak.h>
15d43c36dcSAlexey Dobriyan #include <linux/sched.h>
16*ff8d523cSThomas Gleixner #include <linux/sched/loadavg.h>
1768db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
183ac7fe5aSThomas Gleixner #include <linux/seq_file.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
2014077b9eSThomas Gleixner #include <linux/static_key.h>
213ac7fe5aSThomas Gleixner 
223ac7fe5aSThomas Gleixner #define ODEBUG_HASH_BITS	14
233ac7fe5aSThomas Gleixner #define ODEBUG_HASH_SIZE	(1 << ODEBUG_HASH_BITS)
243ac7fe5aSThomas Gleixner 
2574fe1ad4SThomas Gleixner /* Must be power of two */
26634d61f4SWaiman Long #define ODEBUG_BATCH_SIZE	16
273ac7fe5aSThomas Gleixner 
2874fe1ad4SThomas Gleixner /* Initial values. Must all be a multiple of batch size */
2974fe1ad4SThomas Gleixner #define ODEBUG_POOL_SIZE	(64 * ODEBUG_BATCH_SIZE)
3074fe1ad4SThomas Gleixner #define ODEBUG_POOL_MIN_LEVEL	(ODEBUG_POOL_SIZE / 4)
3174fe1ad4SThomas Gleixner 
32a201a96bSThomas Gleixner #define ODEBUG_POOL_PERCPU_SIZE	(8 * ODEBUG_BATCH_SIZE)
3374fe1ad4SThomas Gleixner 
343ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_SHIFT	PAGE_SHIFT
353ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_SIZE	(1 << ODEBUG_CHUNK_SHIFT)
363ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_MASK	(~(ODEBUG_CHUNK_SIZE - 1))
373ac7fe5aSThomas Gleixner 
38a7344a68SWaiman Long /*
39a7344a68SWaiman Long  * We limit the freeing of debug objects via workqueue at a maximum
40a7344a68SWaiman Long  * frequency of 10Hz and about 1024 objects for each freeing operation.
41a7344a68SWaiman Long  * So it is freeing at most 10k debug objects per second.
42a7344a68SWaiman Long  */
439ce99c6dSThomas Gleixner #define ODEBUG_FREE_WORK_MAX	(1024 / ODEBUG_BATCH_SIZE)
44a7344a68SWaiman Long #define ODEBUG_FREE_WORK_DELAY	DIV_ROUND_UP(HZ, 10)
45a7344a68SWaiman Long 
463ac7fe5aSThomas Gleixner struct debug_bucket {
473ac7fe5aSThomas Gleixner 	struct hlist_head	list;
48aef9cb05SThomas Gleixner 	raw_spinlock_t		lock;
493ac7fe5aSThomas Gleixner };
503ac7fe5aSThomas Gleixner 
512638345dSThomas Gleixner struct pool_stats {
522638345dSThomas Gleixner 	unsigned int		cur_used;
532638345dSThomas Gleixner 	unsigned int		max_used;
542638345dSThomas Gleixner 	unsigned int		min_fill;
552638345dSThomas Gleixner };
562638345dSThomas Gleixner 
57e18328ffSThomas Gleixner struct obj_pool {
58e18328ffSThomas Gleixner 	struct hlist_head	objects;
59e18328ffSThomas Gleixner 	unsigned int		cnt;
6096a9a042SThomas Gleixner 	unsigned int		min_cnt;
6196a9a042SThomas Gleixner 	unsigned int		max_cnt;
622638345dSThomas Gleixner 	struct pool_stats	stats;
63e18328ffSThomas Gleixner } ____cacheline_aligned;
64e18328ffSThomas Gleixner 
6596a9a042SThomas Gleixner 
6696a9a042SThomas Gleixner static DEFINE_PER_CPU_ALIGNED(struct obj_pool, pool_pcpu)  = {
6796a9a042SThomas Gleixner 	.max_cnt	= ODEBUG_POOL_PERCPU_SIZE,
6896a9a042SThomas Gleixner };
69d86998b1SWaiman Long 
703ac7fe5aSThomas Gleixner static struct debug_bucket	obj_hash[ODEBUG_HASH_SIZE];
713ac7fe5aSThomas Gleixner 
721be1cb7bSThomas Gleixner static struct debug_obj		obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
733ac7fe5aSThomas Gleixner 
74aef9cb05SThomas Gleixner static DEFINE_RAW_SPINLOCK(pool_lock);
753ac7fe5aSThomas Gleixner 
7696a9a042SThomas Gleixner static struct obj_pool pool_global = {
7796a9a042SThomas Gleixner 	.min_cnt		= ODEBUG_POOL_MIN_LEVEL,
7896a9a042SThomas Gleixner 	.max_cnt		= ODEBUG_POOL_SIZE,
792638345dSThomas Gleixner 	.stats			= {
802638345dSThomas Gleixner 		.min_fill	= ODEBUG_POOL_SIZE,
812638345dSThomas Gleixner 	},
8296a9a042SThomas Gleixner };
8396a9a042SThomas Gleixner 
8496a9a042SThomas Gleixner static struct obj_pool pool_to_free = {
8596a9a042SThomas Gleixner 	.max_cnt	= UINT_MAX,
8696a9a042SThomas Gleixner };
873ac7fe5aSThomas Gleixner 
88cb58d190SThomas Gleixner static HLIST_HEAD(pool_boot);
89cb58d190SThomas Gleixner 
90*ff8d523cSThomas Gleixner static unsigned long		avg_usage;
91a7344a68SWaiman Long static bool			obj_freeing;
923ac7fe5aSThomas Gleixner 
935b5baba6SBreno Leitao static int __data_racy			debug_objects_maxchain __read_mostly;
945b5baba6SBreno Leitao static int __data_racy __maybe_unused	debug_objects_maxchecked __read_mostly;
955b5baba6SBreno Leitao static int __data_racy			debug_objects_fixups __read_mostly;
965b5baba6SBreno Leitao static int __data_racy			debug_objects_warnings __read_mostly;
97661cc28bSThomas Gleixner static bool __data_racy			debug_objects_enabled __read_mostly
983ae70205SIngo Molnar 					= CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
995b5baba6SBreno Leitao 
100aedcade6SStephen Boyd static const struct debug_obj_descr	*descr_test  __read_mostly;
10168279f9cSAlexey Dobriyan static struct kmem_cache		*obj_cache __ro_after_init;
1023ac7fe5aSThomas Gleixner 
103c4b73aabSWaiman Long /*
1040cad93c3SWaiman Long  * Track numbers of kmem_cache_alloc()/free() calls done.
105c4b73aabSWaiman Long  */
106e4757c71SZhen Lei static int __data_racy		debug_objects_allocated;
107e4757c71SZhen Lei static int __data_racy		debug_objects_freed;
108c4b73aabSWaiman Long 
109337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work);
110a7344a68SWaiman Long static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work);
111337fff8bSThomas Gleixner 
11214077b9eSThomas Gleixner static DEFINE_STATIC_KEY_FALSE(obj_cache_enabled);
11314077b9eSThomas Gleixner 
enable_object_debug(char * str)1143ac7fe5aSThomas Gleixner static int __init enable_object_debug(char *str)
1153ac7fe5aSThomas Gleixner {
116661cc28bSThomas Gleixner 	debug_objects_enabled = true;
1173ac7fe5aSThomas Gleixner 	return 0;
1183ac7fe5aSThomas Gleixner }
119661cc28bSThomas Gleixner early_param("debug_objects", enable_object_debug);
1203e8ebb5cSKyle McMartin 
disable_object_debug(char * str)1213e8ebb5cSKyle McMartin static int __init disable_object_debug(char *str)
1223e8ebb5cSKyle McMartin {
123661cc28bSThomas Gleixner 	debug_objects_enabled = false;
1243e8ebb5cSKyle McMartin 	return 0;
1253e8ebb5cSKyle McMartin }
1263e8ebb5cSKyle McMartin early_param("no_debug_objects", disable_object_debug);
1273ac7fe5aSThomas Gleixner 
1283ac7fe5aSThomas Gleixner static const char *obj_states[ODEBUG_STATE_MAX] = {
1293ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_NONE]		= "none",
1303ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_INIT]		= "initialized",
1313ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_INACTIVE]		= "inactive",
1323ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_ACTIVE]		= "active",
1333ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_DESTROYED]	= "destroyed",
1343ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_NOTAVAILABLE]	= "not available",
1353ac7fe5aSThomas Gleixner };
1363ac7fe5aSThomas Gleixner 
pool_count(struct obj_pool * pool)137e18328ffSThomas Gleixner static __always_inline unsigned int pool_count(struct obj_pool *pool)
138e18328ffSThomas Gleixner {
139e18328ffSThomas Gleixner 	return READ_ONCE(pool->cnt);
140e18328ffSThomas Gleixner }
141e18328ffSThomas Gleixner 
pool_should_refill(struct obj_pool * pool)14296a9a042SThomas Gleixner static __always_inline bool pool_should_refill(struct obj_pool *pool)
143e18328ffSThomas Gleixner {
14496a9a042SThomas Gleixner 	return pool_count(pool) < pool->min_cnt;
145e18328ffSThomas Gleixner }
146e18328ffSThomas Gleixner 
pool_must_refill(struct obj_pool * pool)14796a9a042SThomas Gleixner static __always_inline bool pool_must_refill(struct obj_pool *pool)
148e18328ffSThomas Gleixner {
14996a9a042SThomas Gleixner 	return pool_count(pool) < pool->min_cnt / 2;
150e18328ffSThomas Gleixner }
151e18328ffSThomas Gleixner 
pool_move_batch(struct obj_pool * dst,struct obj_pool * src)152fb60c004SThomas Gleixner static bool pool_move_batch(struct obj_pool *dst, struct obj_pool *src)
153fb60c004SThomas Gleixner {
154f57ebb92SThomas Gleixner 	struct hlist_node *last, *next_batch, *first_batch;
155f57ebb92SThomas Gleixner 	struct debug_obj *obj;
156f57ebb92SThomas Gleixner 
157f57ebb92SThomas Gleixner 	if (dst->cnt >= dst->max_cnt || !src->cnt)
158fb60c004SThomas Gleixner 		return false;
159fb60c004SThomas Gleixner 
160f57ebb92SThomas Gleixner 	first_batch = src->objects.first;
161f57ebb92SThomas Gleixner 	obj = hlist_entry(first_batch, typeof(*obj), node);
162f57ebb92SThomas Gleixner 	last = obj->batch_last;
163f57ebb92SThomas Gleixner 	next_batch = last->next;
164fb60c004SThomas Gleixner 
165f57ebb92SThomas Gleixner 	/* Move the next batch to the front of the source pool */
166f57ebb92SThomas Gleixner 	src->objects.first = next_batch;
167f57ebb92SThomas Gleixner 	if (next_batch)
168f57ebb92SThomas Gleixner 		next_batch->pprev = &src->objects.first;
169fb60c004SThomas Gleixner 
170f57ebb92SThomas Gleixner 	/* Add the extracted batch to the destination pool */
171f57ebb92SThomas Gleixner 	last->next = dst->objects.first;
172f57ebb92SThomas Gleixner 	if (last->next)
173f57ebb92SThomas Gleixner 		last->next->pprev = &last->next;
174f57ebb92SThomas Gleixner 	first_batch->pprev = &dst->objects.first;
175f57ebb92SThomas Gleixner 	dst->objects.first = first_batch;
176f57ebb92SThomas Gleixner 
177f57ebb92SThomas Gleixner 	WRITE_ONCE(src->cnt, src->cnt - ODEBUG_BATCH_SIZE);
178f57ebb92SThomas Gleixner 	WRITE_ONCE(dst->cnt, dst->cnt + ODEBUG_BATCH_SIZE);
179fb60c004SThomas Gleixner 	return true;
180fb60c004SThomas Gleixner }
181fb60c004SThomas Gleixner 
pool_push_batch(struct obj_pool * dst,struct hlist_head * head)182aebbfe07SThomas Gleixner static bool pool_push_batch(struct obj_pool *dst, struct hlist_head *head)
183aebbfe07SThomas Gleixner {
184aebbfe07SThomas Gleixner 	struct hlist_node *last;
185aebbfe07SThomas Gleixner 	struct debug_obj *obj;
186aebbfe07SThomas Gleixner 
187aebbfe07SThomas Gleixner 	if (dst->cnt >= dst->max_cnt)
188aebbfe07SThomas Gleixner 		return false;
189aebbfe07SThomas Gleixner 
190aebbfe07SThomas Gleixner 	obj = hlist_entry(head->first, typeof(*obj), node);
191aebbfe07SThomas Gleixner 	last = obj->batch_last;
192aebbfe07SThomas Gleixner 
193aebbfe07SThomas Gleixner 	hlist_splice_init(head, last, &dst->objects);
194aebbfe07SThomas Gleixner 	WRITE_ONCE(dst->cnt, dst->cnt + ODEBUG_BATCH_SIZE);
195aebbfe07SThomas Gleixner 	return true;
196aebbfe07SThomas Gleixner }
197aebbfe07SThomas Gleixner 
pool_pop_batch(struct hlist_head * head,struct obj_pool * src)1989ce99c6dSThomas Gleixner static bool pool_pop_batch(struct hlist_head *head, struct obj_pool *src)
1999ce99c6dSThomas Gleixner {
200f57ebb92SThomas Gleixner 	struct hlist_node *last, *next;
201f57ebb92SThomas Gleixner 	struct debug_obj *obj;
202f57ebb92SThomas Gleixner 
2039ce99c6dSThomas Gleixner 	if (!src->cnt)
2049ce99c6dSThomas Gleixner 		return false;
2059ce99c6dSThomas Gleixner 
206f57ebb92SThomas Gleixner 	/* Move the complete list to the head */
207f57ebb92SThomas Gleixner 	hlist_move_list(&src->objects, head);
2089ce99c6dSThomas Gleixner 
209f57ebb92SThomas Gleixner 	obj = hlist_entry(head->first, typeof(*obj), node);
210f57ebb92SThomas Gleixner 	last = obj->batch_last;
211f57ebb92SThomas Gleixner 	next = last->next;
212f57ebb92SThomas Gleixner 	/* Disconnect the batch from the list */
213f57ebb92SThomas Gleixner 	last->next = NULL;
214f57ebb92SThomas Gleixner 
215f57ebb92SThomas Gleixner 	/* Move the node after last back to the source pool. */
216f57ebb92SThomas Gleixner 	src->objects.first = next;
217f57ebb92SThomas Gleixner 	if (next)
218f57ebb92SThomas Gleixner 		next->pprev = &src->objects.first;
219f57ebb92SThomas Gleixner 
220f57ebb92SThomas Gleixner 	WRITE_ONCE(src->cnt, src->cnt - ODEBUG_BATCH_SIZE);
2219ce99c6dSThomas Gleixner 	return true;
2229ce99c6dSThomas Gleixner }
2239ce99c6dSThomas Gleixner 
__alloc_object(struct hlist_head * list)224fb60c004SThomas Gleixner static struct debug_obj *__alloc_object(struct hlist_head *list)
225fb60c004SThomas Gleixner {
226fb60c004SThomas Gleixner 	struct debug_obj *obj;
227fb60c004SThomas Gleixner 
228fb60c004SThomas Gleixner 	if (unlikely(!list->first))
229fb60c004SThomas Gleixner 		return NULL;
230fb60c004SThomas Gleixner 
231fb60c004SThomas Gleixner 	obj = hlist_entry(list->first, typeof(*obj), node);
232fb60c004SThomas Gleixner 	hlist_del(&obj->node);
233fb60c004SThomas Gleixner 	return obj;
234fb60c004SThomas Gleixner }
235fb60c004SThomas Gleixner 
pcpu_refill_stats(void)2362638345dSThomas Gleixner static void pcpu_refill_stats(void)
2372638345dSThomas Gleixner {
2382638345dSThomas Gleixner 	struct pool_stats *stats = &pool_global.stats;
2392638345dSThomas Gleixner 
2402638345dSThomas Gleixner 	WRITE_ONCE(stats->cur_used, stats->cur_used + ODEBUG_BATCH_SIZE);
2412638345dSThomas Gleixner 
2422638345dSThomas Gleixner 	if (stats->cur_used > stats->max_used)
2432638345dSThomas Gleixner 		stats->max_used = stats->cur_used;
2442638345dSThomas Gleixner 
2452638345dSThomas Gleixner 	if (pool_global.cnt < stats->min_fill)
2462638345dSThomas Gleixner 		stats->min_fill = pool_global.cnt;
2472638345dSThomas Gleixner }
2482638345dSThomas Gleixner 
pcpu_alloc(void)249fb60c004SThomas Gleixner static struct debug_obj *pcpu_alloc(void)
250fb60c004SThomas Gleixner {
251fb60c004SThomas Gleixner 	struct obj_pool *pcp = this_cpu_ptr(&pool_pcpu);
252fb60c004SThomas Gleixner 
253fb60c004SThomas Gleixner 	lockdep_assert_irqs_disabled();
254fb60c004SThomas Gleixner 
255fb60c004SThomas Gleixner 	for (;;) {
256fb60c004SThomas Gleixner 		struct debug_obj *obj = __alloc_object(&pcp->objects);
257fb60c004SThomas Gleixner 
258fb60c004SThomas Gleixner 		if (likely(obj)) {
259fb60c004SThomas Gleixner 			pcp->cnt--;
26013f9ca72SThomas Gleixner 			/*
26113f9ca72SThomas Gleixner 			 * If this emptied a batch try to refill from the
26213f9ca72SThomas Gleixner 			 * free pool. Don't do that if this was the top-most
26313f9ca72SThomas Gleixner 			 * batch as pcpu_free() expects the per CPU pool
26413f9ca72SThomas Gleixner 			 * to be less than ODEBUG_POOL_PERCPU_SIZE.
26513f9ca72SThomas Gleixner 			 */
26613f9ca72SThomas Gleixner 			if (unlikely(pcp->cnt < (ODEBUG_POOL_PERCPU_SIZE - ODEBUG_BATCH_SIZE) &&
26713f9ca72SThomas Gleixner 				     !(pcp->cnt % ODEBUG_BATCH_SIZE))) {
26813f9ca72SThomas Gleixner 				/*
26913f9ca72SThomas Gleixner 				 * Don't try to allocate from the regular pool here
27013f9ca72SThomas Gleixner 				 * to not exhaust it prematurely.
27113f9ca72SThomas Gleixner 				 */
27213f9ca72SThomas Gleixner 				if (pool_count(&pool_to_free)) {
27313f9ca72SThomas Gleixner 					guard(raw_spinlock)(&pool_lock);
27413f9ca72SThomas Gleixner 					pool_move_batch(pcp, &pool_to_free);
27513f9ca72SThomas Gleixner 					pcpu_refill_stats();
27613f9ca72SThomas Gleixner 				}
27713f9ca72SThomas Gleixner 			}
278fb60c004SThomas Gleixner 			return obj;
279fb60c004SThomas Gleixner 		}
280fb60c004SThomas Gleixner 
281fb60c004SThomas Gleixner 		guard(raw_spinlock)(&pool_lock);
282fb60c004SThomas Gleixner 		if (!pool_move_batch(pcp, &pool_to_free)) {
283fb60c004SThomas Gleixner 			if (!pool_move_batch(pcp, &pool_global))
284fb60c004SThomas Gleixner 				return NULL;
285fb60c004SThomas Gleixner 		}
2862638345dSThomas Gleixner 		pcpu_refill_stats();
287fb60c004SThomas Gleixner 	}
288fb60c004SThomas Gleixner }
289fb60c004SThomas Gleixner 
pcpu_free(struct debug_obj * obj)290a3b9e191SThomas Gleixner static void pcpu_free(struct debug_obj *obj)
291a3b9e191SThomas Gleixner {
292a3b9e191SThomas Gleixner 	struct obj_pool *pcp = this_cpu_ptr(&pool_pcpu);
293f57ebb92SThomas Gleixner 	struct debug_obj *first;
294a3b9e191SThomas Gleixner 
295a3b9e191SThomas Gleixner 	lockdep_assert_irqs_disabled();
296a3b9e191SThomas Gleixner 
297f57ebb92SThomas Gleixner 	if (!(pcp->cnt % ODEBUG_BATCH_SIZE)) {
298f57ebb92SThomas Gleixner 		obj->batch_last = &obj->node;
299f57ebb92SThomas Gleixner 	} else {
300f57ebb92SThomas Gleixner 		first = hlist_entry(pcp->objects.first, typeof(*first), node);
301f57ebb92SThomas Gleixner 		obj->batch_last = first->batch_last;
302f57ebb92SThomas Gleixner 	}
303a3b9e191SThomas Gleixner 	hlist_add_head(&obj->node, &pcp->objects);
304a3b9e191SThomas Gleixner 	pcp->cnt++;
305a3b9e191SThomas Gleixner 
306a3b9e191SThomas Gleixner 	/* Pool full ? */
307a3b9e191SThomas Gleixner 	if (pcp->cnt < ODEBUG_POOL_PERCPU_SIZE)
308a3b9e191SThomas Gleixner 		return;
309a3b9e191SThomas Gleixner 
310a3b9e191SThomas Gleixner 	/* Remove a batch from the per CPU pool */
311a3b9e191SThomas Gleixner 	guard(raw_spinlock)(&pool_lock);
312a3b9e191SThomas Gleixner 	/* Try to fit the batch into the pool_global first */
313a3b9e191SThomas Gleixner 	if (!pool_move_batch(&pool_global, pcp))
314a3b9e191SThomas Gleixner 		pool_move_batch(&pool_to_free, pcp);
3152638345dSThomas Gleixner 	WRITE_ONCE(pool_global.stats.cur_used, pool_global.stats.cur_used - ODEBUG_BATCH_SIZE);
316a3b9e191SThomas Gleixner }
317a3b9e191SThomas Gleixner 
free_object_list(struct hlist_head * head)31849a5cb82SThomas Gleixner static void free_object_list(struct hlist_head *head)
31949a5cb82SThomas Gleixner {
32049a5cb82SThomas Gleixner 	struct hlist_node *tmp;
32149a5cb82SThomas Gleixner 	struct debug_obj *obj;
32249a5cb82SThomas Gleixner 	int cnt = 0;
32349a5cb82SThomas Gleixner 
32449a5cb82SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, head, node) {
32549a5cb82SThomas Gleixner 		hlist_del(&obj->node);
32649a5cb82SThomas Gleixner 		kmem_cache_free(obj_cache, obj);
32749a5cb82SThomas Gleixner 		cnt++;
32849a5cb82SThomas Gleixner 	}
32949a5cb82SThomas Gleixner 	debug_objects_freed += cnt;
33049a5cb82SThomas Gleixner }
33149a5cb82SThomas Gleixner 
fill_pool_from_freelist(void)332d8c6cd3aSZhen Lei static void fill_pool_from_freelist(void)
3333ac7fe5aSThomas Gleixner {
334d8c6cd3aSZhen Lei 	static unsigned long state;
3353ac7fe5aSThomas Gleixner 
33636c4ead6SYang Shi 	/*
33763a4a9b5SZhen Lei 	 * Reuse objs from the global obj_to_free list; they will be
33863a4a9b5SZhen Lei 	 * reinitialized when allocating.
33936c4ead6SYang Shi 	 */
340e18328ffSThomas Gleixner 	if (!pool_count(&pool_to_free))
341d8c6cd3aSZhen Lei 		return;
342d8c6cd3aSZhen Lei 
343d8c6cd3aSZhen Lei 	/*
344d8c6cd3aSZhen Lei 	 * Prevent the context from being scheduled or interrupted after
345d8c6cd3aSZhen Lei 	 * setting the state flag;
346d8c6cd3aSZhen Lei 	 */
347d8c6cd3aSZhen Lei 	guard(irqsave)();
348d8c6cd3aSZhen Lei 
349d8c6cd3aSZhen Lei 	/*
350d8c6cd3aSZhen Lei 	 * Avoid lock contention on &pool_lock and avoid making the cache
351d8c6cd3aSZhen Lei 	 * line exclusive by testing the bit before attempting to set it.
352d8c6cd3aSZhen Lei 	 */
353d8c6cd3aSZhen Lei 	if (test_bit(0, &state) || test_and_set_bit(0, &state))
354d8c6cd3aSZhen Lei 		return;
355d8c6cd3aSZhen Lei 
356fb60c004SThomas Gleixner 	/* Avoid taking the lock when there is no work to do */
357fb60c004SThomas Gleixner 	while (pool_should_refill(&pool_global) && pool_count(&pool_to_free)) {
358d8c6cd3aSZhen Lei 		guard(raw_spinlock)(&pool_lock);
359fb60c004SThomas Gleixner 		/* Move a batch if possible */
360fb60c004SThomas Gleixner 		pool_move_batch(&pool_global, &pool_to_free);
36136c4ead6SYang Shi 	}
362d8c6cd3aSZhen Lei 	clear_bit(0, &state);
36336c4ead6SYang Shi }
36436c4ead6SYang Shi 
kmem_alloc_batch(struct hlist_head * head,struct kmem_cache * cache,gfp_t gfp)365aebbfe07SThomas Gleixner static bool kmem_alloc_batch(struct hlist_head *head, struct kmem_cache *cache, gfp_t gfp)
366aebbfe07SThomas Gleixner {
367aebbfe07SThomas Gleixner 	struct hlist_node *last = NULL;
368aebbfe07SThomas Gleixner 	struct debug_obj *obj;
369aebbfe07SThomas Gleixner 
370aebbfe07SThomas Gleixner 	for (int cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
371aebbfe07SThomas Gleixner 		obj = kmem_cache_zalloc(cache, gfp);
372aebbfe07SThomas Gleixner 		if (!obj) {
373aebbfe07SThomas Gleixner 			free_object_list(head);
374aebbfe07SThomas Gleixner 			return false;
375aebbfe07SThomas Gleixner 		}
376aebbfe07SThomas Gleixner 		debug_objects_allocated++;
377aebbfe07SThomas Gleixner 
378aebbfe07SThomas Gleixner 		if (!last)
379aebbfe07SThomas Gleixner 			last = &obj->node;
380aebbfe07SThomas Gleixner 		obj->batch_last = last;
381aebbfe07SThomas Gleixner 
382aebbfe07SThomas Gleixner 		hlist_add_head(&obj->node, head);
383aebbfe07SThomas Gleixner 	}
384aebbfe07SThomas Gleixner 	return true;
385aebbfe07SThomas Gleixner }
386aebbfe07SThomas Gleixner 
fill_pool(void)387d8c6cd3aSZhen Lei static void fill_pool(void)
388d8c6cd3aSZhen Lei {
389d8c6cd3aSZhen Lei 	static atomic_t cpus_allocating;
390d8c6cd3aSZhen Lei 
391d8c6cd3aSZhen Lei 	/*
392d8c6cd3aSZhen Lei 	 * Avoid allocation and lock contention when:
393d8c6cd3aSZhen Lei 	 *   - One other CPU is already allocating
394d8c6cd3aSZhen Lei 	 *   - the global pool has not reached the critical level yet
395d8c6cd3aSZhen Lei 	 */
39696a9a042SThomas Gleixner 	if (!pool_must_refill(&pool_global) && atomic_read(&cpus_allocating))
3971fda107dSThomas Gleixner 		return;
3983ac7fe5aSThomas Gleixner 
399d8c6cd3aSZhen Lei 	atomic_inc(&cpus_allocating);
40096a9a042SThomas Gleixner 	while (pool_should_refill(&pool_global)) {
401813fd078SZhen Lei 		HLIST_HEAD(head);
4023ac7fe5aSThomas Gleixner 
403aebbfe07SThomas Gleixner 		if (!kmem_alloc_batch(&head, obj_cache, __GFP_HIGH | __GFP_NOWARN))
404d8c6cd3aSZhen Lei 			break;
4053ac7fe5aSThomas Gleixner 
406d8c6cd3aSZhen Lei 		guard(raw_spinlock_irqsave)(&pool_lock);
407aebbfe07SThomas Gleixner 		if (!pool_push_batch(&pool_global, &head))
408aebbfe07SThomas Gleixner 			pool_push_batch(&pool_to_free, &head);
4093ac7fe5aSThomas Gleixner 	}
410d8c6cd3aSZhen Lei 	atomic_dec(&cpus_allocating);
4113ac7fe5aSThomas Gleixner }
4123ac7fe5aSThomas Gleixner 
4133ac7fe5aSThomas Gleixner /*
4143ac7fe5aSThomas Gleixner  * Lookup an object in the hash bucket.
4153ac7fe5aSThomas Gleixner  */
lookup_object(void * addr,struct debug_bucket * b)4163ac7fe5aSThomas Gleixner static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
4173ac7fe5aSThomas Gleixner {
4183ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
4193ac7fe5aSThomas Gleixner 	int cnt = 0;
4203ac7fe5aSThomas Gleixner 
421b67bfe0dSSasha Levin 	hlist_for_each_entry(obj, &b->list, node) {
4223ac7fe5aSThomas Gleixner 		cnt++;
4233ac7fe5aSThomas Gleixner 		if (obj->object == addr)
4243ac7fe5aSThomas Gleixner 			return obj;
4253ac7fe5aSThomas Gleixner 	}
4263ac7fe5aSThomas Gleixner 	if (cnt > debug_objects_maxchain)
4273ac7fe5aSThomas Gleixner 		debug_objects_maxchain = cnt;
4283ac7fe5aSThomas Gleixner 
4293ac7fe5aSThomas Gleixner 	return NULL;
4303ac7fe5aSThomas Gleixner }
4313ac7fe5aSThomas Gleixner 
calc_usage(void)432*ff8d523cSThomas Gleixner static void calc_usage(void)
433*ff8d523cSThomas Gleixner {
434*ff8d523cSThomas Gleixner 	static DEFINE_RAW_SPINLOCK(avg_lock);
435*ff8d523cSThomas Gleixner 	static unsigned long avg_period;
436*ff8d523cSThomas Gleixner 	unsigned long cur, now = jiffies;
437*ff8d523cSThomas Gleixner 
438*ff8d523cSThomas Gleixner 	if (!time_after_eq(now, READ_ONCE(avg_period)))
439*ff8d523cSThomas Gleixner 		return;
440*ff8d523cSThomas Gleixner 
441*ff8d523cSThomas Gleixner 	if (!raw_spin_trylock(&avg_lock))
442*ff8d523cSThomas Gleixner 		return;
443*ff8d523cSThomas Gleixner 
444*ff8d523cSThomas Gleixner 	WRITE_ONCE(avg_period, now + msecs_to_jiffies(10));
445*ff8d523cSThomas Gleixner 	cur = READ_ONCE(pool_global.stats.cur_used) * ODEBUG_FREE_WORK_MAX;
446*ff8d523cSThomas Gleixner 	WRITE_ONCE(avg_usage, calc_load(avg_usage, EXP_5, cur));
447*ff8d523cSThomas Gleixner 	raw_spin_unlock(&avg_lock);
448*ff8d523cSThomas Gleixner }
449*ff8d523cSThomas Gleixner 
alloc_object(void * addr,struct debug_bucket * b,const struct debug_obj_descr * descr)450fb60c004SThomas Gleixner static struct debug_obj *alloc_object(void *addr, struct debug_bucket *b,
451fb60c004SThomas Gleixner 				      const struct debug_obj_descr *descr)
452d86998b1SWaiman Long {
453d86998b1SWaiman Long 	struct debug_obj *obj;
454d86998b1SWaiman Long 
455*ff8d523cSThomas Gleixner 	calc_usage();
456*ff8d523cSThomas Gleixner 
45714077b9eSThomas Gleixner 	if (static_branch_likely(&obj_cache_enabled))
458fb60c004SThomas Gleixner 		obj = pcpu_alloc();
459fb60c004SThomas Gleixner 	else
460cb58d190SThomas Gleixner 		obj = __alloc_object(&pool_boot);
4613ac7fe5aSThomas Gleixner 
462fb60c004SThomas Gleixner 	if (likely(obj)) {
463d86998b1SWaiman Long 		obj->object = addr;
464d86998b1SWaiman Long 		obj->descr  = descr;
465d86998b1SWaiman Long 		obj->state  = ODEBUG_STATE_NONE;
466d86998b1SWaiman Long 		obj->astate = 0;
467d86998b1SWaiman Long 		hlist_add_head(&obj->node, &b->list);
468d86998b1SWaiman Long 	}
4693ac7fe5aSThomas Gleixner 	return obj;
4703ac7fe5aSThomas Gleixner }
4713ac7fe5aSThomas Gleixner 
4729ce99c6dSThomas Gleixner /* workqueue function to free objects. */
free_obj_work(struct work_struct * work)473337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work)
474337fff8bSThomas Gleixner {
475*ff8d523cSThomas Gleixner 	static unsigned long last_use_avg;
476*ff8d523cSThomas Gleixner 	unsigned long cur_used, last_used, delta;
477*ff8d523cSThomas Gleixner 	unsigned int max_free = 0;
478337fff8bSThomas Gleixner 
479a7344a68SWaiman Long 	WRITE_ONCE(obj_freeing, false);
4809ce99c6dSThomas Gleixner 
481*ff8d523cSThomas Gleixner 	/* Rate limit freeing based on current use average */
482*ff8d523cSThomas Gleixner 	cur_used = READ_ONCE(avg_usage);
483*ff8d523cSThomas Gleixner 	last_used = last_use_avg;
484*ff8d523cSThomas Gleixner 	last_use_avg = cur_used;
485*ff8d523cSThomas Gleixner 
4869ce99c6dSThomas Gleixner 	if (!pool_count(&pool_to_free))
487858274b6SWaiman Long 		return;
48836c4ead6SYang Shi 
489*ff8d523cSThomas Gleixner 	if (cur_used <= last_used) {
490*ff8d523cSThomas Gleixner 		delta = (last_used - cur_used) / ODEBUG_FREE_WORK_MAX;
491*ff8d523cSThomas Gleixner 		max_free = min(delta, ODEBUG_FREE_WORK_MAX);
492*ff8d523cSThomas Gleixner 	}
493*ff8d523cSThomas Gleixner 
494*ff8d523cSThomas Gleixner 	for (int cnt = 0; cnt < ODEBUG_FREE_WORK_MAX; cnt++) {
4959ce99c6dSThomas Gleixner 		HLIST_HEAD(tofree);
496a7344a68SWaiman Long 
4979ce99c6dSThomas Gleixner 		/* Acquire and drop the lock for each batch */
4989ce99c6dSThomas Gleixner 		scoped_guard(raw_spinlock_irqsave, &pool_lock) {
4999ce99c6dSThomas Gleixner 			if (!pool_to_free.cnt)
500a7344a68SWaiman Long 				return;
50136c4ead6SYang Shi 
5029ce99c6dSThomas Gleixner 			/* Refill the global pool if possible */
5039ce99c6dSThomas Gleixner 			if (pool_move_batch(&pool_global, &pool_to_free)) {
5049ce99c6dSThomas Gleixner 				/* Don't free as there seems to be demand */
505*ff8d523cSThomas Gleixner 				max_free = 0;
506*ff8d523cSThomas Gleixner 			} else if (max_free) {
5079ce99c6dSThomas Gleixner 				pool_pop_batch(&tofree, &pool_to_free);
508*ff8d523cSThomas Gleixner 				max_free--;
5099ce99c6dSThomas Gleixner 			} else {
5109ce99c6dSThomas Gleixner 				return;
51136c4ead6SYang Shi 			}
5129ce99c6dSThomas Gleixner 		}
51349a5cb82SThomas Gleixner 		free_object_list(&tofree);
514337fff8bSThomas Gleixner 	}
5159ce99c6dSThomas Gleixner }
516337fff8bSThomas Gleixner 
__free_object(struct debug_obj * obj)517a7344a68SWaiman Long static void __free_object(struct debug_obj *obj)
518636e1970SYang Shi {
519cb58d190SThomas Gleixner 	guard(irqsave)();
52014077b9eSThomas Gleixner 	if (static_branch_likely(&obj_cache_enabled))
521a3b9e191SThomas Gleixner 		pcpu_free(obj);
522a3b9e191SThomas Gleixner 	else
523cb58d190SThomas Gleixner 		hlist_add_head(&obj->node, &pool_boot);
524636e1970SYang Shi }
525636e1970SYang Shi 
526337fff8bSThomas Gleixner /*
527337fff8bSThomas Gleixner  * Put the object back into the pool and schedule work to free objects
528337fff8bSThomas Gleixner  * if necessary.
5293ac7fe5aSThomas Gleixner  */
free_object(struct debug_obj * obj)5303ac7fe5aSThomas Gleixner static void free_object(struct debug_obj *obj)
5313ac7fe5aSThomas Gleixner {
532a7344a68SWaiman Long 	__free_object(obj);
533e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
534a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
535a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
536a7344a68SWaiman Long 	}
5373ac7fe5aSThomas Gleixner }
5383ac7fe5aSThomas Gleixner 
put_objects(struct hlist_head * list)539a2a70238SThomas Gleixner static void put_objects(struct hlist_head *list)
54088451f2cSZqiang {
54188451f2cSZqiang 	struct hlist_node *tmp;
54288451f2cSZqiang 	struct debug_obj *obj;
54388451f2cSZqiang 
544a2a70238SThomas Gleixner 	/*
545a2a70238SThomas Gleixner 	 * Using free_object() puts the objects into reuse or schedules
546a2a70238SThomas Gleixner 	 * them for freeing and it get's all the accounting correct.
547a2a70238SThomas Gleixner 	 */
548a2a70238SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, list, node) {
54988451f2cSZqiang 		hlist_del(&obj->node);
550a2a70238SThomas Gleixner 		free_object(obj);
551a2a70238SThomas Gleixner 	}
55288451f2cSZqiang }
553eabb7f1aSwuchi 
55449968cf1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
object_cpu_offline(unsigned int cpu)555a2a70238SThomas Gleixner static int object_cpu_offline(unsigned int cpu)
556a2a70238SThomas Gleixner {
557a2a70238SThomas Gleixner 	/* Remote access is safe as the CPU is dead already */
55818b8afcbSThomas Gleixner 	struct obj_pool *pcp = per_cpu_ptr(&pool_pcpu, cpu);
559eabb7f1aSwuchi 
56018b8afcbSThomas Gleixner 	put_objects(&pcp->objects);
56118b8afcbSThomas Gleixner 	pcp->cnt = 0;
56288451f2cSZqiang 	return 0;
56388451f2cSZqiang }
56488451f2cSZqiang #endif
56588451f2cSZqiang 
56649968cf1SThomas Gleixner /* Out of memory. Free all objects from hash */
debug_objects_oom(void)5673ac7fe5aSThomas Gleixner static void debug_objects_oom(void)
5683ac7fe5aSThomas Gleixner {
5693ac7fe5aSThomas Gleixner 	struct debug_bucket *db = obj_hash;
570673d62ccSVegard Nossum 	HLIST_HEAD(freelist);
5713ac7fe5aSThomas Gleixner 
572719e4843SFabian Frederick 	pr_warn("Out of memory. ODEBUG disabled\n");
5733ac7fe5aSThomas Gleixner 
57449968cf1SThomas Gleixner 	for (int i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
57549968cf1SThomas Gleixner 		scoped_guard(raw_spinlock_irqsave, &db->lock)
576673d62ccSVegard Nossum 			hlist_move_list(&db->list, &freelist);
577673d62ccSVegard Nossum 
57849968cf1SThomas Gleixner 		put_objects(&freelist);
5793ac7fe5aSThomas Gleixner 	}
5803ac7fe5aSThomas Gleixner }
5813ac7fe5aSThomas Gleixner 
5823ac7fe5aSThomas Gleixner /*
5833ac7fe5aSThomas Gleixner  * We use the pfn of the address for the hash. That way we can check
5843ac7fe5aSThomas Gleixner  * for freed objects simply by checking the affected bucket.
5853ac7fe5aSThomas Gleixner  */
get_bucket(unsigned long addr)5863ac7fe5aSThomas Gleixner static struct debug_bucket *get_bucket(unsigned long addr)
5873ac7fe5aSThomas Gleixner {
5883ac7fe5aSThomas Gleixner 	unsigned long hash;
5893ac7fe5aSThomas Gleixner 
5903ac7fe5aSThomas Gleixner 	hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
5913ac7fe5aSThomas Gleixner 	return &obj_hash[hash];
5923ac7fe5aSThomas Gleixner }
5933ac7fe5aSThomas Gleixner 
debug_print_object(struct debug_obj * obj,char * msg)5943ac7fe5aSThomas Gleixner static void debug_print_object(struct debug_obj *obj, char *msg)
5953ac7fe5aSThomas Gleixner {
596aedcade6SStephen Boyd 	const struct debug_obj_descr *descr = obj->descr;
5973ac7fe5aSThomas Gleixner 	static int limit;
5983ac7fe5aSThomas Gleixner 
5998b64d420STetsuo Handa 	/*
6008b64d420STetsuo Handa 	 * Don't report if lookup_object_or_alloc() by the current thread
6018b64d420STetsuo Handa 	 * failed because lookup_object_or_alloc()/debug_objects_oom() by a
6028b64d420STetsuo Handa 	 * concurrent thread turned off debug_objects_enabled and cleared
6038b64d420STetsuo Handa 	 * the hash buckets.
6048b64d420STetsuo Handa 	 */
6058b64d420STetsuo Handa 	if (!debug_objects_enabled)
6068b64d420STetsuo Handa 		return;
6078b64d420STetsuo Handa 
60899777288SStanislaw Gruszka 	if (limit < 5 && descr != descr_test) {
60999777288SStanislaw Gruszka 		void *hint = descr->debug_hint ?
61099777288SStanislaw Gruszka 			descr->debug_hint(obj->object) : NULL;
6113ac7fe5aSThomas Gleixner 		limit++;
612a5d8e467SMathieu Desnoyers 		WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
613c4db2d3bSStephen Boyd 				 "object: %p object type: %s hint: %pS\n",
614a5d8e467SMathieu Desnoyers 			msg, obj_states[obj->state], obj->astate,
615c4db2d3bSStephen Boyd 			obj->object, descr->name, hint);
6163ac7fe5aSThomas Gleixner 	}
6173ac7fe5aSThomas Gleixner 	debug_objects_warnings++;
6183ac7fe5aSThomas Gleixner }
6193ac7fe5aSThomas Gleixner 
6203ac7fe5aSThomas Gleixner /*
6213ac7fe5aSThomas Gleixner  * Try to repair the damage, so we have a better chance to get useful
6223ac7fe5aSThomas Gleixner  * debug output.
6233ac7fe5aSThomas Gleixner  */
624b1e4d9d8SDu, Changbin static bool
debug_object_fixup(bool (* fixup)(void * addr,enum debug_obj_state state),void * addr,enum debug_obj_state state)625b1e4d9d8SDu, Changbin debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
6263ac7fe5aSThomas Gleixner 		   void * addr, enum debug_obj_state state)
6273ac7fe5aSThomas Gleixner {
628b1e4d9d8SDu, Changbin 	if (fixup && fixup(addr, state)) {
629b1e4d9d8SDu, Changbin 		debug_objects_fixups++;
630b1e4d9d8SDu, Changbin 		return true;
631b1e4d9d8SDu, Changbin 	}
632b1e4d9d8SDu, Changbin 	return false;
6333ac7fe5aSThomas Gleixner }
6343ac7fe5aSThomas Gleixner 
debug_object_is_on_stack(void * addr,int onstack)6353ac7fe5aSThomas Gleixner static void debug_object_is_on_stack(void *addr, int onstack)
6363ac7fe5aSThomas Gleixner {
6373ac7fe5aSThomas Gleixner 	int is_on_stack;
6383ac7fe5aSThomas Gleixner 	static int limit;
6393ac7fe5aSThomas Gleixner 
6403ac7fe5aSThomas Gleixner 	if (limit > 4)
6413ac7fe5aSThomas Gleixner 		return;
6423ac7fe5aSThomas Gleixner 
6438b05c7e6SFUJITA Tomonori 	is_on_stack = object_is_on_stack(addr);
6443ac7fe5aSThomas Gleixner 	if (is_on_stack == onstack)
6453ac7fe5aSThomas Gleixner 		return;
6463ac7fe5aSThomas Gleixner 
6473ac7fe5aSThomas Gleixner 	limit++;
6483ac7fe5aSThomas Gleixner 	if (is_on_stack)
649fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
650fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
6513ac7fe5aSThomas Gleixner 	else
652fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
653fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
654fc91a3c4SJoel Fernandes (Google) 
6553ac7fe5aSThomas Gleixner 	WARN_ON(1);
6563ac7fe5aSThomas Gleixner }
6573ac7fe5aSThomas Gleixner 
lookup_object_or_alloc(void * addr,struct debug_bucket * b,const struct debug_obj_descr * descr,bool onstack,bool alloc_ifstatic)65863a75969SThomas Gleixner static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
65963a75969SThomas Gleixner 						const struct debug_obj_descr *descr,
66063a75969SThomas Gleixner 						bool onstack, bool alloc_ifstatic)
66163a75969SThomas Gleixner {
66263a75969SThomas Gleixner 	struct debug_obj *obj = lookup_object(addr, b);
66363a75969SThomas Gleixner 	enum debug_obj_state state = ODEBUG_STATE_NONE;
66463a75969SThomas Gleixner 
66563a75969SThomas Gleixner 	if (likely(obj))
66663a75969SThomas Gleixner 		return obj;
66763a75969SThomas Gleixner 
66863a75969SThomas Gleixner 	/*
66963a75969SThomas Gleixner 	 * debug_object_init() unconditionally allocates untracked
67063a75969SThomas Gleixner 	 * objects. It does not matter whether it is a static object or
67163a75969SThomas Gleixner 	 * not.
67263a75969SThomas Gleixner 	 *
67363a75969SThomas Gleixner 	 * debug_object_assert_init() and debug_object_activate() allow
67463a75969SThomas Gleixner 	 * allocation only if the descriptor callback confirms that the
67563a75969SThomas Gleixner 	 * object is static and considered initialized. For non-static
67663a75969SThomas Gleixner 	 * objects the allocation needs to be done from the fixup callback.
67763a75969SThomas Gleixner 	 */
67863a75969SThomas Gleixner 	if (unlikely(alloc_ifstatic)) {
67963a75969SThomas Gleixner 		if (!descr->is_static_object || !descr->is_static_object(addr))
68063a75969SThomas Gleixner 			return ERR_PTR(-ENOENT);
68163a75969SThomas Gleixner 		/* Statically allocated objects are considered initialized */
68263a75969SThomas Gleixner 		state = ODEBUG_STATE_INIT;
68363a75969SThomas Gleixner 	}
68463a75969SThomas Gleixner 
68563a75969SThomas Gleixner 	obj = alloc_object(addr, b, descr);
68663a75969SThomas Gleixner 	if (likely(obj)) {
68763a75969SThomas Gleixner 		obj->state = state;
68863a75969SThomas Gleixner 		debug_object_is_on_stack(addr, onstack);
68963a75969SThomas Gleixner 		return obj;
69063a75969SThomas Gleixner 	}
69163a75969SThomas Gleixner 
69263a75969SThomas Gleixner 	/* Out of memory. Do the cleanup outside of the locked region */
693661cc28bSThomas Gleixner 	debug_objects_enabled = false;
69463a75969SThomas Gleixner 	return NULL;
69563a75969SThomas Gleixner }
69663a75969SThomas Gleixner 
debug_objects_fill_pool(void)6970af462f1SThomas Gleixner static void debug_objects_fill_pool(void)
6980af462f1SThomas Gleixner {
69914077b9eSThomas Gleixner 	if (!static_branch_likely(&obj_cache_enabled))
700d8c6cd3aSZhen Lei 		return;
701d8c6cd3aSZhen Lei 
70296a9a042SThomas Gleixner 	if (likely(!pool_should_refill(&pool_global)))
703d8c6cd3aSZhen Lei 		return;
704d8c6cd3aSZhen Lei 
705d8c6cd3aSZhen Lei 	/* Try reusing objects from obj_to_free_list */
706d8c6cd3aSZhen Lei 	fill_pool_from_freelist();
707d8c6cd3aSZhen Lei 
70896a9a042SThomas Gleixner 	if (likely(!pool_should_refill(&pool_global)))
709d8c6cd3aSZhen Lei 		return;
710d8c6cd3aSZhen Lei 
7110af462f1SThomas Gleixner 	/*
7120af462f1SThomas Gleixner 	 * On RT enabled kernels the pool refill must happen in preemptible
7130cce06baSPeter Zijlstra 	 * context -- for !RT kernels we rely on the fact that spinlock_t and
7140cce06baSPeter Zijlstra 	 * raw_spinlock_t are basically the same type and this lock-type
7150cce06baSPeter Zijlstra 	 * inversion works just fine.
7160af462f1SThomas Gleixner 	 */
7170cce06baSPeter Zijlstra 	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) {
7180cce06baSPeter Zijlstra 		/*
7190cce06baSPeter Zijlstra 		 * Annotate away the spinlock_t inside raw_spinlock_t warning
7200cce06baSPeter Zijlstra 		 * by temporarily raising the wait-type to WAIT_SLEEP, matching
7210cce06baSPeter Zijlstra 		 * the preemptible() condition above.
7220cce06baSPeter Zijlstra 		 */
7230cce06baSPeter Zijlstra 		static DEFINE_WAIT_OVERRIDE_MAP(fill_pool_map, LD_WAIT_SLEEP);
7240cce06baSPeter Zijlstra 		lock_map_acquire_try(&fill_pool_map);
7250af462f1SThomas Gleixner 		fill_pool();
7260cce06baSPeter Zijlstra 		lock_map_release(&fill_pool_map);
7270cce06baSPeter Zijlstra 	}
7280af462f1SThomas Gleixner }
7290af462f1SThomas Gleixner 
7303ac7fe5aSThomas Gleixner static void
__debug_object_init(void * addr,const struct debug_obj_descr * descr,int onstack)731aedcade6SStephen Boyd __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
7323ac7fe5aSThomas Gleixner {
7339bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
7343ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
7353ac7fe5aSThomas Gleixner 	unsigned long flags;
7363ac7fe5aSThomas Gleixner 
7370af462f1SThomas Gleixner 	debug_objects_fill_pool();
73850db04ddSVegard Nossum 
7393ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
7403ac7fe5aSThomas Gleixner 
741aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
7423ac7fe5aSThomas Gleixner 
74363a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
74463a75969SThomas Gleixner 	if (unlikely(!obj)) {
745aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
7463ac7fe5aSThomas Gleixner 		debug_objects_oom();
7473ac7fe5aSThomas Gleixner 		return;
7483ac7fe5aSThomas Gleixner 	}
7493ac7fe5aSThomas Gleixner 
7503ac7fe5aSThomas Gleixner 	switch (obj->state) {
7513ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
7523ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
7533ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
7543ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_INIT;
755aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
756d5f34153SWaiman Long 		return;
7573ac7fe5aSThomas Gleixner 	default:
7583ac7fe5aSThomas Gleixner 		break;
7593ac7fe5aSThomas Gleixner 	}
7603ac7fe5aSThomas Gleixner 
7619bb63626SAndrzej Hajda 	o = *obj;
762aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
7639bb63626SAndrzej Hajda 	debug_print_object(&o, "init");
7649bb63626SAndrzej Hajda 
7659bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
7669bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_init, addr, o.state);
7673ac7fe5aSThomas Gleixner }
7683ac7fe5aSThomas Gleixner 
7693ac7fe5aSThomas Gleixner /**
7703ac7fe5aSThomas Gleixner  * debug_object_init - debug checks when an object is initialized
7713ac7fe5aSThomas Gleixner  * @addr:	address of the object
7723ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7733ac7fe5aSThomas Gleixner  */
debug_object_init(void * addr,const struct debug_obj_descr * descr)774aedcade6SStephen Boyd void debug_object_init(void *addr, const struct debug_obj_descr *descr)
7753ac7fe5aSThomas Gleixner {
7763ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7773ac7fe5aSThomas Gleixner 		return;
7783ac7fe5aSThomas Gleixner 
7793ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 0);
7803ac7fe5aSThomas Gleixner }
781f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init);
7823ac7fe5aSThomas Gleixner 
7833ac7fe5aSThomas Gleixner /**
7843ac7fe5aSThomas Gleixner  * debug_object_init_on_stack - debug checks when an object on stack is
7853ac7fe5aSThomas Gleixner  *				initialized
7863ac7fe5aSThomas Gleixner  * @addr:	address of the object
7873ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7883ac7fe5aSThomas Gleixner  */
debug_object_init_on_stack(void * addr,const struct debug_obj_descr * descr)789aedcade6SStephen Boyd void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
7903ac7fe5aSThomas Gleixner {
7913ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7923ac7fe5aSThomas Gleixner 		return;
7933ac7fe5aSThomas Gleixner 
7943ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 1);
7953ac7fe5aSThomas Gleixner }
796f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
7973ac7fe5aSThomas Gleixner 
7983ac7fe5aSThomas Gleixner /**
7993ac7fe5aSThomas Gleixner  * debug_object_activate - debug checks when an object is activated
8003ac7fe5aSThomas Gleixner  * @addr:	address of the object
8013ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
802b778ae25SPaul E. McKenney  * Returns 0 for success, -EINVAL for check failed.
8033ac7fe5aSThomas Gleixner  */
debug_object_activate(void * addr,const struct debug_obj_descr * descr)804aedcade6SStephen Boyd int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
8053ac7fe5aSThomas Gleixner {
80663a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
8073ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8083ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
8093ac7fe5aSThomas Gleixner 	unsigned long flags;
8103ac7fe5aSThomas Gleixner 
8113ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
812b778ae25SPaul E. McKenney 		return 0;
8133ac7fe5aSThomas Gleixner 
8140af462f1SThomas Gleixner 	debug_objects_fill_pool();
8150af462f1SThomas Gleixner 
8163ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8173ac7fe5aSThomas Gleixner 
818aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8193ac7fe5aSThomas Gleixner 
82063a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
8219bb63626SAndrzej Hajda 	if (unlikely(!obj)) {
8229bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
8239bb63626SAndrzej Hajda 		debug_objects_oom();
8249bb63626SAndrzej Hajda 		return 0;
8259bb63626SAndrzej Hajda 	} else if (likely(!IS_ERR(obj))) {
8263ac7fe5aSThomas Gleixner 		switch (obj->state) {
8279bb63626SAndrzej Hajda 		case ODEBUG_STATE_ACTIVE:
8289bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
8299bb63626SAndrzej Hajda 			o = *obj;
8309bb63626SAndrzej Hajda 			break;
8313ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
8323ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
8333ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_ACTIVE;
8349bb63626SAndrzej Hajda 			fallthrough;
8353ac7fe5aSThomas Gleixner 		default:
836aef9cb05SThomas Gleixner 			raw_spin_unlock_irqrestore(&db->lock, flags);
837b778ae25SPaul E. McKenney 			return 0;
8383ac7fe5aSThomas Gleixner 		}
8399bb63626SAndrzej Hajda 	}
84063a75969SThomas Gleixner 
8419bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
84263a75969SThomas Gleixner 	debug_print_object(&o, "activate");
8439bb63626SAndrzej Hajda 
8449bb63626SAndrzej Hajda 	switch (o.state) {
8459bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
8469bb63626SAndrzej Hajda 	case ODEBUG_STATE_NOTAVAILABLE:
8479bb63626SAndrzej Hajda 		if (debug_object_fixup(descr->fixup_activate, addr, o.state))
8489bb63626SAndrzej Hajda 			return 0;
8499bb63626SAndrzej Hajda 		fallthrough;
8509bb63626SAndrzej Hajda 	default:
8519bb63626SAndrzej Hajda 		return -EINVAL;
8529bb63626SAndrzej Hajda 	}
85363a75969SThomas Gleixner }
854f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_activate);
8553ac7fe5aSThomas Gleixner 
8563ac7fe5aSThomas Gleixner /**
8573ac7fe5aSThomas Gleixner  * debug_object_deactivate - debug checks when an object is deactivated
8583ac7fe5aSThomas Gleixner  * @addr:	address of the object
8593ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8603ac7fe5aSThomas Gleixner  */
debug_object_deactivate(void * addr,const struct debug_obj_descr * descr)861aedcade6SStephen Boyd void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr)
8623ac7fe5aSThomas Gleixner {
8639bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
8643ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8653ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
8663ac7fe5aSThomas Gleixner 	unsigned long flags;
8673ac7fe5aSThomas Gleixner 
8683ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8693ac7fe5aSThomas Gleixner 		return;
8703ac7fe5aSThomas Gleixner 
8713ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8723ac7fe5aSThomas Gleixner 
873aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8743ac7fe5aSThomas Gleixner 
8753ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8763ac7fe5aSThomas Gleixner 	if (obj) {
8773ac7fe5aSThomas Gleixner 		switch (obj->state) {
8789bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
8799bb63626SAndrzej Hajda 			break;
8803ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
8813ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
8823ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_ACTIVE:
8839bb63626SAndrzej Hajda 			if (obj->astate)
8849bb63626SAndrzej Hajda 				break;
8853ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_INACTIVE;
8869bb63626SAndrzej Hajda 			fallthrough;
8873ac7fe5aSThomas Gleixner 		default:
8889bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
8899bb63626SAndrzej Hajda 			return;
8903ac7fe5aSThomas Gleixner 		}
8919bb63626SAndrzej Hajda 		o = *obj;
892d5f34153SWaiman Long 	}
893d5f34153SWaiman Long 
894d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
8953ac7fe5aSThomas Gleixner 	debug_print_object(&o, "deactivate");
8963ac7fe5aSThomas Gleixner }
897f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_deactivate);
8983ac7fe5aSThomas Gleixner 
8993ac7fe5aSThomas Gleixner /**
9003ac7fe5aSThomas Gleixner  * debug_object_destroy - debug checks when an object is destroyed
9013ac7fe5aSThomas Gleixner  * @addr:	address of the object
9023ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
9033ac7fe5aSThomas Gleixner  */
debug_object_destroy(void * addr,const struct debug_obj_descr * descr)904aedcade6SStephen Boyd void debug_object_destroy(void *addr, const struct debug_obj_descr *descr)
9053ac7fe5aSThomas Gleixner {
9069bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
9073ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
9083ac7fe5aSThomas Gleixner 	unsigned long flags;
9093ac7fe5aSThomas Gleixner 
9103ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
9113ac7fe5aSThomas Gleixner 		return;
9123ac7fe5aSThomas Gleixner 
9133ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
9143ac7fe5aSThomas Gleixner 
915aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
9163ac7fe5aSThomas Gleixner 
9173ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
9189bb63626SAndrzej Hajda 	if (!obj) {
9199bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
9209bb63626SAndrzej Hajda 		return;
9219bb63626SAndrzej Hajda 	}
9223ac7fe5aSThomas Gleixner 
9233ac7fe5aSThomas Gleixner 	switch (obj->state) {
9249bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
9259bb63626SAndrzej Hajda 	case ODEBUG_STATE_DESTROYED:
9269bb63626SAndrzej Hajda 		break;
9273ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
9283ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
9293ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
9303ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_DESTROYED;
9319bb63626SAndrzej Hajda 		fallthrough;
9323ac7fe5aSThomas Gleixner 	default:
933aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
9349bb63626SAndrzej Hajda 		return;
9359bb63626SAndrzej Hajda 	}
9369bb63626SAndrzej Hajda 
9379bb63626SAndrzej Hajda 	o = *obj;
9389bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
9399bb63626SAndrzej Hajda 	debug_print_object(&o, "destroy");
9409bb63626SAndrzej Hajda 
9419bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
9429bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_destroy, addr, o.state);
9433ac7fe5aSThomas Gleixner }
944f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_destroy);
9453ac7fe5aSThomas Gleixner 
9463ac7fe5aSThomas Gleixner /**
9473ac7fe5aSThomas Gleixner  * debug_object_free - debug checks when an object is freed
9483ac7fe5aSThomas Gleixner  * @addr:	address of the object
9493ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
9503ac7fe5aSThomas Gleixner  */
debug_object_free(void * addr,const struct debug_obj_descr * descr)951aedcade6SStephen Boyd void debug_object_free(void *addr, const struct debug_obj_descr *descr)
9523ac7fe5aSThomas Gleixner {
9539bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
9543ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
9553ac7fe5aSThomas Gleixner 	unsigned long flags;
9563ac7fe5aSThomas Gleixner 
9573ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
9583ac7fe5aSThomas Gleixner 		return;
9593ac7fe5aSThomas Gleixner 
9603ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
9613ac7fe5aSThomas Gleixner 
962aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
9633ac7fe5aSThomas Gleixner 
9643ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
9659bb63626SAndrzej Hajda 	if (!obj) {
9669bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
9679bb63626SAndrzej Hajda 		return;
9689bb63626SAndrzej Hajda 	}
9693ac7fe5aSThomas Gleixner 
9703ac7fe5aSThomas Gleixner 	switch (obj->state) {
9713ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
9729bb63626SAndrzej Hajda 		break;
9733ac7fe5aSThomas Gleixner 	default:
9743ac7fe5aSThomas Gleixner 		hlist_del(&obj->node);
975aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
9763ac7fe5aSThomas Gleixner 		free_object(obj);
977673d62ccSVegard Nossum 		return;
9783ac7fe5aSThomas Gleixner 	}
9799bb63626SAndrzej Hajda 
9809bb63626SAndrzej Hajda 	o = *obj;
981aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
9829bb63626SAndrzej Hajda 	debug_print_object(&o, "free");
9839bb63626SAndrzej Hajda 
9849bb63626SAndrzej Hajda 	debug_object_fixup(descr->fixup_free, addr, o.state);
9853ac7fe5aSThomas Gleixner }
986f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_free);
9873ac7fe5aSThomas Gleixner 
988a5d8e467SMathieu Desnoyers /**
989b84d435cSChristine Chan  * debug_object_assert_init - debug checks when object should be init-ed
990b84d435cSChristine Chan  * @addr:	address of the object
991b84d435cSChristine Chan  * @descr:	pointer to an object specific debug description structure
992b84d435cSChristine Chan  */
debug_object_assert_init(void * addr,const struct debug_obj_descr * descr)993aedcade6SStephen Boyd void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
994b84d435cSChristine Chan {
99563a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
996b84d435cSChristine Chan 	struct debug_bucket *db;
997b84d435cSChristine Chan 	struct debug_obj *obj;
998b84d435cSChristine Chan 	unsigned long flags;
999b84d435cSChristine Chan 
1000b84d435cSChristine Chan 	if (!debug_objects_enabled)
1001b84d435cSChristine Chan 		return;
1002b84d435cSChristine Chan 
10030af462f1SThomas Gleixner 	debug_objects_fill_pool();
10040af462f1SThomas Gleixner 
1005b84d435cSChristine Chan 	db = get_bucket((unsigned long) addr);
1006b84d435cSChristine Chan 
1007b84d435cSChristine Chan 	raw_spin_lock_irqsave(&db->lock, flags);
100863a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
1009b84d435cSChristine Chan 	raw_spin_unlock_irqrestore(&db->lock, flags);
101063a75969SThomas Gleixner 	if (likely(!IS_ERR_OR_NULL(obj)))
101163a75969SThomas Gleixner 		return;
101263a75969SThomas Gleixner 
101363a75969SThomas Gleixner 	/* If NULL the allocation has hit OOM */
101463a75969SThomas Gleixner 	if (!obj) {
101563a75969SThomas Gleixner 		debug_objects_oom();
1016b84d435cSChristine Chan 		return;
1017b84d435cSChristine Chan 	}
1018b84d435cSChristine Chan 
101963a75969SThomas Gleixner 	/* Object is neither tracked nor static. It's not initialized. */
102063a75969SThomas Gleixner 	debug_print_object(&o, "assert_init");
102163a75969SThomas Gleixner 	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
1022b84d435cSChristine Chan }
1023f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_assert_init);
1024b84d435cSChristine Chan 
1025b84d435cSChristine Chan /**
1026a5d8e467SMathieu Desnoyers  * debug_object_active_state - debug checks object usage state machine
1027a5d8e467SMathieu Desnoyers  * @addr:	address of the object
1028a5d8e467SMathieu Desnoyers  * @descr:	pointer to an object specific debug description structure
1029a5d8e467SMathieu Desnoyers  * @expect:	expected state
1030a5d8e467SMathieu Desnoyers  * @next:	state to move to if expected state is found
1031a5d8e467SMathieu Desnoyers  */
1032a5d8e467SMathieu Desnoyers void
debug_object_active_state(void * addr,const struct debug_obj_descr * descr,unsigned int expect,unsigned int next)1033aedcade6SStephen Boyd debug_object_active_state(void *addr, const struct debug_obj_descr *descr,
1034a5d8e467SMathieu Desnoyers 			  unsigned int expect, unsigned int next)
1035a5d8e467SMathieu Desnoyers {
10369bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
1037a5d8e467SMathieu Desnoyers 	struct debug_bucket *db;
1038a5d8e467SMathieu Desnoyers 	struct debug_obj *obj;
1039a5d8e467SMathieu Desnoyers 	unsigned long flags;
1040a5d8e467SMathieu Desnoyers 
1041a5d8e467SMathieu Desnoyers 	if (!debug_objects_enabled)
1042a5d8e467SMathieu Desnoyers 		return;
1043a5d8e467SMathieu Desnoyers 
1044a5d8e467SMathieu Desnoyers 	db = get_bucket((unsigned long) addr);
1045a5d8e467SMathieu Desnoyers 
1046a5d8e467SMathieu Desnoyers 	raw_spin_lock_irqsave(&db->lock, flags);
1047a5d8e467SMathieu Desnoyers 
1048a5d8e467SMathieu Desnoyers 	obj = lookup_object(addr, db);
1049a5d8e467SMathieu Desnoyers 	if (obj) {
1050a5d8e467SMathieu Desnoyers 		switch (obj->state) {
1051a5d8e467SMathieu Desnoyers 		case ODEBUG_STATE_ACTIVE:
10529bb63626SAndrzej Hajda 			if (obj->astate != expect)
1053a5d8e467SMathieu Desnoyers 				break;
10549bb63626SAndrzej Hajda 			obj->astate = next;
10559bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
10569bb63626SAndrzej Hajda 			return;
1057a5d8e467SMathieu Desnoyers 		default:
1058a5d8e467SMathieu Desnoyers 			break;
1059a5d8e467SMathieu Desnoyers 		}
10609bb63626SAndrzej Hajda 		o = *obj;
1061d5f34153SWaiman Long 	}
1062d5f34153SWaiman Long 
1063d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
1064a5d8e467SMathieu Desnoyers 	debug_print_object(&o, "active_state");
1065a5d8e467SMathieu Desnoyers }
1066f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_active_state);
1067a5d8e467SMathieu Desnoyers 
10683ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
__debug_check_no_obj_freed(const void * address,unsigned long size)10693ac7fe5aSThomas Gleixner static void __debug_check_no_obj_freed(const void *address, unsigned long size)
10703ac7fe5aSThomas Gleixner {
10713ac7fe5aSThomas Gleixner 	unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
10729bb63626SAndrzej Hajda 	int cnt, objs_checked = 0;
10739bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
10743ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
10751ea9b98bSYang Shi 	struct hlist_node *tmp;
10763ac7fe5aSThomas Gleixner 
10773ac7fe5aSThomas Gleixner 	saddr = (unsigned long) address;
10783ac7fe5aSThomas Gleixner 	eaddr = saddr + size;
10793ac7fe5aSThomas Gleixner 	paddr = saddr & ODEBUG_CHUNK_MASK;
10803ac7fe5aSThomas Gleixner 	chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
10813ac7fe5aSThomas Gleixner 	chunks >>= ODEBUG_CHUNK_SHIFT;
10823ac7fe5aSThomas Gleixner 
10833ac7fe5aSThomas Gleixner 	for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
10843ac7fe5aSThomas Gleixner 		db = get_bucket(paddr);
10853ac7fe5aSThomas Gleixner 
10863ac7fe5aSThomas Gleixner repeat:
10873ac7fe5aSThomas Gleixner 		cnt = 0;
1088aef9cb05SThomas Gleixner 		raw_spin_lock_irqsave(&db->lock, flags);
1089b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
10903ac7fe5aSThomas Gleixner 			cnt++;
10913ac7fe5aSThomas Gleixner 			oaddr = (unsigned long) obj->object;
10923ac7fe5aSThomas Gleixner 			if (oaddr < saddr || oaddr >= eaddr)
10933ac7fe5aSThomas Gleixner 				continue;
10943ac7fe5aSThomas Gleixner 
10953ac7fe5aSThomas Gleixner 			switch (obj->state) {
10963ac7fe5aSThomas Gleixner 			case ODEBUG_STATE_ACTIVE:
10979bb63626SAndrzej Hajda 				o = *obj;
1098aef9cb05SThomas Gleixner 				raw_spin_unlock_irqrestore(&db->lock, flags);
10999bb63626SAndrzej Hajda 				debug_print_object(&o, "free");
11009bb63626SAndrzej Hajda 				debug_object_fixup(o.descr->fixup_free, (void *)oaddr, o.state);
11013ac7fe5aSThomas Gleixner 				goto repeat;
11023ac7fe5aSThomas Gleixner 			default:
11033ac7fe5aSThomas Gleixner 				hlist_del(&obj->node);
1104a7344a68SWaiman Long 				__free_object(obj);
11053ac7fe5aSThomas Gleixner 				break;
11063ac7fe5aSThomas Gleixner 			}
11073ac7fe5aSThomas Gleixner 		}
1108aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
1109673d62ccSVegard Nossum 
11103ac7fe5aSThomas Gleixner 		if (cnt > debug_objects_maxchain)
11113ac7fe5aSThomas Gleixner 			debug_objects_maxchain = cnt;
1112bd9dcd04SYang Shi 
1113bd9dcd04SYang Shi 		objs_checked += cnt;
11143ac7fe5aSThomas Gleixner 	}
1115bd9dcd04SYang Shi 
1116bd9dcd04SYang Shi 	if (objs_checked > debug_objects_maxchecked)
1117bd9dcd04SYang Shi 		debug_objects_maxchecked = objs_checked;
11181ea9b98bSYang Shi 
11191ea9b98bSYang Shi 	/* Schedule work to actually kmem_cache_free() objects */
1120e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
1121a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
1122a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
1123a7344a68SWaiman Long 	}
11243ac7fe5aSThomas Gleixner }
11253ac7fe5aSThomas Gleixner 
debug_check_no_obj_freed(const void * address,unsigned long size)11263ac7fe5aSThomas Gleixner void debug_check_no_obj_freed(const void *address, unsigned long size)
11273ac7fe5aSThomas Gleixner {
11283ac7fe5aSThomas Gleixner 	if (debug_objects_enabled)
11293ac7fe5aSThomas Gleixner 		__debug_check_no_obj_freed(address, size);
11303ac7fe5aSThomas Gleixner }
11313ac7fe5aSThomas Gleixner #endif
11323ac7fe5aSThomas Gleixner 
11333ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_FS
11343ac7fe5aSThomas Gleixner 
debug_stats_show(struct seq_file * m,void * v)11353ac7fe5aSThomas Gleixner static int debug_stats_show(struct seq_file *m, void *v)
11363ac7fe5aSThomas Gleixner {
11372638345dSThomas Gleixner 	unsigned int cpu, pool_used, pcp_free = 0;
1138d86998b1SWaiman Long 
11392638345dSThomas Gleixner 	/*
11402638345dSThomas Gleixner 	 * pool_global.stats.cur_used is the number of batches currently
11412638345dSThomas Gleixner 	 * handed out to per CPU pools. Convert it to number of objects
11422638345dSThomas Gleixner 	 * and subtract the number of free objects in the per CPU pools.
11432638345dSThomas Gleixner 	 * As this is lockless the number is an estimate.
11442638345dSThomas Gleixner 	 */
1145d86998b1SWaiman Long 	for_each_possible_cpu(cpu)
11462638345dSThomas Gleixner 		pcp_free += per_cpu(pool_pcpu.cnt, cpu);
11472638345dSThomas Gleixner 
1148*ff8d523cSThomas Gleixner 	pool_used = READ_ONCE(pool_global.stats.cur_used);
11492638345dSThomas Gleixner 	pcp_free = min(pool_used, pcp_free);
11502638345dSThomas Gleixner 	pool_used -= pcp_free;
1151d86998b1SWaiman Long 
11523ac7fe5aSThomas Gleixner 	seq_printf(m, "max_chain     : %d\n", debug_objects_maxchain);
1153bd9dcd04SYang Shi 	seq_printf(m, "max_checked   : %d\n", debug_objects_maxchecked);
11543ac7fe5aSThomas Gleixner 	seq_printf(m, "warnings      : %d\n", debug_objects_warnings);
11553ac7fe5aSThomas Gleixner 	seq_printf(m, "fixups        : %d\n", debug_objects_fixups);
11562638345dSThomas Gleixner 	seq_printf(m, "pool_free     : %u\n", pool_count(&pool_global) + pcp_free);
11572638345dSThomas Gleixner 	seq_printf(m, "pool_pcp_free : %u\n", pcp_free);
11582638345dSThomas Gleixner 	seq_printf(m, "pool_min_free : %u\n", data_race(pool_global.stats.min_fill));
11592638345dSThomas Gleixner 	seq_printf(m, "pool_used     : %u\n", pool_used);
11602638345dSThomas Gleixner 	seq_printf(m, "pool_max_used : %u\n", data_race(pool_global.stats.max_used));
11612638345dSThomas Gleixner 	seq_printf(m, "on_free_list  : %u\n", pool_count(&pool_to_free));
11620cad93c3SWaiman Long 	seq_printf(m, "objs_allocated: %d\n", debug_objects_allocated);
11630cad93c3SWaiman Long 	seq_printf(m, "objs_freed    : %d\n", debug_objects_freed);
11643ac7fe5aSThomas Gleixner 	return 0;
11653ac7fe5aSThomas Gleixner }
11660f85c480SQinglang Miao DEFINE_SHOW_ATTRIBUTE(debug_stats);
11673ac7fe5aSThomas Gleixner 
debug_objects_init_debugfs(void)11683ac7fe5aSThomas Gleixner static int __init debug_objects_init_debugfs(void)
11693ac7fe5aSThomas Gleixner {
1170fecb0d95SGreg Kroah-Hartman 	struct dentry *dbgdir;
11713ac7fe5aSThomas Gleixner 
11723ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
11733ac7fe5aSThomas Gleixner 		return 0;
11743ac7fe5aSThomas Gleixner 
11753ac7fe5aSThomas Gleixner 	dbgdir = debugfs_create_dir("debug_objects", NULL);
11763ac7fe5aSThomas Gleixner 
1177fecb0d95SGreg Kroah-Hartman 	debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
11783ac7fe5aSThomas Gleixner 
11793ac7fe5aSThomas Gleixner 	return 0;
11803ac7fe5aSThomas Gleixner }
11813ac7fe5aSThomas Gleixner __initcall(debug_objects_init_debugfs);
11823ac7fe5aSThomas Gleixner 
11833ac7fe5aSThomas Gleixner #else
debug_objects_init_debugfs(void)11843ac7fe5aSThomas Gleixner static inline void debug_objects_init_debugfs(void) { }
11853ac7fe5aSThomas Gleixner #endif
11863ac7fe5aSThomas Gleixner 
11873ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
11883ac7fe5aSThomas Gleixner 
11893ac7fe5aSThomas Gleixner /* Random data structure for the self test */
11903ac7fe5aSThomas Gleixner struct self_test {
11913ac7fe5aSThomas Gleixner 	unsigned long	dummy1[6];
11923ac7fe5aSThomas Gleixner 	int		static_init;
11933ac7fe5aSThomas Gleixner 	unsigned long	dummy2[3];
11943ac7fe5aSThomas Gleixner };
11953ac7fe5aSThomas Gleixner 
1196aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test;
11973ac7fe5aSThomas Gleixner 
is_static_object(void * addr)1198b9fdac7fSDu, Changbin static bool __init is_static_object(void *addr)
1199b9fdac7fSDu, Changbin {
1200b9fdac7fSDu, Changbin 	struct self_test *obj = addr;
1201b9fdac7fSDu, Changbin 
1202b9fdac7fSDu, Changbin 	return obj->static_init;
1203b9fdac7fSDu, Changbin }
1204b9fdac7fSDu, Changbin 
12053ac7fe5aSThomas Gleixner /*
12063ac7fe5aSThomas Gleixner  * fixup_init is called when:
12073ac7fe5aSThomas Gleixner  * - an active object is initialized
12083ac7fe5aSThomas Gleixner  */
fixup_init(void * addr,enum debug_obj_state state)1209b1e4d9d8SDu, Changbin static bool __init fixup_init(void *addr, enum debug_obj_state state)
12103ac7fe5aSThomas Gleixner {
12113ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
12123ac7fe5aSThomas Gleixner 
12133ac7fe5aSThomas Gleixner 	switch (state) {
12143ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
12153ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
12163ac7fe5aSThomas Gleixner 		debug_object_init(obj, &descr_type_test);
1217b1e4d9d8SDu, Changbin 		return true;
12183ac7fe5aSThomas Gleixner 	default:
1219b1e4d9d8SDu, Changbin 		return false;
12203ac7fe5aSThomas Gleixner 	}
12213ac7fe5aSThomas Gleixner }
12223ac7fe5aSThomas Gleixner 
12233ac7fe5aSThomas Gleixner /*
12243ac7fe5aSThomas Gleixner  * fixup_activate is called when:
12253ac7fe5aSThomas Gleixner  * - an active object is activated
1226b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
12273ac7fe5aSThomas Gleixner  */
fixup_activate(void * addr,enum debug_obj_state state)1228b1e4d9d8SDu, Changbin static bool __init fixup_activate(void *addr, enum debug_obj_state state)
12293ac7fe5aSThomas Gleixner {
12303ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
12313ac7fe5aSThomas Gleixner 
12323ac7fe5aSThomas Gleixner 	switch (state) {
12333ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
1234b1e4d9d8SDu, Changbin 		return true;
12353ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
12363ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
12373ac7fe5aSThomas Gleixner 		debug_object_activate(obj, &descr_type_test);
1238b1e4d9d8SDu, Changbin 		return true;
12393ac7fe5aSThomas Gleixner 
12403ac7fe5aSThomas Gleixner 	default:
1241b1e4d9d8SDu, Changbin 		return false;
12423ac7fe5aSThomas Gleixner 	}
12433ac7fe5aSThomas Gleixner }
12443ac7fe5aSThomas Gleixner 
12453ac7fe5aSThomas Gleixner /*
12463ac7fe5aSThomas Gleixner  * fixup_destroy is called when:
12473ac7fe5aSThomas Gleixner  * - an active object is destroyed
12483ac7fe5aSThomas Gleixner  */
fixup_destroy(void * addr,enum debug_obj_state state)1249b1e4d9d8SDu, Changbin static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
12503ac7fe5aSThomas Gleixner {
12513ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
12523ac7fe5aSThomas Gleixner 
12533ac7fe5aSThomas Gleixner 	switch (state) {
12543ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
12553ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
12563ac7fe5aSThomas Gleixner 		debug_object_destroy(obj, &descr_type_test);
1257b1e4d9d8SDu, Changbin 		return true;
12583ac7fe5aSThomas Gleixner 	default:
1259b1e4d9d8SDu, Changbin 		return false;
12603ac7fe5aSThomas Gleixner 	}
12613ac7fe5aSThomas Gleixner }
12623ac7fe5aSThomas Gleixner 
12633ac7fe5aSThomas Gleixner /*
12643ac7fe5aSThomas Gleixner  * fixup_free is called when:
12653ac7fe5aSThomas Gleixner  * - an active object is freed
12663ac7fe5aSThomas Gleixner  */
fixup_free(void * addr,enum debug_obj_state state)1267b1e4d9d8SDu, Changbin static bool __init fixup_free(void *addr, enum debug_obj_state state)
12683ac7fe5aSThomas Gleixner {
12693ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
12703ac7fe5aSThomas Gleixner 
12713ac7fe5aSThomas Gleixner 	switch (state) {
12723ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
12733ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
12743ac7fe5aSThomas Gleixner 		debug_object_free(obj, &descr_type_test);
1275b1e4d9d8SDu, Changbin 		return true;
12763ac7fe5aSThomas Gleixner 	default:
1277b1e4d9d8SDu, Changbin 		return false;
12783ac7fe5aSThomas Gleixner 	}
12793ac7fe5aSThomas Gleixner }
12803ac7fe5aSThomas Gleixner 
12811fb2f77cSHenrik Kretzschmar static int __init
check_results(void * addr,enum debug_obj_state state,int fixups,int warnings)12823ac7fe5aSThomas Gleixner check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
12833ac7fe5aSThomas Gleixner {
12843ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
12853ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
12863ac7fe5aSThomas Gleixner 	unsigned long flags;
12873ac7fe5aSThomas Gleixner 	int res = -EINVAL;
12883ac7fe5aSThomas Gleixner 
12893ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
12903ac7fe5aSThomas Gleixner 
1291aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
12923ac7fe5aSThomas Gleixner 
12933ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
12943ac7fe5aSThomas Gleixner 	if (!obj && state != ODEBUG_STATE_NONE) {
12955cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
12963ac7fe5aSThomas Gleixner 		goto out;
12973ac7fe5aSThomas Gleixner 	}
12983ac7fe5aSThomas Gleixner 	if (obj && obj->state != state) {
12995cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
13003ac7fe5aSThomas Gleixner 		       obj->state, state);
13013ac7fe5aSThomas Gleixner 		goto out;
13023ac7fe5aSThomas Gleixner 	}
13033ac7fe5aSThomas Gleixner 	if (fixups != debug_objects_fixups) {
13045cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
13053ac7fe5aSThomas Gleixner 		       fixups, debug_objects_fixups);
13063ac7fe5aSThomas Gleixner 		goto out;
13073ac7fe5aSThomas Gleixner 	}
13083ac7fe5aSThomas Gleixner 	if (warnings != debug_objects_warnings) {
13095cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
13103ac7fe5aSThomas Gleixner 		       warnings, debug_objects_warnings);
13113ac7fe5aSThomas Gleixner 		goto out;
13123ac7fe5aSThomas Gleixner 	}
13133ac7fe5aSThomas Gleixner 	res = 0;
13143ac7fe5aSThomas Gleixner out:
1315aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
13163ac7fe5aSThomas Gleixner 	if (res)
1317661cc28bSThomas Gleixner 		debug_objects_enabled = false;
13183ac7fe5aSThomas Gleixner 	return res;
13193ac7fe5aSThomas Gleixner }
13203ac7fe5aSThomas Gleixner 
1321aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test = {
13223ac7fe5aSThomas Gleixner 	.name			= "selftest",
1323b9fdac7fSDu, Changbin 	.is_static_object	= is_static_object,
13243ac7fe5aSThomas Gleixner 	.fixup_init		= fixup_init,
13253ac7fe5aSThomas Gleixner 	.fixup_activate		= fixup_activate,
13263ac7fe5aSThomas Gleixner 	.fixup_destroy		= fixup_destroy,
13273ac7fe5aSThomas Gleixner 	.fixup_free		= fixup_free,
13283ac7fe5aSThomas Gleixner };
13293ac7fe5aSThomas Gleixner 
13303ac7fe5aSThomas Gleixner static __initdata struct self_test obj = { .static_init = 0 };
13313ac7fe5aSThomas Gleixner 
debug_objects_selftest(void)133255fb412eSThomas Gleixner static bool __init debug_objects_selftest(void)
13333ac7fe5aSThomas Gleixner {
13343ac7fe5aSThomas Gleixner 	int fixups, oldfixups, warnings, oldwarnings;
13353ac7fe5aSThomas Gleixner 	unsigned long flags;
13363ac7fe5aSThomas Gleixner 
13373ac7fe5aSThomas Gleixner 	local_irq_save(flags);
13383ac7fe5aSThomas Gleixner 
13393ac7fe5aSThomas Gleixner 	fixups = oldfixups = debug_objects_fixups;
13403ac7fe5aSThomas Gleixner 	warnings = oldwarnings = debug_objects_warnings;
13413ac7fe5aSThomas Gleixner 	descr_test = &descr_type_test;
13423ac7fe5aSThomas Gleixner 
13433ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13443ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
13453ac7fe5aSThomas Gleixner 		goto out;
13463ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13473ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13483ac7fe5aSThomas Gleixner 		goto out;
13493ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13503ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
13513ac7fe5aSThomas Gleixner 		goto out;
13523ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
13533ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
13543ac7fe5aSThomas Gleixner 		goto out;
13553ac7fe5aSThomas Gleixner 	debug_object_destroy(&obj, &descr_type_test);
13563ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
13573ac7fe5aSThomas Gleixner 		goto out;
13583ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13593ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
13603ac7fe5aSThomas Gleixner 		goto out;
13613ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13623ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
13633ac7fe5aSThomas Gleixner 		goto out;
13643ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
13653ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
13663ac7fe5aSThomas Gleixner 		goto out;
13673ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
13683ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
13693ac7fe5aSThomas Gleixner 		goto out;
13703ac7fe5aSThomas Gleixner 
13713ac7fe5aSThomas Gleixner 	obj.static_init = 1;
13723ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13739f78ff00SStephen Boyd 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13743ac7fe5aSThomas Gleixner 		goto out;
13753ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13763ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
13773ac7fe5aSThomas Gleixner 		goto out;
13783ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
13793ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
13803ac7fe5aSThomas Gleixner 		goto out;
13813ac7fe5aSThomas Gleixner 
13823ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
13833ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13843ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
13853ac7fe5aSThomas Gleixner 		goto out;
13863ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13873ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13883ac7fe5aSThomas Gleixner 		goto out;
13893ac7fe5aSThomas Gleixner 	__debug_check_no_obj_freed(&obj, sizeof(obj));
13903ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
13913ac7fe5aSThomas Gleixner 		goto out;
13923ac7fe5aSThomas Gleixner #endif
1393719e4843SFabian Frederick 	pr_info("selftest passed\n");
13943ac7fe5aSThomas Gleixner 
13953ac7fe5aSThomas Gleixner out:
13963ac7fe5aSThomas Gleixner 	debug_objects_fixups = oldfixups;
13973ac7fe5aSThomas Gleixner 	debug_objects_warnings = oldwarnings;
13983ac7fe5aSThomas Gleixner 	descr_test = NULL;
13993ac7fe5aSThomas Gleixner 
14003ac7fe5aSThomas Gleixner 	local_irq_restore(flags);
1401661cc28bSThomas Gleixner 	return debug_objects_enabled;
14023ac7fe5aSThomas Gleixner }
14033ac7fe5aSThomas Gleixner #else
debug_objects_selftest(void)140455fb412eSThomas Gleixner static inline bool debug_objects_selftest(void) { return true; }
14053ac7fe5aSThomas Gleixner #endif
14063ac7fe5aSThomas Gleixner 
14073ac7fe5aSThomas Gleixner /*
14083ac7fe5aSThomas Gleixner  * Called during early boot to initialize the hash buckets and link
14093ac7fe5aSThomas Gleixner  * the static object pool objects into the poll list. After this call
14103ac7fe5aSThomas Gleixner  * the object tracker is fully operational.
14113ac7fe5aSThomas Gleixner  */
debug_objects_early_init(void)14123ac7fe5aSThomas Gleixner void __init debug_objects_early_init(void)
14133ac7fe5aSThomas Gleixner {
14143ac7fe5aSThomas Gleixner 	int i;
14153ac7fe5aSThomas Gleixner 
14163ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1417aef9cb05SThomas Gleixner 		raw_spin_lock_init(&obj_hash[i].lock);
14183ac7fe5aSThomas Gleixner 
1419cb58d190SThomas Gleixner 	/* Keep early boot simple and add everything to the boot list */
14203ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1421cb58d190SThomas Gleixner 		hlist_add_head(&obj_static_pool[i].node, &pool_boot);
14223ac7fe5aSThomas Gleixner }
14233ac7fe5aSThomas Gleixner 
14243ac7fe5aSThomas Gleixner /*
142555fb412eSThomas Gleixner  * Convert the statically allocated objects to dynamic ones.
142655fb412eSThomas Gleixner  * debug_objects_mem_init() is called early so only one CPU is up and
142755fb412eSThomas Gleixner  * interrupts are disabled, which means it is safe to replace the active
142855fb412eSThomas Gleixner  * object references.
14291be1cb7bSThomas Gleixner  */
debug_objects_replace_static_objects(struct kmem_cache * cache)143055fb412eSThomas Gleixner static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache)
14311be1cb7bSThomas Gleixner {
14321be1cb7bSThomas Gleixner 	struct debug_bucket *db = obj_hash;
143355fb412eSThomas Gleixner 	struct hlist_node *tmp;
1434aebbfe07SThomas Gleixner 	struct debug_obj *obj;
14351be1cb7bSThomas Gleixner 	HLIST_HEAD(objects);
1436241463f4SThomas Gleixner 	int i;
14371be1cb7bSThomas Gleixner 
1438aebbfe07SThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i += ODEBUG_BATCH_SIZE) {
1439aebbfe07SThomas Gleixner 		if (!kmem_alloc_batch(&objects, cache, GFP_KERNEL))
14401be1cb7bSThomas Gleixner 			goto free;
1441aebbfe07SThomas Gleixner 		pool_push_batch(&pool_global, &objects);
14421be1cb7bSThomas Gleixner 	}
14431be1cb7bSThomas Gleixner 
1444aebbfe07SThomas Gleixner 	/* Disconnect the boot pool. */
1445cb58d190SThomas Gleixner 	pool_boot.first = NULL;
14461be1cb7bSThomas Gleixner 
14471be1cb7bSThomas Gleixner 	/* Replace the active object references */
14481be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
14491be1cb7bSThomas Gleixner 		hlist_move_list(&db->list, &objects);
14501be1cb7bSThomas Gleixner 
1451b67bfe0dSSasha Levin 		hlist_for_each_entry(obj, &objects, node) {
1452aebbfe07SThomas Gleixner 			struct debug_obj *new = pcpu_alloc();
1453aebbfe07SThomas Gleixner 
14541be1cb7bSThomas Gleixner 			/* copy object data */
14551be1cb7bSThomas Gleixner 			*new = *obj;
14561be1cb7bSThomas Gleixner 			hlist_add_head(&new->node, &db->list);
14571be1cb7bSThomas Gleixner 		}
14581be1cb7bSThomas Gleixner 	}
145955fb412eSThomas Gleixner 	return true;
14601be1cb7bSThomas Gleixner free:
146149a5cb82SThomas Gleixner 	/* Can't use free_object_list() as the cache is not populated yet */
1462aebbfe07SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, &pool_global.objects, node) {
14631be1cb7bSThomas Gleixner 		hlist_del(&obj->node);
146455fb412eSThomas Gleixner 		kmem_cache_free(cache, obj);
14651be1cb7bSThomas Gleixner 	}
146655fb412eSThomas Gleixner 	return false;
14671be1cb7bSThomas Gleixner }
14681be1cb7bSThomas Gleixner 
14691be1cb7bSThomas Gleixner /*
14703ac7fe5aSThomas Gleixner  * Called after the kmem_caches are functional to setup a dedicated
14713ac7fe5aSThomas Gleixner  * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
14723ac7fe5aSThomas Gleixner  * prevents that the debug code is called on kmem_cache_free() for the
14733ac7fe5aSThomas Gleixner  * debug tracker objects to avoid recursive calls.
14743ac7fe5aSThomas Gleixner  */
debug_objects_mem_init(void)14753ac7fe5aSThomas Gleixner void __init debug_objects_mem_init(void)
14763ac7fe5aSThomas Gleixner {
147755fb412eSThomas Gleixner 	struct kmem_cache *cache;
14783f397bf9SThomas Gleixner 	int extras;
1479d86998b1SWaiman Long 
14803ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
14813ac7fe5aSThomas Gleixner 		return;
14823ac7fe5aSThomas Gleixner 
148355fb412eSThomas Gleixner 	if (!debug_objects_selftest())
1484eabb7f1aSwuchi 		return;
148555fb412eSThomas Gleixner 
148655fb412eSThomas Gleixner 	cache = kmem_cache_create("debug_objects_cache", sizeof (struct debug_obj), 0,
148755fb412eSThomas Gleixner 				  SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, NULL);
148855fb412eSThomas Gleixner 
148955fb412eSThomas Gleixner 	if (!cache || !debug_objects_replace_static_objects(cache)) {
1490661cc28bSThomas Gleixner 		debug_objects_enabled = false;
149155fb412eSThomas Gleixner 		pr_warn("Out of memory.\n");
149255fb412eSThomas Gleixner 		return;
149355fb412eSThomas Gleixner 	}
149455fb412eSThomas Gleixner 
149555fb412eSThomas Gleixner 	/*
149655fb412eSThomas Gleixner 	 * Adjust the thresholds for allocating and freeing objects
149755fb412eSThomas Gleixner 	 * according to the number of possible CPUs available in the
149855fb412eSThomas Gleixner 	 * system.
149955fb412eSThomas Gleixner 	 */
150055fb412eSThomas Gleixner 	extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
150196a9a042SThomas Gleixner 	pool_global.max_cnt += extras;
150296a9a042SThomas Gleixner 	pool_global.min_cnt += extras;
150355fb412eSThomas Gleixner 
150455fb412eSThomas Gleixner 	/* Everything worked. Expose the cache */
150555fb412eSThomas Gleixner 	obj_cache = cache;
150614077b9eSThomas Gleixner 	static_branch_enable(&obj_cache_enabled);
1507634d61f4SWaiman Long 
150888451f2cSZqiang #ifdef CONFIG_HOTPLUG_CPU
150988451f2cSZqiang 	cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
151088451f2cSZqiang 				  object_cpu_offline);
151188451f2cSZqiang #endif
151255fb412eSThomas Gleixner 	return;
15133ac7fe5aSThomas Gleixner }
1514