xref: /linux-6.15/include/linux/hardirq.h (revision e11da5b4)
1 #ifndef LINUX_HARDIRQ_H
2 #define LINUX_HARDIRQ_H
3 
4 #include <linux/preempt.h>
5 #ifdef CONFIG_PREEMPT
6 #include <linux/smp_lock.h>
7 #endif
8 #include <linux/lockdep.h>
9 #include <linux/ftrace_irq.h>
10 #include <asm/hardirq.h>
11 
12 /*
13  * We put the hardirq and softirq counter into the preemption
14  * counter. The bitmask has the following meaning:
15  *
16  * - bits 0-7 are the preemption count (max preemption depth: 256)
17  * - bits 8-15 are the softirq count (max # of softirqs: 256)
18  *
19  * The hardirq count can in theory reach the same as NR_IRQS.
20  * In reality, the number of nested IRQS is limited to the stack
21  * size as well. For archs with over 1000 IRQS it is not practical
22  * to expect that they will all nest. We give a max of 10 bits for
23  * hardirq nesting. An arch may choose to give less than 10 bits.
24  * m68k expects it to be 8.
25  *
26  * - bits 16-25 are the hardirq count (max # of nested hardirqs: 1024)
27  * - bit 26 is the NMI_MASK
28  * - bit 28 is the PREEMPT_ACTIVE flag
29  *
30  * PREEMPT_MASK: 0x000000ff
31  * SOFTIRQ_MASK: 0x0000ff00
32  * HARDIRQ_MASK: 0x03ff0000
33  *     NMI_MASK: 0x04000000
34  */
35 #define PREEMPT_BITS	8
36 #define SOFTIRQ_BITS	8
37 #define NMI_BITS	1
38 
39 #define MAX_HARDIRQ_BITS 10
40 
41 #ifndef HARDIRQ_BITS
42 # define HARDIRQ_BITS	MAX_HARDIRQ_BITS
43 #endif
44 
45 #if HARDIRQ_BITS > MAX_HARDIRQ_BITS
46 #error HARDIRQ_BITS too high!
47 #endif
48 
49 #define PREEMPT_SHIFT	0
50 #define SOFTIRQ_SHIFT	(PREEMPT_SHIFT + PREEMPT_BITS)
51 #define HARDIRQ_SHIFT	(SOFTIRQ_SHIFT + SOFTIRQ_BITS)
52 #define NMI_SHIFT	(HARDIRQ_SHIFT + HARDIRQ_BITS)
53 
54 #define __IRQ_MASK(x)	((1UL << (x))-1)
55 
56 #define PREEMPT_MASK	(__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
57 #define SOFTIRQ_MASK	(__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
58 #define HARDIRQ_MASK	(__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
59 #define NMI_MASK	(__IRQ_MASK(NMI_BITS)     << NMI_SHIFT)
60 
61 #define PREEMPT_OFFSET	(1UL << PREEMPT_SHIFT)
62 #define SOFTIRQ_OFFSET	(1UL << SOFTIRQ_SHIFT)
63 #define HARDIRQ_OFFSET	(1UL << HARDIRQ_SHIFT)
64 #define NMI_OFFSET	(1UL << NMI_SHIFT)
65 
66 #define SOFTIRQ_DISABLE_OFFSET	(2 * SOFTIRQ_OFFSET)
67 
68 #ifndef PREEMPT_ACTIVE
69 #define PREEMPT_ACTIVE_BITS	1
70 #define PREEMPT_ACTIVE_SHIFT	(NMI_SHIFT + NMI_BITS)
71 #define PREEMPT_ACTIVE	(__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
72 #endif
73 
74 #if PREEMPT_ACTIVE < (1 << (NMI_SHIFT + NMI_BITS))
75 #error PREEMPT_ACTIVE is too low!
76 #endif
77 
78 #define hardirq_count()	(preempt_count() & HARDIRQ_MASK)
79 #define softirq_count()	(preempt_count() & SOFTIRQ_MASK)
80 #define irq_count()	(preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
81 				 | NMI_MASK))
82 
83 /*
84  * Are we doing bottom half or hardware interrupt processing?
85  * Are we in a softirq context? Interrupt context?
86  * in_softirq - Are we currently processing softirq or have bh disabled?
87  * in_serving_softirq - Are we currently processing softirq?
88  */
89 #define in_irq()		(hardirq_count())
90 #define in_softirq()		(softirq_count())
91 #define in_interrupt()		(irq_count())
92 #define in_serving_softirq()	(softirq_count() & SOFTIRQ_OFFSET)
93 
94 /*
95  * Are we in NMI context?
96  */
97 #define in_nmi()	(preempt_count() & NMI_MASK)
98 
99 #if defined(CONFIG_PREEMPT)
100 # define PREEMPT_INATOMIC_BASE kernel_locked()
101 # define PREEMPT_CHECK_OFFSET 1
102 #else
103 # define PREEMPT_INATOMIC_BASE 0
104 # define PREEMPT_CHECK_OFFSET 0
105 #endif
106 
107 /*
108  * Are we running in atomic context?  WARNING: this macro cannot
109  * always detect atomic context; in particular, it cannot know about
110  * held spinlocks in non-preemptible kernels.  Thus it should not be
111  * used in the general case to determine whether sleeping is possible.
112  * Do not use in_atomic() in driver code.
113  */
114 #define in_atomic()	((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_INATOMIC_BASE)
115 
116 /*
117  * Check whether we were atomic before we did preempt_disable():
118  * (used by the scheduler, *after* releasing the kernel lock)
119  */
120 #define in_atomic_preempt_off() \
121 		((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_CHECK_OFFSET)
122 
123 #ifdef CONFIG_PREEMPT
124 # define preemptible()	(preempt_count() == 0 && !irqs_disabled())
125 # define IRQ_EXIT_OFFSET (HARDIRQ_OFFSET-1)
126 #else
127 # define preemptible()	0
128 # define IRQ_EXIT_OFFSET HARDIRQ_OFFSET
129 #endif
130 
131 #if defined(CONFIG_SMP) || defined(CONFIG_GENERIC_HARDIRQS)
132 extern void synchronize_irq(unsigned int irq);
133 #else
134 # define synchronize_irq(irq)	barrier()
135 #endif
136 
137 struct task_struct;
138 
139 #if !defined(CONFIG_VIRT_CPU_ACCOUNTING) && !defined(CONFIG_IRQ_TIME_ACCOUNTING)
140 static inline void account_system_vtime(struct task_struct *tsk)
141 {
142 }
143 #else
144 extern void account_system_vtime(struct task_struct *tsk);
145 #endif
146 
147 #if defined(CONFIG_NO_HZ)
148 #if defined(CONFIG_TINY_RCU) || defined(CONFIG_TINY_PREEMPT_RCU)
149 extern void rcu_enter_nohz(void);
150 extern void rcu_exit_nohz(void);
151 
152 static inline void rcu_irq_enter(void)
153 {
154 	rcu_exit_nohz();
155 }
156 
157 static inline void rcu_irq_exit(void)
158 {
159 	rcu_enter_nohz();
160 }
161 
162 static inline void rcu_nmi_enter(void)
163 {
164 }
165 
166 static inline void rcu_nmi_exit(void)
167 {
168 }
169 
170 #else
171 extern void rcu_irq_enter(void);
172 extern void rcu_irq_exit(void);
173 extern void rcu_nmi_enter(void);
174 extern void rcu_nmi_exit(void);
175 #endif
176 #else
177 # define rcu_irq_enter() do { } while (0)
178 # define rcu_irq_exit() do { } while (0)
179 # define rcu_nmi_enter() do { } while (0)
180 # define rcu_nmi_exit() do { } while (0)
181 #endif /* #if defined(CONFIG_NO_HZ) */
182 
183 /*
184  * It is safe to do non-atomic ops on ->hardirq_context,
185  * because NMI handlers may not preempt and the ops are
186  * always balanced, so the interrupted value of ->hardirq_context
187  * will always be restored.
188  */
189 #define __irq_enter()					\
190 	do {						\
191 		account_system_vtime(current);		\
192 		add_preempt_count(HARDIRQ_OFFSET);	\
193 		trace_hardirq_enter();			\
194 	} while (0)
195 
196 /*
197  * Enter irq context (on NO_HZ, update jiffies):
198  */
199 extern void irq_enter(void);
200 
201 /*
202  * Exit irq context without processing softirqs:
203  */
204 #define __irq_exit()					\
205 	do {						\
206 		trace_hardirq_exit();			\
207 		account_system_vtime(current);		\
208 		sub_preempt_count(HARDIRQ_OFFSET);	\
209 	} while (0)
210 
211 /*
212  * Exit irq context and process softirqs if needed:
213  */
214 extern void irq_exit(void);
215 
216 #define nmi_enter()						\
217 	do {							\
218 		ftrace_nmi_enter();				\
219 		BUG_ON(in_nmi());				\
220 		add_preempt_count(NMI_OFFSET + HARDIRQ_OFFSET);	\
221 		lockdep_off();					\
222 		rcu_nmi_enter();				\
223 		trace_hardirq_enter();				\
224 	} while (0)
225 
226 #define nmi_exit()						\
227 	do {							\
228 		trace_hardirq_exit();				\
229 		rcu_nmi_exit();					\
230 		lockdep_on();					\
231 		BUG_ON(!in_nmi());				\
232 		sub_preempt_count(NMI_OFFSET + HARDIRQ_OFFSET);	\
233 		ftrace_nmi_exit();				\
234 	} while (0)
235 
236 #endif /* LINUX_HARDIRQ_H */
237