1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * include/linux/irqflags.h
4  *
5  * IRQ flags tracing: follow the state of the hardirq and softirq flags and
6  * provide callbacks for transitions between ON and OFF states.
7  *
8  * This file gets included from lowlevel asm headers too, to provide
9  * wrapped versions of the local_irq_*() APIs, based on the
10  * raw_local_irq_*() macros from the lowlevel headers.
11  */
12 #ifndef _LINUX_TRACE_IRQFLAGS_H
13 #define _LINUX_TRACE_IRQFLAGS_H
14 
15 #include <linux/typecheck.h>
16 #include <asm/irqflags.h>
17 #include <asm/percpu.h>
18 
19 /* Currently lockdep_softirqs_on/off is used only by lockdep */
20 #ifdef CONFIG_PROVE_LOCKING
21   extern void lockdep_softirqs_on(unsigned long ip);
22   extern void lockdep_softirqs_off(unsigned long ip);
23   extern void lockdep_hardirqs_on_prepare(unsigned long ip);
24   extern void lockdep_hardirqs_on(unsigned long ip);
25   extern void lockdep_hardirqs_off(unsigned long ip);
26 #else
lockdep_softirqs_on(unsigned long ip)27   static inline void lockdep_softirqs_on(unsigned long ip) { }
lockdep_softirqs_off(unsigned long ip)28   static inline void lockdep_softirqs_off(unsigned long ip) { }
lockdep_hardirqs_on_prepare(unsigned long ip)29   static inline void lockdep_hardirqs_on_prepare(unsigned long ip) { }
lockdep_hardirqs_on(unsigned long ip)30   static inline void lockdep_hardirqs_on(unsigned long ip) { }
lockdep_hardirqs_off(unsigned long ip)31   static inline void lockdep_hardirqs_off(unsigned long ip) { }
32 #endif
33 
34 #ifdef CONFIG_TRACE_IRQFLAGS
35 
36 /* Per-task IRQ trace events information. */
37 struct irqtrace_events {
38 	unsigned int	irq_events;
39 	unsigned long	hardirq_enable_ip;
40 	unsigned long	hardirq_disable_ip;
41 	unsigned int	hardirq_enable_event;
42 	unsigned int	hardirq_disable_event;
43 	unsigned long	softirq_disable_ip;
44 	unsigned long	softirq_enable_ip;
45 	unsigned int	softirq_disable_event;
46 	unsigned int	softirq_enable_event;
47 };
48 
49 DECLARE_PER_CPU(int, hardirqs_enabled);
50 DECLARE_PER_CPU(int, hardirq_context);
51 
52 extern void trace_hardirqs_on_prepare(void);
53 extern void trace_hardirqs_off_finish(void);
54 extern void trace_hardirqs_on(void);
55 extern void trace_hardirqs_off(void);
56 
57 # define lockdep_hardirq_context()	(raw_cpu_read(hardirq_context))
58 # define lockdep_softirq_context(p)	((p)->softirq_context)
59 # define lockdep_hardirqs_enabled()	(this_cpu_read(hardirqs_enabled))
60 # define lockdep_softirqs_enabled(p)	((p)->softirqs_enabled)
61 # define lockdep_hardirq_enter()			\
62 do {							\
63 	if (__this_cpu_inc_return(hardirq_context) == 1)\
64 		current->hardirq_threaded = 0;		\
65 } while (0)
66 # define lockdep_hardirq_threaded()		\
67 do {						\
68 	current->hardirq_threaded = 1;		\
69 } while (0)
70 # define lockdep_hardirq_exit()			\
71 do {						\
72 	__this_cpu_dec(hardirq_context);	\
73 } while (0)
74 # define lockdep_softirq_enter()		\
75 do {						\
76 	current->softirq_context++;		\
77 } while (0)
78 # define lockdep_softirq_exit()			\
79 do {						\
80 	current->softirq_context--;		\
81 } while (0)
82 
83 # define lockdep_hrtimer_enter(__hrtimer)		\
84 ({							\
85 	bool __expires_hardirq = true;			\
86 							\
87 	if (!__hrtimer->is_hard) {			\
88 		current->irq_config = 1;		\
89 		__expires_hardirq = false;		\
90 	}						\
91 	__expires_hardirq;				\
92 })
93 
94 # define lockdep_hrtimer_exit(__expires_hardirq)	\
95 	do {						\
96 		if (!__expires_hardirq)			\
97 			current->irq_config = 0;	\
98 	} while (0)
99 
100 # define lockdep_posixtimer_enter()				\
101 	  do {							\
102 		  current->irq_config = 1;			\
103 	  } while (0)
104 
105 # define lockdep_posixtimer_exit()				\
106 	  do {							\
107 		  current->irq_config = 0;			\
108 	  } while (0)
109 
110 # define lockdep_irq_work_enter(_flags)					\
111 	  do {								\
112 		  if (!((_flags) & IRQ_WORK_HARD_IRQ))			\
113 			current->irq_config = 1;			\
114 	  } while (0)
115 # define lockdep_irq_work_exit(_flags)					\
116 	  do {								\
117 		  if (!((_flags) & IRQ_WORK_HARD_IRQ))			\
118 			current->irq_config = 0;			\
119 	  } while (0)
120 
121 #else
122 # define trace_hardirqs_on_prepare()		do { } while (0)
123 # define trace_hardirqs_off_finish()		do { } while (0)
124 # define trace_hardirqs_on()			do { } while (0)
125 # define trace_hardirqs_off()			do { } while (0)
126 # define lockdep_hardirq_context()		0
127 # define lockdep_softirq_context(p)		0
128 # define lockdep_hardirqs_enabled()		0
129 # define lockdep_softirqs_enabled(p)		0
130 # define lockdep_hardirq_enter()		do { } while (0)
131 # define lockdep_hardirq_threaded()		do { } while (0)
132 # define lockdep_hardirq_exit()			do { } while (0)
133 # define lockdep_softirq_enter()		do { } while (0)
134 # define lockdep_softirq_exit()			do { } while (0)
135 # define lockdep_hrtimer_enter(__hrtimer)	false
136 # define lockdep_hrtimer_exit(__context)	do { } while (0)
137 # define lockdep_posixtimer_enter()		do { } while (0)
138 # define lockdep_posixtimer_exit()		do { } while (0)
139 # define lockdep_irq_work_enter(__work)		do { } while (0)
140 # define lockdep_irq_work_exit(__work)		do { } while (0)
141 #endif
142 
143 #if defined(CONFIG_IRQSOFF_TRACER) || \
144 	defined(CONFIG_PREEMPT_TRACER)
145  extern void stop_critical_timings(void);
146  extern void start_critical_timings(void);
147 #else
148 # define stop_critical_timings() do { } while (0)
149 # define start_critical_timings() do { } while (0)
150 #endif
151 
152 #ifdef CONFIG_DEBUG_IRQFLAGS
153 extern void warn_bogus_irq_restore(void);
154 #define raw_check_bogus_irq_restore()			\
155 	do {						\
156 		if (unlikely(!arch_irqs_disabled()))	\
157 			warn_bogus_irq_restore();	\
158 	} while (0)
159 #else
160 #define raw_check_bogus_irq_restore() do { } while (0)
161 #endif
162 
163 /*
164  * Wrap the arch provided IRQ routines to provide appropriate checks.
165  */
166 #define raw_local_irq_disable()		arch_local_irq_disable()
167 #define raw_local_irq_enable()		arch_local_irq_enable()
168 #define raw_local_irq_save(flags)			\
169 	do {						\
170 		typecheck(unsigned long, flags);	\
171 		flags = arch_local_irq_save();		\
172 	} while (0)
173 #define raw_local_irq_restore(flags)			\
174 	do {						\
175 		typecheck(unsigned long, flags);	\
176 		raw_check_bogus_irq_restore();		\
177 		arch_local_irq_restore(flags);		\
178 	} while (0)
179 #define raw_local_save_flags(flags)			\
180 	do {						\
181 		typecheck(unsigned long, flags);	\
182 		flags = arch_local_save_flags();	\
183 	} while (0)
184 #define raw_irqs_disabled_flags(flags)			\
185 	({						\
186 		typecheck(unsigned long, flags);	\
187 		arch_irqs_disabled_flags(flags);	\
188 	})
189 #define raw_irqs_disabled()		(arch_irqs_disabled())
190 #define raw_safe_halt()			arch_safe_halt()
191 
192 /*
193  * The local_irq_*() APIs are equal to the raw_local_irq*()
194  * if !TRACE_IRQFLAGS.
195  */
196 #ifdef CONFIG_TRACE_IRQFLAGS
197 
198 #define local_irq_enable()				\
199 	do {						\
200 		trace_hardirqs_on();			\
201 		raw_local_irq_enable();			\
202 	} while (0)
203 
204 #define local_irq_disable()				\
205 	do {						\
206 		bool was_disabled = raw_irqs_disabled();\
207 		raw_local_irq_disable();		\
208 		if (!was_disabled)			\
209 			trace_hardirqs_off();		\
210 	} while (0)
211 
212 #define local_irq_save(flags)				\
213 	do {						\
214 		raw_local_irq_save(flags);		\
215 		if (!raw_irqs_disabled_flags(flags))	\
216 			trace_hardirqs_off();		\
217 	} while (0)
218 
219 #define local_irq_restore(flags)			\
220 	do {						\
221 		if (!raw_irqs_disabled_flags(flags))	\
222 			trace_hardirqs_on();		\
223 		raw_local_irq_restore(flags);		\
224 	} while (0)
225 
226 #define safe_halt()				\
227 	do {					\
228 		trace_hardirqs_on();		\
229 		raw_safe_halt();		\
230 	} while (0)
231 
232 
233 #else /* !CONFIG_TRACE_IRQFLAGS */
234 
235 #define local_irq_enable()	do { raw_local_irq_enable(); } while (0)
236 #define local_irq_disable()	do { raw_local_irq_disable(); } while (0)
237 #define local_irq_save(flags)	do { raw_local_irq_save(flags); } while (0)
238 #define local_irq_restore(flags) do { raw_local_irq_restore(flags); } while (0)
239 #define safe_halt()		do { raw_safe_halt(); } while (0)
240 
241 #endif /* CONFIG_TRACE_IRQFLAGS */
242 
243 #define local_save_flags(flags)	raw_local_save_flags(flags)
244 
245 /*
246  * Some architectures don't define arch_irqs_disabled(), so even if either
247  * definition would be fine we need to use different ones for the time being
248  * to avoid build issues.
249  */
250 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
251 #define irqs_disabled()					\
252 	({						\
253 		unsigned long _flags;			\
254 		raw_local_save_flags(_flags);		\
255 		raw_irqs_disabled_flags(_flags);	\
256 	})
257 #else /* !CONFIG_TRACE_IRQFLAGS_SUPPORT */
258 #define irqs_disabled()	raw_irqs_disabled()
259 #endif /* CONFIG_TRACE_IRQFLAGS_SUPPORT */
260 
261 #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
262 
263 #endif
264