1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_TRACE_RECURSION_H
3 #define _LINUX_TRACE_RECURSION_H
4 
5 #include <linux/interrupt.h>
6 #include <linux/sched.h>
7 
8 #ifdef CONFIG_TRACING
9 
10 /* Only current can touch trace_recursion */
11 
12 /*
13  * For function tracing recursion:
14  *  The order of these bits are important.
15  *
16  *  When function tracing occurs, the following steps are made:
17  *   If arch does not support a ftrace feature:
18  *    call internal function (uses INTERNAL bits) which calls...
19  *   The function callback, which can use the FTRACE bits to
20  *    check for recursion.
21  */
22 enum {
23 	/* Function recursion bits */
24 	TRACE_FTRACE_BIT,
25 	TRACE_FTRACE_NMI_BIT,
26 	TRACE_FTRACE_IRQ_BIT,
27 	TRACE_FTRACE_SIRQ_BIT,
28 	TRACE_FTRACE_TRANSITION_BIT,
29 
30 	/* Internal use recursion bits */
31 	TRACE_INTERNAL_BIT,
32 	TRACE_INTERNAL_NMI_BIT,
33 	TRACE_INTERNAL_IRQ_BIT,
34 	TRACE_INTERNAL_SIRQ_BIT,
35 	TRACE_INTERNAL_TRANSITION_BIT,
36 
37 	TRACE_BRANCH_BIT,
38 /*
39  * Abuse of the trace_recursion.
40  * As we need a way to maintain state if we are tracing the function
41  * graph in irq because we want to trace a particular function that
42  * was called in irq context but we have irq tracing off. Since this
43  * can only be modified by current, we can reuse trace_recursion.
44  */
45 	TRACE_IRQ_BIT,
46 
47 	/*
48 	 * In the very unlikely case that an interrupt came in
49 	 * at a start of graph tracing, and we want to trace
50 	 * the function in that interrupt, the depth can be greater
51 	 * than zero, because of the preempted start of a previous
52 	 * trace. In an even more unlikely case, depth could be 2
53 	 * if a softirq interrupted the start of graph tracing,
54 	 * followed by an interrupt preempting a start of graph
55 	 * tracing in the softirq, and depth can even be 3
56 	 * if an NMI came in at the start of an interrupt function
57 	 * that preempted a softirq start of a function that
58 	 * preempted normal context!!!! Luckily, it can't be
59 	 * greater than 3, so the next two bits are a mask
60 	 * of what the depth is when we set TRACE_GRAPH_FL
61 	 */
62 
63 	TRACE_GRAPH_DEPTH_START_BIT,
64 	TRACE_GRAPH_DEPTH_END_BIT,
65 
66 	/*
67 	 * To implement set_graph_notrace, if this bit is set, we ignore
68 	 * function graph tracing of called functions, until the return
69 	 * function is called to clear it.
70 	 */
71 	TRACE_GRAPH_NOTRACE_BIT,
72 
73 	/* Used to prevent recursion recording from recursing. */
74 	TRACE_RECORD_RECURSION_BIT,
75 };
76 
77 #define trace_recursion_set(bit)	do { (current)->trace_recursion |= (1<<(bit)); } while (0)
78 #define trace_recursion_clear(bit)	do { (current)->trace_recursion &= ~(1<<(bit)); } while (0)
79 #define trace_recursion_test(bit)	((current)->trace_recursion & (1<<(bit)))
80 
81 #define trace_recursion_depth() \
82 	(((current)->trace_recursion >> TRACE_GRAPH_DEPTH_START_BIT) & 3)
83 #define trace_recursion_set_depth(depth) \
84 	do {								\
85 		current->trace_recursion &=				\
86 			~(3 << TRACE_GRAPH_DEPTH_START_BIT);		\
87 		current->trace_recursion |=				\
88 			((depth) & 3) << TRACE_GRAPH_DEPTH_START_BIT;	\
89 	} while (0)
90 
91 #define TRACE_CONTEXT_BITS	4
92 
93 #define TRACE_FTRACE_START	TRACE_FTRACE_BIT
94 
95 #define TRACE_LIST_START	TRACE_INTERNAL_BIT
96 
97 #define TRACE_CONTEXT_MASK	((1 << (TRACE_LIST_START + TRACE_CONTEXT_BITS)) - 1)
98 
99 /*
100  * Used for setting context
101  *  NMI     = 0
102  *  IRQ     = 1
103  *  SOFTIRQ = 2
104  *  NORMAL  = 3
105  */
106 enum {
107 	TRACE_CTX_NMI,
108 	TRACE_CTX_IRQ,
109 	TRACE_CTX_SOFTIRQ,
110 	TRACE_CTX_NORMAL,
111 	TRACE_CTX_TRANSITION,
112 };
113 
114 static __always_inline int trace_get_context_bit(void)
115 {
116 	unsigned char bit = interrupt_context_level();
117 
118 	return TRACE_CTX_NORMAL - bit;
119 }
120 
121 #ifdef CONFIG_FTRACE_RECORD_RECURSION
122 extern void ftrace_record_recursion(unsigned long ip, unsigned long parent_ip);
123 # define do_ftrace_record_recursion(ip, pip)				\
124 	do {								\
125 		if (!trace_recursion_test(TRACE_RECORD_RECURSION_BIT)) { \
126 			trace_recursion_set(TRACE_RECORD_RECURSION_BIT); \
127 			ftrace_record_recursion(ip, pip);		\
128 			trace_recursion_clear(TRACE_RECORD_RECURSION_BIT); \
129 		}							\
130 	} while (0)
131 #else
132 # define do_ftrace_record_recursion(ip, pip)	do { } while (0)
133 #endif
134 
135 #ifdef CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING
136 # define trace_warn_on_no_rcu(ip)					\
137 	({								\
138 		bool __ret = !rcu_is_watching();			\
139 		if (__ret && !trace_recursion_test(TRACE_RECORD_RECURSION_BIT)) { \
140 			trace_recursion_set(TRACE_RECORD_RECURSION_BIT); \
141 			WARN_ONCE(true, "RCU not on for: %pS\n", (void *)ip); \
142 			trace_recursion_clear(TRACE_RECORD_RECURSION_BIT); \
143 		}							\
144 		__ret;							\
145 	})
146 #else
147 # define trace_warn_on_no_rcu(ip)	false
148 #endif
149 
150 /*
151  * Preemption is promised to be disabled when return bit >= 0.
152  */
153 static __always_inline int trace_test_and_set_recursion(unsigned long ip, unsigned long pip,
154 							int start)
155 {
156 	unsigned int val = READ_ONCE(current->trace_recursion);
157 	int bit;
158 
159 	if (trace_warn_on_no_rcu(ip))
160 		return -1;
161 
162 	bit = trace_get_context_bit() + start;
163 	if (unlikely(val & (1 << bit))) {
164 		/*
165 		 * If an interrupt occurs during a trace, and another trace
166 		 * happens in that interrupt but before the preempt_count is
167 		 * updated to reflect the new interrupt context, then this
168 		 * will think a recursion occurred, and the event will be dropped.
169 		 * Let a single instance happen via the TRANSITION_BIT to
170 		 * not drop those events.
171 		 */
172 		bit = TRACE_CTX_TRANSITION + start;
173 		if (val & (1 << bit)) {
174 			do_ftrace_record_recursion(ip, pip);
175 			return -1;
176 		}
177 	}
178 
179 	val |= 1 << bit;
180 	current->trace_recursion = val;
181 	barrier();
182 
183 	preempt_disable_notrace();
184 
185 	return bit;
186 }
187 
188 /*
189  * Preemption will be enabled (if it was previously enabled).
190  */
191 static __always_inline void trace_clear_recursion(int bit)
192 {
193 	preempt_enable_notrace();
194 	barrier();
195 	trace_recursion_clear(bit);
196 }
197 
198 /**
199  * ftrace_test_recursion_trylock - tests for recursion in same context
200  *
201  * Use this for ftrace callbacks. This will detect if the function
202  * tracing recursed in the same context (normal vs interrupt),
203  *
204  * Returns: -1 if a recursion happened.
205  *           >= 0 if no recursion.
206  */
207 static __always_inline int ftrace_test_recursion_trylock(unsigned long ip,
208 							 unsigned long parent_ip)
209 {
210 	return trace_test_and_set_recursion(ip, parent_ip, TRACE_FTRACE_START);
211 }
212 
213 /**
214  * ftrace_test_recursion_unlock - called when function callback is complete
215  * @bit: The return of a successful ftrace_test_recursion_trylock()
216  *
217  * This is used at the end of a ftrace callback.
218  */
219 static __always_inline void ftrace_test_recursion_unlock(int bit)
220 {
221 	trace_clear_recursion(bit);
222 }
223 
224 #endif /* CONFIG_TRACING */
225 #endif /* _LINUX_TRACE_RECURSION_H */
226