]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef __LINUX_PREEMPT_H |
3 | #define __LINUX_PREEMPT_H | |
4 | ||
5 | /* | |
6 | * include/linux/preempt.h - macros for accessing and manipulating | |
7 | * preempt_count (used for kernel preemption, interrupt count, etc.) | |
8 | */ | |
9 | ||
1da177e4 | 10 | #include <linux/linkage.h> |
e107be36 | 11 | #include <linux/list.h> |
1da177e4 | 12 | |
92cf2118 FW |
13 | /* |
14 | * We put the hardirq and softirq counter into the preemption | |
15 | * counter. The bitmask has the following meaning: | |
16 | * | |
17 | * - bits 0-7 are the preemption count (max preemption depth: 256) | |
18 | * - bits 8-15 are the softirq count (max # of softirqs: 256) | |
19 | * | |
20 | * The hardirq count could in theory be the same as the number of | |
21 | * interrupts in the system, but we run all interrupt handlers with | |
22 | * interrupts disabled, so we cannot have nesting interrupts. Though | |
23 | * there are a few palaeontologic drivers which reenable interrupts in | |
24 | * the handler, so we need more than one bit here. | |
25 | * | |
2e10e71c FW |
26 | * PREEMPT_MASK: 0x000000ff |
27 | * SOFTIRQ_MASK: 0x0000ff00 | |
28 | * HARDIRQ_MASK: 0x000f0000 | |
29 | * NMI_MASK: 0x00100000 | |
2e10e71c | 30 | * PREEMPT_NEED_RESCHED: 0x80000000 |
92cf2118 FW |
31 | */ |
32 | #define PREEMPT_BITS 8 | |
33 | #define SOFTIRQ_BITS 8 | |
34 | #define HARDIRQ_BITS 4 | |
35 | #define NMI_BITS 1 | |
36 | ||
37 | #define PREEMPT_SHIFT 0 | |
38 | #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS) | |
39 | #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS) | |
40 | #define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS) | |
41 | ||
42 | #define __IRQ_MASK(x) ((1UL << (x))-1) | |
43 | ||
44 | #define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT) | |
45 | #define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT) | |
46 | #define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT) | |
47 | #define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT) | |
48 | ||
49 | #define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT) | |
50 | #define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT) | |
51 | #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT) | |
52 | #define NMI_OFFSET (1UL << NMI_SHIFT) | |
53 | ||
54 | #define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET) | |
55 | ||
2e10e71c FW |
56 | /* We use the MSB mostly because its available */ |
57 | #define PREEMPT_NEED_RESCHED 0x80000000 | |
58 | ||
d04b0ad3 IM |
59 | #define PREEMPT_DISABLED (PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED) |
60 | ||
61 | /* | |
62 | * Disable preemption until the scheduler is running -- use an unconditional | |
63 | * value so that it also works on !PREEMPT_COUNT kernels. | |
64 | * | |
65 | * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count(). | |
66 | */ | |
67 | #define INIT_PREEMPT_COUNT PREEMPT_OFFSET | |
68 | ||
69 | /* | |
70 | * Initial preempt_count value; reflects the preempt_count schedule invariant | |
71 | * which states that during context switches: | |
72 | * | |
73 | * preempt_count() == 2*PREEMPT_DISABLE_OFFSET | |
74 | * | |
75 | * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels. | |
76 | * Note: See finish_task_switch(). | |
77 | */ | |
78 | #define FORK_PREEMPT_COUNT (2*PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED) | |
79 | ||
2e10e71c FW |
80 | /* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */ |
81 | #include <asm/preempt.h> | |
82 | ||
92cf2118 FW |
83 | #define hardirq_count() (preempt_count() & HARDIRQ_MASK) |
84 | #define softirq_count() (preempt_count() & SOFTIRQ_MASK) | |
85 | #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \ | |
86 | | NMI_MASK)) | |
87 | ||
88 | /* | |
89 | * Are we doing bottom half or hardware interrupt processing? | |
7c478895 PZ |
90 | * |
91 | * in_irq() - We're in (hard) IRQ context | |
92 | * in_softirq() - We have BH disabled, or are processing softirqs | |
93 | * in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled | |
94 | * in_serving_softirq() - We're in softirq context | |
95 | * in_nmi() - We're in NMI context | |
96 | * in_task() - We're in task context | |
97 | * | |
98 | * Note: due to the BH disabled confusion: in_softirq(),in_interrupt() really | |
99 | * should not be used in new code. | |
92cf2118 FW |
100 | */ |
101 | #define in_irq() (hardirq_count()) | |
102 | #define in_softirq() (softirq_count()) | |
103 | #define in_interrupt() (irq_count()) | |
104 | #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET) | |
7c478895 PZ |
105 | #define in_nmi() (preempt_count() & NMI_MASK) |
106 | #define in_task() (!(preempt_count() & \ | |
107 | (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET))) | |
92cf2118 | 108 | |
fe32d3cd KK |
109 | /* |
110 | * The preempt_count offset after preempt_disable(); | |
111 | */ | |
92cf2118 | 112 | #if defined(CONFIG_PREEMPT_COUNT) |
fe32d3cd | 113 | # define PREEMPT_DISABLE_OFFSET PREEMPT_OFFSET |
92cf2118 | 114 | #else |
fe32d3cd | 115 | # define PREEMPT_DISABLE_OFFSET 0 |
92cf2118 FW |
116 | #endif |
117 | ||
fe32d3cd KK |
118 | /* |
119 | * The preempt_count offset after spin_lock() | |
120 | */ | |
121 | #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET | |
122 | ||
92cf2118 FW |
123 | /* |
124 | * The preempt_count offset needed for things like: | |
125 | * | |
126 | * spin_lock_bh() | |
127 | * | |
128 | * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and | |
129 | * softirqs, such that unlock sequences of: | |
130 | * | |
131 | * spin_unlock(); | |
132 | * local_bh_enable(); | |
133 | * | |
134 | * Work as expected. | |
135 | */ | |
fe32d3cd | 136 | #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET) |
92cf2118 FW |
137 | |
138 | /* | |
139 | * Are we running in atomic context? WARNING: this macro cannot | |
140 | * always detect atomic context; in particular, it cannot know about | |
141 | * held spinlocks in non-preemptible kernels. Thus it should not be | |
142 | * used in the general case to determine whether sleeping is possible. | |
143 | * Do not use in_atomic() in driver code. | |
144 | */ | |
3e51f3c4 | 145 | #define in_atomic() (preempt_count() != 0) |
92cf2118 FW |
146 | |
147 | /* | |
148 | * Check whether we were atomic before we did preempt_disable(): | |
e017cf21 | 149 | * (used by the scheduler) |
92cf2118 | 150 | */ |
da7142e2 | 151 | #define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET) |
92cf2118 | 152 | |
c3bc8fd6 | 153 | #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_TRACE_PREEMPT_TOGGLE) |
bdb43806 PZ |
154 | extern void preempt_count_add(int val); |
155 | extern void preempt_count_sub(int val); | |
fe32d3cd KK |
156 | #define preempt_count_dec_and_test() \ |
157 | ({ preempt_count_sub(1); should_resched(0); }) | |
1da177e4 | 158 | #else |
bdb43806 PZ |
159 | #define preempt_count_add(val) __preempt_count_add(val) |
160 | #define preempt_count_sub(val) __preempt_count_sub(val) | |
161 | #define preempt_count_dec_and_test() __preempt_count_dec_and_test() | |
1da177e4 LT |
162 | #endif |
163 | ||
bdb43806 PZ |
164 | #define __preempt_count_inc() __preempt_count_add(1) |
165 | #define __preempt_count_dec() __preempt_count_sub(1) | |
bdd4e85d | 166 | |
bdb43806 PZ |
167 | #define preempt_count_inc() preempt_count_add(1) |
168 | #define preempt_count_dec() preempt_count_sub(1) | |
bdd4e85d FW |
169 | |
170 | #ifdef CONFIG_PREEMPT_COUNT | |
171 | ||
1da177e4 LT |
172 | #define preempt_disable() \ |
173 | do { \ | |
bdb43806 | 174 | preempt_count_inc(); \ |
1da177e4 LT |
175 | barrier(); \ |
176 | } while (0) | |
177 | ||
ba74c144 | 178 | #define sched_preempt_enable_no_resched() \ |
1da177e4 LT |
179 | do { \ |
180 | barrier(); \ | |
bdb43806 | 181 | preempt_count_dec(); \ |
1da177e4 LT |
182 | } while (0) |
183 | ||
bdb43806 | 184 | #define preempt_enable_no_resched() sched_preempt_enable_no_resched() |
ba74c144 | 185 | |
2e10e71c FW |
186 | #define preemptible() (preempt_count() == 0 && !irqs_disabled()) |
187 | ||
bdb43806 | 188 | #ifdef CONFIG_PREEMPT |
1da177e4 LT |
189 | #define preempt_enable() \ |
190 | do { \ | |
bdb43806 PZ |
191 | barrier(); \ |
192 | if (unlikely(preempt_count_dec_and_test())) \ | |
1a338ac3 | 193 | __preempt_schedule(); \ |
1da177e4 LT |
194 | } while (0) |
195 | ||
9a92e3dc FW |
196 | #define preempt_enable_notrace() \ |
197 | do { \ | |
198 | barrier(); \ | |
199 | if (unlikely(__preempt_count_dec_and_test())) \ | |
200 | __preempt_schedule_notrace(); \ | |
201 | } while (0) | |
202 | ||
bdb43806 PZ |
203 | #define preempt_check_resched() \ |
204 | do { \ | |
fe32d3cd | 205 | if (should_resched(0)) \ |
1a338ac3 | 206 | __preempt_schedule(); \ |
bdb43806 PZ |
207 | } while (0) |
208 | ||
9a92e3dc | 209 | #else /* !CONFIG_PREEMPT */ |
62b94a08 PZ |
210 | #define preempt_enable() \ |
211 | do { \ | |
212 | barrier(); \ | |
213 | preempt_count_dec(); \ | |
214 | } while (0) | |
50282528 | 215 | |
9a92e3dc | 216 | #define preempt_enable_notrace() \ |
50282528 SR |
217 | do { \ |
218 | barrier(); \ | |
bdb43806 | 219 | __preempt_count_dec(); \ |
50282528 SR |
220 | } while (0) |
221 | ||
9a92e3dc FW |
222 | #define preempt_check_resched() do { } while (0) |
223 | #endif /* CONFIG_PREEMPT */ | |
bdb43806 | 224 | |
9a92e3dc | 225 | #define preempt_disable_notrace() \ |
50282528 | 226 | do { \ |
9a92e3dc | 227 | __preempt_count_inc(); \ |
bdb43806 | 228 | barrier(); \ |
50282528 | 229 | } while (0) |
9a92e3dc FW |
230 | |
231 | #define preempt_enable_no_resched_notrace() \ | |
62b94a08 PZ |
232 | do { \ |
233 | barrier(); \ | |
234 | __preempt_count_dec(); \ | |
235 | } while (0) | |
50282528 | 236 | |
bdd4e85d | 237 | #else /* !CONFIG_PREEMPT_COUNT */ |
1da177e4 | 238 | |
386afc91 LT |
239 | /* |
240 | * Even if we don't have any preemption, we need preempt disable/enable | |
241 | * to be barriers, so that we don't have things like get_user/put_user | |
242 | * that can cause faults and scheduling migrate into our preempt-protected | |
243 | * region. | |
244 | */ | |
bdb43806 | 245 | #define preempt_disable() barrier() |
386afc91 | 246 | #define sched_preempt_enable_no_resched() barrier() |
bdb43806 PZ |
247 | #define preempt_enable_no_resched() barrier() |
248 | #define preempt_enable() barrier() | |
249 | #define preempt_check_resched() do { } while (0) | |
386afc91 LT |
250 | |
251 | #define preempt_disable_notrace() barrier() | |
252 | #define preempt_enable_no_resched_notrace() barrier() | |
253 | #define preempt_enable_notrace() barrier() | |
2e10e71c | 254 | #define preemptible() 0 |
50282528 | 255 | |
bdd4e85d | 256 | #endif /* CONFIG_PREEMPT_COUNT */ |
1da177e4 | 257 | |
62b94a08 PZ |
258 | #ifdef MODULE |
259 | /* | |
260 | * Modules have no business playing preemption tricks. | |
261 | */ | |
262 | #undef sched_preempt_enable_no_resched | |
263 | #undef preempt_enable_no_resched | |
264 | #undef preempt_enable_no_resched_notrace | |
265 | #undef preempt_check_resched | |
266 | #endif | |
267 | ||
8cb75e0c PZ |
268 | #define preempt_set_need_resched() \ |
269 | do { \ | |
270 | set_preempt_need_resched(); \ | |
271 | } while (0) | |
272 | #define preempt_fold_need_resched() \ | |
273 | do { \ | |
274 | if (tif_need_resched()) \ | |
275 | set_preempt_need_resched(); \ | |
276 | } while (0) | |
8cb75e0c | 277 | |
e107be36 AK |
278 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
279 | ||
280 | struct preempt_notifier; | |
281 | ||
282 | /** | |
283 | * preempt_ops - notifiers called when a task is preempted and rescheduled | |
284 | * @sched_in: we're about to be rescheduled: | |
285 | * notifier: struct preempt_notifier for the task being scheduled | |
286 | * cpu: cpu we're scheduled on | |
287 | * @sched_out: we've just been preempted | |
288 | * notifier: struct preempt_notifier for the task being preempted | |
289 | * next: the task that's kicking us out | |
8592e648 TH |
290 | * |
291 | * Please note that sched_in and out are called under different | |
292 | * contexts. sched_out is called with rq lock held and irq disabled | |
293 | * while sched_in is called without rq lock and irq enabled. This | |
294 | * difference is intentional and depended upon by its users. | |
e107be36 AK |
295 | */ |
296 | struct preempt_ops { | |
297 | void (*sched_in)(struct preempt_notifier *notifier, int cpu); | |
298 | void (*sched_out)(struct preempt_notifier *notifier, | |
299 | struct task_struct *next); | |
300 | }; | |
301 | ||
302 | /** | |
303 | * preempt_notifier - key for installing preemption notifiers | |
304 | * @link: internal use | |
305 | * @ops: defines the notifier functions to be called | |
306 | * | |
307 | * Usually used in conjunction with container_of(). | |
308 | */ | |
309 | struct preempt_notifier { | |
310 | struct hlist_node link; | |
311 | struct preempt_ops *ops; | |
312 | }; | |
313 | ||
2ecd9d29 PZ |
314 | void preempt_notifier_inc(void); |
315 | void preempt_notifier_dec(void); | |
e107be36 AK |
316 | void preempt_notifier_register(struct preempt_notifier *notifier); |
317 | void preempt_notifier_unregister(struct preempt_notifier *notifier); | |
318 | ||
319 | static inline void preempt_notifier_init(struct preempt_notifier *notifier, | |
320 | struct preempt_ops *ops) | |
321 | { | |
322 | INIT_HLIST_NODE(¬ifier->link); | |
323 | notifier->ops = ops; | |
324 | } | |
325 | ||
326 | #endif | |
327 | ||
1da177e4 | 328 | #endif /* __LINUX_PREEMPT_H */ |