xref: /OK3568_Linux_fs/kernel/lib/stackdepot.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Generic stack depot for storing stack traces.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Some debugging tools need to save stack traces of certain events which can
6*4882a593Smuzhiyun  * be later presented to the user. For example, KASAN needs to safe alloc and
7*4882a593Smuzhiyun  * free stacks for each object, but storing two stack traces per object
8*4882a593Smuzhiyun  * requires too much memory (e.g. SLUB_DEBUG needs 256 bytes per object for
9*4882a593Smuzhiyun  * that).
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Instead, stack depot maintains a hashtable of unique stacktraces. Since alloc
12*4882a593Smuzhiyun  * and free stacks repeat a lot, we save about 100x space.
13*4882a593Smuzhiyun  * Stacks are never removed from depot, so we store them contiguously one after
14*4882a593Smuzhiyun  * another in a contiguos memory allocation.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Author: Alexander Potapenko <glider@google.com>
17*4882a593Smuzhiyun  * Copyright (C) 2016 Google, Inc.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * Based on code by Dmitry Chernenkov.
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <linux/gfp.h>
23*4882a593Smuzhiyun #include <linux/interrupt.h>
24*4882a593Smuzhiyun #include <linux/jhash.h>
25*4882a593Smuzhiyun #include <linux/kernel.h>
26*4882a593Smuzhiyun #include <linux/mm.h>
27*4882a593Smuzhiyun #include <linux/percpu.h>
28*4882a593Smuzhiyun #include <linux/printk.h>
29*4882a593Smuzhiyun #include <linux/slab.h>
30*4882a593Smuzhiyun #include <linux/stacktrace.h>
31*4882a593Smuzhiyun #include <linux/stackdepot.h>
32*4882a593Smuzhiyun #include <linux/string.h>
33*4882a593Smuzhiyun #include <linux/types.h>
34*4882a593Smuzhiyun #include <linux/memblock.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define STACK_ALLOC_NULL_PROTECTION_BITS 1
39*4882a593Smuzhiyun #define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */
40*4882a593Smuzhiyun #define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
41*4882a593Smuzhiyun #define STACK_ALLOC_ALIGN 4
42*4882a593Smuzhiyun #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
43*4882a593Smuzhiyun 					STACK_ALLOC_ALIGN)
44*4882a593Smuzhiyun #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
45*4882a593Smuzhiyun 		STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
46*4882a593Smuzhiyun #define STACK_ALLOC_SLABS_CAP 8192
47*4882a593Smuzhiyun #define STACK_ALLOC_MAX_SLABS \
48*4882a593Smuzhiyun 	(((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
49*4882a593Smuzhiyun 	 (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /* The compact structure to store the reference to stacks. */
52*4882a593Smuzhiyun union handle_parts {
53*4882a593Smuzhiyun 	depot_stack_handle_t handle;
54*4882a593Smuzhiyun 	struct {
55*4882a593Smuzhiyun 		u32 slabindex : STACK_ALLOC_INDEX_BITS;
56*4882a593Smuzhiyun 		u32 offset : STACK_ALLOC_OFFSET_BITS;
57*4882a593Smuzhiyun 		u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
58*4882a593Smuzhiyun 	};
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun struct stack_record {
62*4882a593Smuzhiyun 	struct stack_record *next;	/* Link in the hashtable */
63*4882a593Smuzhiyun 	u32 hash;			/* Hash in the hastable */
64*4882a593Smuzhiyun 	u32 size;			/* Number of frames in the stack */
65*4882a593Smuzhiyun 	union handle_parts handle;
66*4882a593Smuzhiyun 	unsigned long entries[];	/* Variable-sized array of entries. */
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun static int depot_index;
72*4882a593Smuzhiyun static int next_slab_inited;
73*4882a593Smuzhiyun static size_t depot_offset;
74*4882a593Smuzhiyun static DEFINE_RAW_SPINLOCK(depot_lock);
75*4882a593Smuzhiyun 
init_stack_slab(void ** prealloc)76*4882a593Smuzhiyun static bool init_stack_slab(void **prealloc)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	if (!*prealloc)
79*4882a593Smuzhiyun 		return false;
80*4882a593Smuzhiyun 	/*
81*4882a593Smuzhiyun 	 * This smp_load_acquire() pairs with smp_store_release() to
82*4882a593Smuzhiyun 	 * |next_slab_inited| below and in depot_alloc_stack().
83*4882a593Smuzhiyun 	 */
84*4882a593Smuzhiyun 	if (smp_load_acquire(&next_slab_inited))
85*4882a593Smuzhiyun 		return true;
86*4882a593Smuzhiyun 	if (stack_slabs[depot_index] == NULL) {
87*4882a593Smuzhiyun 		stack_slabs[depot_index] = *prealloc;
88*4882a593Smuzhiyun 		*prealloc = NULL;
89*4882a593Smuzhiyun 	} else {
90*4882a593Smuzhiyun 		/* If this is the last depot slab, do not touch the next one. */
91*4882a593Smuzhiyun 		if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) {
92*4882a593Smuzhiyun 			stack_slabs[depot_index + 1] = *prealloc;
93*4882a593Smuzhiyun 			*prealloc = NULL;
94*4882a593Smuzhiyun 		}
95*4882a593Smuzhiyun 		/*
96*4882a593Smuzhiyun 		 * This smp_store_release pairs with smp_load_acquire() from
97*4882a593Smuzhiyun 		 * |next_slab_inited| above and in stack_depot_save().
98*4882a593Smuzhiyun 		 */
99*4882a593Smuzhiyun 		smp_store_release(&next_slab_inited, 1);
100*4882a593Smuzhiyun 	}
101*4882a593Smuzhiyun 	return true;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /* Allocation of a new stack in raw storage */
depot_alloc_stack(unsigned long * entries,int size,u32 hash,void ** prealloc,gfp_t alloc_flags)105*4882a593Smuzhiyun static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
106*4882a593Smuzhiyun 		u32 hash, void **prealloc, gfp_t alloc_flags)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	struct stack_record *stack;
109*4882a593Smuzhiyun 	size_t required_size = struct_size(stack, entries, size);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
114*4882a593Smuzhiyun 		if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
115*4882a593Smuzhiyun 			WARN_ONCE(1, "Stack depot reached limit capacity");
116*4882a593Smuzhiyun 			return NULL;
117*4882a593Smuzhiyun 		}
118*4882a593Smuzhiyun 		depot_index++;
119*4882a593Smuzhiyun 		depot_offset = 0;
120*4882a593Smuzhiyun 		/*
121*4882a593Smuzhiyun 		 * smp_store_release() here pairs with smp_load_acquire() from
122*4882a593Smuzhiyun 		 * |next_slab_inited| in stack_depot_save() and
123*4882a593Smuzhiyun 		 * init_stack_slab().
124*4882a593Smuzhiyun 		 */
125*4882a593Smuzhiyun 		if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
126*4882a593Smuzhiyun 			smp_store_release(&next_slab_inited, 0);
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 	init_stack_slab(prealloc);
129*4882a593Smuzhiyun 	if (stack_slabs[depot_index] == NULL)
130*4882a593Smuzhiyun 		return NULL;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	stack = stack_slabs[depot_index] + depot_offset;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	stack->hash = hash;
135*4882a593Smuzhiyun 	stack->size = size;
136*4882a593Smuzhiyun 	stack->handle.slabindex = depot_index;
137*4882a593Smuzhiyun 	stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
138*4882a593Smuzhiyun 	stack->handle.valid = 1;
139*4882a593Smuzhiyun 	memcpy(stack->entries, entries, flex_array_size(stack, entries, size));
140*4882a593Smuzhiyun 	depot_offset += required_size;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return stack;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun #define STACK_HASH_SIZE (1L << CONFIG_STACK_HASH_ORDER)
146*4882a593Smuzhiyun #define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
147*4882a593Smuzhiyun #define STACK_HASH_SEED 0x9747b28c
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun static bool stack_depot_disable;
150*4882a593Smuzhiyun static struct stack_record **stack_table;
151*4882a593Smuzhiyun 
is_stack_depot_disabled(char * str)152*4882a593Smuzhiyun static int __init is_stack_depot_disabled(char *str)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	int ret;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	ret = kstrtobool(str, &stack_depot_disable);
157*4882a593Smuzhiyun 	if (!ret && stack_depot_disable) {
158*4882a593Smuzhiyun 		pr_info("Stack Depot is disabled\n");
159*4882a593Smuzhiyun 		stack_table = NULL;
160*4882a593Smuzhiyun 	}
161*4882a593Smuzhiyun 	return 0;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun early_param("stack_depot_disable", is_stack_depot_disabled);
164*4882a593Smuzhiyun 
stack_depot_init(void)165*4882a593Smuzhiyun int __init stack_depot_init(void)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	if (!stack_depot_disable) {
168*4882a593Smuzhiyun 		size_t size = (STACK_HASH_SIZE * sizeof(struct stack_record *));
169*4882a593Smuzhiyun 		int i;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 		stack_table = memblock_alloc(size, size);
172*4882a593Smuzhiyun 		for (i = 0; i < STACK_HASH_SIZE;  i++)
173*4882a593Smuzhiyun 			stack_table[i] = NULL;
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 	return 0;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun /* Calculate hash for a stack */
hash_stack(unsigned long * entries,unsigned int size)179*4882a593Smuzhiyun static inline u32 hash_stack(unsigned long *entries, unsigned int size)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	return jhash2((u32 *)entries,
182*4882a593Smuzhiyun 		      array_size(size,  sizeof(*entries)) / sizeof(u32),
183*4882a593Smuzhiyun 		      STACK_HASH_SEED);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /* Use our own, non-instrumented version of memcmp().
187*4882a593Smuzhiyun  *
188*4882a593Smuzhiyun  * We actually don't care about the order, just the equality.
189*4882a593Smuzhiyun  */
190*4882a593Smuzhiyun static inline
stackdepot_memcmp(const unsigned long * u1,const unsigned long * u2,unsigned int n)191*4882a593Smuzhiyun int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
192*4882a593Smuzhiyun 			unsigned int n)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	for ( ; n-- ; u1++, u2++) {
195*4882a593Smuzhiyun 		if (*u1 != *u2)
196*4882a593Smuzhiyun 			return 1;
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun 	return 0;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /* Find a stack that is equal to the one stored in entries in the hash */
find_stack(struct stack_record * bucket,unsigned long * entries,int size,u32 hash)202*4882a593Smuzhiyun static inline struct stack_record *find_stack(struct stack_record *bucket,
203*4882a593Smuzhiyun 					     unsigned long *entries, int size,
204*4882a593Smuzhiyun 					     u32 hash)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	struct stack_record *found;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	for (found = bucket; found; found = found->next) {
209*4882a593Smuzhiyun 		if (found->hash == hash &&
210*4882a593Smuzhiyun 		    found->size == size &&
211*4882a593Smuzhiyun 		    !stackdepot_memcmp(entries, found->entries, size))
212*4882a593Smuzhiyun 			return found;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 	return NULL;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun /**
218*4882a593Smuzhiyun  * stack_depot_fetch - Fetch stack entries from a depot
219*4882a593Smuzhiyun  *
220*4882a593Smuzhiyun  * @handle:		Stack depot handle which was returned from
221*4882a593Smuzhiyun  *			stack_depot_save().
222*4882a593Smuzhiyun  * @entries:		Pointer to store the entries address
223*4882a593Smuzhiyun  *
224*4882a593Smuzhiyun  * Return: The number of trace entries for this depot.
225*4882a593Smuzhiyun  */
stack_depot_fetch(depot_stack_handle_t handle,unsigned long ** entries)226*4882a593Smuzhiyun unsigned int stack_depot_fetch(depot_stack_handle_t handle,
227*4882a593Smuzhiyun 			       unsigned long **entries)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	union handle_parts parts = { .handle = handle };
230*4882a593Smuzhiyun 	void *slab;
231*4882a593Smuzhiyun 	size_t offset = parts.offset << STACK_ALLOC_ALIGN;
232*4882a593Smuzhiyun 	struct stack_record *stack;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	*entries = NULL;
235*4882a593Smuzhiyun 	if (parts.slabindex > depot_index) {
236*4882a593Smuzhiyun 		WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n",
237*4882a593Smuzhiyun 			parts.slabindex, depot_index, handle);
238*4882a593Smuzhiyun 		return 0;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 	slab = stack_slabs[parts.slabindex];
241*4882a593Smuzhiyun 	if (!slab)
242*4882a593Smuzhiyun 		return 0;
243*4882a593Smuzhiyun 	stack = slab + offset;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	*entries = stack->entries;
246*4882a593Smuzhiyun 	return stack->size;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(stack_depot_fetch);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun /**
251*4882a593Smuzhiyun  * stack_depot_save - Save a stack trace from an array
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * @entries:		Pointer to storage array
254*4882a593Smuzhiyun  * @nr_entries:		Size of the storage array
255*4882a593Smuzhiyun  * @alloc_flags:	Allocation gfp flags
256*4882a593Smuzhiyun  *
257*4882a593Smuzhiyun  * Return: The handle of the stack struct stored in depot
258*4882a593Smuzhiyun  */
stack_depot_save(unsigned long * entries,unsigned int nr_entries,gfp_t alloc_flags)259*4882a593Smuzhiyun depot_stack_handle_t stack_depot_save(unsigned long *entries,
260*4882a593Smuzhiyun 				      unsigned int nr_entries,
261*4882a593Smuzhiyun 				      gfp_t alloc_flags)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	struct stack_record *found = NULL, **bucket;
264*4882a593Smuzhiyun 	depot_stack_handle_t retval = 0;
265*4882a593Smuzhiyun 	struct page *page = NULL;
266*4882a593Smuzhiyun 	void *prealloc = NULL;
267*4882a593Smuzhiyun 	unsigned long flags;
268*4882a593Smuzhiyun 	u32 hash;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	if (unlikely(nr_entries == 0) || stack_depot_disable)
271*4882a593Smuzhiyun 		goto fast_exit;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	hash = hash_stack(entries, nr_entries);
274*4882a593Smuzhiyun 	bucket = &stack_table[hash & STACK_HASH_MASK];
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	/*
277*4882a593Smuzhiyun 	 * Fast path: look the stack trace up without locking.
278*4882a593Smuzhiyun 	 * The smp_load_acquire() here pairs with smp_store_release() to
279*4882a593Smuzhiyun 	 * |bucket| below.
280*4882a593Smuzhiyun 	 */
281*4882a593Smuzhiyun 	found = find_stack(smp_load_acquire(bucket), entries,
282*4882a593Smuzhiyun 			   nr_entries, hash);
283*4882a593Smuzhiyun 	if (found)
284*4882a593Smuzhiyun 		goto exit;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	/*
287*4882a593Smuzhiyun 	 * Check if the current or the next stack slab need to be initialized.
288*4882a593Smuzhiyun 	 * If so, allocate the memory - we won't be able to do that under the
289*4882a593Smuzhiyun 	 * lock.
290*4882a593Smuzhiyun 	 *
291*4882a593Smuzhiyun 	 * The smp_load_acquire() here pairs with smp_store_release() to
292*4882a593Smuzhiyun 	 * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
293*4882a593Smuzhiyun 	 */
294*4882a593Smuzhiyun 	if (unlikely(!smp_load_acquire(&next_slab_inited))) {
295*4882a593Smuzhiyun 		/*
296*4882a593Smuzhiyun 		 * Zero out zone modifiers, as we don't have specific zone
297*4882a593Smuzhiyun 		 * requirements. Keep the flags related to allocation in atomic
298*4882a593Smuzhiyun 		 * contexts and I/O.
299*4882a593Smuzhiyun 		 */
300*4882a593Smuzhiyun 		alloc_flags &= ~GFP_ZONEMASK;
301*4882a593Smuzhiyun 		alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
302*4882a593Smuzhiyun 		alloc_flags |= __GFP_NOWARN;
303*4882a593Smuzhiyun 		page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
304*4882a593Smuzhiyun 		if (page)
305*4882a593Smuzhiyun 			prealloc = page_address(page);
306*4882a593Smuzhiyun 	}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	raw_spin_lock_irqsave(&depot_lock, flags);
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	found = find_stack(*bucket, entries, nr_entries, hash);
311*4882a593Smuzhiyun 	if (!found) {
312*4882a593Smuzhiyun 		struct stack_record *new =
313*4882a593Smuzhiyun 			depot_alloc_stack(entries, nr_entries,
314*4882a593Smuzhiyun 					  hash, &prealloc, alloc_flags);
315*4882a593Smuzhiyun 		if (new) {
316*4882a593Smuzhiyun 			new->next = *bucket;
317*4882a593Smuzhiyun 			/*
318*4882a593Smuzhiyun 			 * This smp_store_release() pairs with
319*4882a593Smuzhiyun 			 * smp_load_acquire() from |bucket| above.
320*4882a593Smuzhiyun 			 */
321*4882a593Smuzhiyun 			smp_store_release(bucket, new);
322*4882a593Smuzhiyun 			found = new;
323*4882a593Smuzhiyun 		}
324*4882a593Smuzhiyun 	} else if (prealloc) {
325*4882a593Smuzhiyun 		/*
326*4882a593Smuzhiyun 		 * We didn't need to store this stack trace, but let's keep
327*4882a593Smuzhiyun 		 * the preallocated memory for the future.
328*4882a593Smuzhiyun 		 */
329*4882a593Smuzhiyun 		WARN_ON(!init_stack_slab(&prealloc));
330*4882a593Smuzhiyun 	}
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	raw_spin_unlock_irqrestore(&depot_lock, flags);
333*4882a593Smuzhiyun exit:
334*4882a593Smuzhiyun 	if (prealloc) {
335*4882a593Smuzhiyun 		/* Nobody used this memory, ok to free it. */
336*4882a593Smuzhiyun 		free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
337*4882a593Smuzhiyun 	}
338*4882a593Smuzhiyun 	if (found)
339*4882a593Smuzhiyun 		retval = found->handle.handle;
340*4882a593Smuzhiyun fast_exit:
341*4882a593Smuzhiyun 	return retval;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(stack_depot_save);
344*4882a593Smuzhiyun 
in_irqentry_text(unsigned long ptr)345*4882a593Smuzhiyun static inline int in_irqentry_text(unsigned long ptr)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	return (ptr >= (unsigned long)&__irqentry_text_start &&
348*4882a593Smuzhiyun 		ptr < (unsigned long)&__irqentry_text_end) ||
349*4882a593Smuzhiyun 		(ptr >= (unsigned long)&__softirqentry_text_start &&
350*4882a593Smuzhiyun 		 ptr < (unsigned long)&__softirqentry_text_end);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
filter_irq_stacks(unsigned long * entries,unsigned int nr_entries)353*4882a593Smuzhiyun unsigned int filter_irq_stacks(unsigned long *entries,
354*4882a593Smuzhiyun 					     unsigned int nr_entries)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun 	unsigned int i;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	for (i = 0; i < nr_entries; i++) {
359*4882a593Smuzhiyun 		if (in_irqentry_text(entries[i])) {
360*4882a593Smuzhiyun 			/* Include the irqentry function into the stack. */
361*4882a593Smuzhiyun 			return i + 1;
362*4882a593Smuzhiyun 		}
363*4882a593Smuzhiyun 	}
364*4882a593Smuzhiyun 	return nr_entries;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(filter_irq_stacks);
367