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