1 /* SPDX-License-Identifier: GPL-2.0 */
3 * include/linux/irqflags.h
5 * IRQ flags tracing: follow the state of the hardirq and softirq flags and
6 * provide callbacks for transitions between ON and OFF states.
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.
12 #ifndef _LINUX_TRACE_IRQFLAGS_H
13 #define _LINUX_TRACE_IRQFLAGS_H
15 #include <linux/irqflags_types.h>
16 #include <linux/typecheck.h>
17 #include <linux/cleanup.h>
18 #include <asm/irqflags.h>
19 #include <asm/percpu.h>
23 /* Currently lockdep_softirqs_on/off is used only by lockdep */
24 #ifdef CONFIG_PROVE_LOCKING
25 extern void lockdep_softirqs_on(unsigned long ip
);
26 extern void lockdep_softirqs_off(unsigned long ip
);
27 extern void lockdep_hardirqs_on_prepare(void);
28 extern void lockdep_hardirqs_on(unsigned long ip
);
29 extern void lockdep_hardirqs_off(unsigned long ip
);
30 extern void lockdep_cleanup_dead_cpu(unsigned int cpu
,
31 struct task_struct
*idle
);
33 static inline void lockdep_softirqs_on(unsigned long ip
) { }
34 static inline void lockdep_softirqs_off(unsigned long ip
) { }
35 static inline void lockdep_hardirqs_on_prepare(void) { }
36 static inline void lockdep_hardirqs_on(unsigned long ip
) { }
37 static inline void lockdep_hardirqs_off(unsigned long ip
) { }
38 static inline void lockdep_cleanup_dead_cpu(unsigned int cpu
,
39 struct task_struct
*idle
) {}
42 #ifdef CONFIG_TRACE_IRQFLAGS
44 DECLARE_PER_CPU(int, hardirqs_enabled
);
45 DECLARE_PER_CPU(int, hardirq_context
);
47 extern void trace_hardirqs_on_prepare(void);
48 extern void trace_hardirqs_off_finish(void);
49 extern void trace_hardirqs_on(void);
50 extern void trace_hardirqs_off(void);
52 # define lockdep_hardirq_context() (raw_cpu_read(hardirq_context))
53 # define lockdep_softirq_context(p) ((p)->softirq_context)
54 # define lockdep_hardirqs_enabled() (this_cpu_read(hardirqs_enabled))
55 # define lockdep_softirqs_enabled(p) ((p)->softirqs_enabled)
56 # define lockdep_hardirq_enter() \
58 if (__this_cpu_inc_return(hardirq_context) == 1)\
59 current->hardirq_threaded = 0; \
61 # define lockdep_hardirq_threaded() \
63 current->hardirq_threaded = 1; \
65 # define lockdep_hardirq_exit() \
67 __this_cpu_dec(hardirq_context); \
70 # define lockdep_hrtimer_enter(__hrtimer) \
72 bool __expires_hardirq = true; \
74 if (!__hrtimer->is_hard) { \
75 current->irq_config = 1; \
76 __expires_hardirq = false; \
81 # define lockdep_hrtimer_exit(__expires_hardirq) \
83 if (!__expires_hardirq) \
84 current->irq_config = 0; \
87 # define lockdep_posixtimer_enter() \
89 current->irq_config = 1; \
92 # define lockdep_posixtimer_exit() \
94 current->irq_config = 0; \
97 # define lockdep_irq_work_enter(_flags) \
99 if (!((_flags) & IRQ_WORK_HARD_IRQ)) \
100 current->irq_config = 1; \
102 # define lockdep_irq_work_exit(_flags) \
104 if (!((_flags) & IRQ_WORK_HARD_IRQ)) \
105 current->irq_config = 0; \
109 # define trace_hardirqs_on_prepare() do { } while (0)
110 # define trace_hardirqs_off_finish() do { } while (0)
111 # define trace_hardirqs_on() do { } while (0)
112 # define trace_hardirqs_off() do { } while (0)
113 # define lockdep_hardirq_context() 0
114 # define lockdep_softirq_context(p) 0
115 # define lockdep_hardirqs_enabled() 0
116 # define lockdep_softirqs_enabled(p) 0
117 # define lockdep_hardirq_enter() do { } while (0)
118 # define lockdep_hardirq_threaded() do { } while (0)
119 # define lockdep_hardirq_exit() do { } while (0)
120 # define lockdep_softirq_enter() do { } while (0)
121 # define lockdep_softirq_exit() do { } while (0)
122 # define lockdep_hrtimer_enter(__hrtimer) false
123 # define lockdep_hrtimer_exit(__context) do { (void)(__context); } while (0)
124 # define lockdep_posixtimer_enter() do { } while (0)
125 # define lockdep_posixtimer_exit() do { } while (0)
126 # define lockdep_irq_work_enter(__work) do { } while (0)
127 # define lockdep_irq_work_exit(__work) do { } while (0)
130 #if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT)
131 # define lockdep_softirq_enter() \
133 current->softirq_context++; \
135 # define lockdep_softirq_exit() \
137 current->softirq_context--; \
141 # define lockdep_softirq_enter() do { } while (0)
142 # define lockdep_softirq_exit() do { } while (0)
145 #if defined(CONFIG_IRQSOFF_TRACER) || \
146 defined(CONFIG_PREEMPT_TRACER)
147 extern void stop_critical_timings(void);
148 extern void start_critical_timings(void);
150 # define stop_critical_timings() do { } while (0)
151 # define start_critical_timings() do { } while (0)
154 #ifdef CONFIG_DEBUG_IRQFLAGS
155 extern void warn_bogus_irq_restore(void);
156 #define raw_check_bogus_irq_restore() \
158 if (unlikely(!arch_irqs_disabled())) \
159 warn_bogus_irq_restore(); \
162 #define raw_check_bogus_irq_restore() do { } while (0)
166 * Wrap the arch provided IRQ routines to provide appropriate checks.
168 #define raw_local_irq_disable() arch_local_irq_disable()
169 #define raw_local_irq_enable() arch_local_irq_enable()
170 #define raw_local_irq_save(flags) \
172 typecheck(unsigned long, flags); \
173 flags = arch_local_irq_save(); \
175 #define raw_local_irq_restore(flags) \
177 typecheck(unsigned long, flags); \
178 raw_check_bogus_irq_restore(); \
179 arch_local_irq_restore(flags); \
181 #define raw_local_save_flags(flags) \
183 typecheck(unsigned long, flags); \
184 flags = arch_local_save_flags(); \
186 #define raw_irqs_disabled_flags(flags) \
188 typecheck(unsigned long, flags); \
189 arch_irqs_disabled_flags(flags); \
191 #define raw_irqs_disabled() (arch_irqs_disabled())
192 #define raw_safe_halt() arch_safe_halt()
195 * The local_irq_*() APIs are equal to the raw_local_irq*()
196 * if !TRACE_IRQFLAGS.
198 #ifdef CONFIG_TRACE_IRQFLAGS
200 #define local_irq_enable() \
202 trace_hardirqs_on(); \
203 raw_local_irq_enable(); \
206 #define local_irq_disable() \
208 bool was_disabled = raw_irqs_disabled();\
209 raw_local_irq_disable(); \
211 trace_hardirqs_off(); \
214 #define local_irq_save(flags) \
216 raw_local_irq_save(flags); \
217 if (!raw_irqs_disabled_flags(flags)) \
218 trace_hardirqs_off(); \
221 #define local_irq_restore(flags) \
223 if (!raw_irqs_disabled_flags(flags)) \
224 trace_hardirqs_on(); \
225 raw_local_irq_restore(flags); \
228 #define safe_halt() \
230 trace_hardirqs_on(); \
235 #else /* !CONFIG_TRACE_IRQFLAGS */
237 #define local_irq_enable() do { raw_local_irq_enable(); } while (0)
238 #define local_irq_disable() do { raw_local_irq_disable(); } while (0)
239 #define local_irq_save(flags) do { raw_local_irq_save(flags); } while (0)
240 #define local_irq_restore(flags) do { raw_local_irq_restore(flags); } while (0)
241 #define safe_halt() do { raw_safe_halt(); } while (0)
243 #endif /* CONFIG_TRACE_IRQFLAGS */
245 #define local_save_flags(flags) raw_local_save_flags(flags)
248 * Some architectures don't define arch_irqs_disabled(), so even if either
249 * definition would be fine we need to use different ones for the time being
250 * to avoid build issues.
252 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
253 #define irqs_disabled() \
255 unsigned long _flags; \
256 raw_local_save_flags(_flags); \
257 raw_irqs_disabled_flags(_flags); \
259 #else /* !CONFIG_TRACE_IRQFLAGS_SUPPORT */
260 #define irqs_disabled() raw_irqs_disabled()
261 #endif /* CONFIG_TRACE_IRQFLAGS_SUPPORT */
263 #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
265 DEFINE_LOCK_GUARD_0(irq
, local_irq_disable(), local_irq_enable())
266 DEFINE_LOCK_GUARD_0(irqsave
,
267 local_irq_save(_T
->flags
),
268 local_irq_restore(_T
->flags
),