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 	 * To implement set_graph_notrace, if this bit is set, we ignore
49 	 * function graph tracing of called functions, until the return
50 	 * function is called to clear it.
51 	 */
52 	TRACE_GRAPH_NOTRACE_BIT,
53 
54 	/* Used to prevent recursion recording from recursing. */
55 	TRACE_RECORD_RECURSION_BIT,
56 };
57 
58 #define trace_recursion_set(bit)	do { (current)->trace_recursion |= (1<<(bit)); } while (0)
59 #define trace_recursion_clear(bit)	do { (current)->trace_recursion &= ~(1<<(bit)); } while (0)
60 #define trace_recursion_test(bit)	((current)->trace_recursion & (1<<(bit)))
61 
62 #define TRACE_CONTEXT_BITS	4
63 
64 #define TRACE_FTRACE_START	TRACE_FTRACE_BIT
65 
66 #define TRACE_LIST_START	TRACE_INTERNAL_BIT
67 
68 #define TRACE_CONTEXT_MASK	((1 << (TRACE_LIST_START + TRACE_CONTEXT_BITS)) - 1)
69 
70 /*
71  * Used for setting context
72  *  NMI     = 0
73  *  IRQ     = 1
74  *  SOFTIRQ = 2
75  *  NORMAL  = 3
76  */
77 enum {
78 	TRACE_CTX_NMI,
79 	TRACE_CTX_IRQ,
80 	TRACE_CTX_SOFTIRQ,
81 	TRACE_CTX_NORMAL,
82 	TRACE_CTX_TRANSITION,
83 };
84 
85 static __always_inline int trace_get_context_bit(void)
86 {
87 	unsigned char bit = interrupt_context_level();
88 
89 	return TRACE_CTX_NORMAL - bit;
90 }
91 
92 #ifdef CONFIG_FTRACE_RECORD_RECURSION
93 extern void ftrace_record_recursion(unsigned long ip, unsigned long parent_ip);
94 # define do_ftrace_record_recursion(ip, pip)				\
95 	do {								\
96 		if (!trace_recursion_test(TRACE_RECORD_RECURSION_BIT)) { \
97 			trace_recursion_set(TRACE_RECORD_RECURSION_BIT); \
98 			ftrace_record_recursion(ip, pip);		\
99 			trace_recursion_clear(TRACE_RECORD_RECURSION_BIT); \
100 		}							\
101 	} while (0)
102 #else
103 # define do_ftrace_record_recursion(ip, pip)	do { } while (0)
104 #endif
105 
106 #ifdef CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING
107 # define trace_warn_on_no_rcu(ip)					\
108 	({								\
109 		bool __ret = !rcu_is_watching();			\
110 		if (__ret && !trace_recursion_test(TRACE_RECORD_RECURSION_BIT)) { \
111 			trace_recursion_set(TRACE_RECORD_RECURSION_BIT); \
112 			WARN_ONCE(true, "RCU not on for: %pS\n", (void *)ip); \
113 			trace_recursion_clear(TRACE_RECORD_RECURSION_BIT); \
114 		}							\
115 		__ret;							\
116 	})
117 #else
118 # define trace_warn_on_no_rcu(ip)	false
119 #endif
120 
121 /*
122  * Preemption is promised to be disabled when return bit >= 0.
123  */
124 static __always_inline int trace_test_and_set_recursion(unsigned long ip, unsigned long pip,
125 							int start)
126 {
127 	unsigned int val = READ_ONCE(current->trace_recursion);
128 	int bit;
129 
130 	if (trace_warn_on_no_rcu(ip))
131 		return -1;
132 
133 	bit = trace_get_context_bit() + start;
134 	if (unlikely(val & (1 << bit))) {
135 		/*
136 		 * If an interrupt occurs during a trace, and another trace
137 		 * happens in that interrupt but before the preempt_count is
138 		 * updated to reflect the new interrupt context, then this
139 		 * will think a recursion occurred, and the event will be dropped.
140 		 * Let a single instance happen via the TRANSITION_BIT to
141 		 * not drop those events.
142 		 */
143 		bit = TRACE_CTX_TRANSITION + start;
144 		if (val & (1 << bit)) {
145 			do_ftrace_record_recursion(ip, pip);
146 			return -1;
147 		}
148 	}
149 
150 	val |= 1 << bit;
151 	current->trace_recursion = val;
152 	barrier();
153 
154 	preempt_disable_notrace();
155 
156 	return bit;
157 }
158 
159 /*
160  * Preemption will be enabled (if it was previously enabled).
161  */
162 static __always_inline void trace_clear_recursion(int bit)
163 {
164 	preempt_enable_notrace();
165 	barrier();
166 	trace_recursion_clear(bit);
167 }
168 
169 /**
170  * ftrace_test_recursion_trylock - tests for recursion in same context
171  *
172  * Use this for ftrace callbacks. This will detect if the function
173  * tracing recursed in the same context (normal vs interrupt),
174  *
175  * Returns: -1 if a recursion happened.
176  *           >= 0 if no recursion.
177  */
178 static __always_inline int ftrace_test_recursion_trylock(unsigned long ip,
179 							 unsigned long parent_ip)
180 {
181 	return trace_test_and_set_recursion(ip, parent_ip, TRACE_FTRACE_START);
182 }
183 
184 /**
185  * ftrace_test_recursion_unlock - called when function callback is complete
186  * @bit: The return of a successful ftrace_test_recursion_trylock()
187  *
188  * This is used at the end of a ftrace callback.
189  */
190 static __always_inline void ftrace_test_recursion_unlock(int bit)
191 {
192 	trace_clear_recursion(bit);
193 }
194 
195 #endif /* CONFIG_TRACING */
196 #endif /* _LINUX_TRACE_RECURSION_H */
197