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