]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - include/linux/preempt.h
sched/preempt: Fix out of date comment
[mirror_ubuntu-hirsute-kernel.git] / include / linux / preempt.h
CommitLineData
1da177e4
LT
1#ifndef __LINUX_PREEMPT_H
2#define __LINUX_PREEMPT_H
3
4/*
5 * include/linux/preempt.h - macros for accessing and manipulating
6 * preempt_count (used for kernel preemption, interrupt count, etc.)
7 */
8
1da177e4 9#include <linux/linkage.h>
e107be36 10#include <linux/list.h>
1da177e4 11
92cf2118
FW
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 could in theory be the same as the number of
20 * interrupts in the system, but we run all interrupt handlers with
21 * interrupts disabled, so we cannot have nesting interrupts. Though
22 * there are a few palaeontologic drivers which reenable interrupts in
23 * the handler, so we need more than one bit here.
24 *
2e10e71c
FW
25 * PREEMPT_MASK: 0x000000ff
26 * SOFTIRQ_MASK: 0x0000ff00
27 * HARDIRQ_MASK: 0x000f0000
28 * NMI_MASK: 0x00100000
29 * PREEMPT_ACTIVE: 0x00200000
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
56#define PREEMPT_ACTIVE_BITS 1
57#define PREEMPT_ACTIVE_SHIFT (NMI_SHIFT + NMI_BITS)
58#define PREEMPT_ACTIVE (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
59
2e10e71c
FW
60/* We use the MSB mostly because its available */
61#define PREEMPT_NEED_RESCHED 0x80000000
62
63/* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */
64#include <asm/preempt.h>
65
92cf2118
FW
66#define hardirq_count() (preempt_count() & HARDIRQ_MASK)
67#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
68#define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
69 | NMI_MASK))
70
71/*
72 * Are we doing bottom half or hardware interrupt processing?
73 * Are we in a softirq context? Interrupt context?
74 * in_softirq - Are we currently processing softirq or have bh disabled?
75 * in_serving_softirq - Are we currently processing softirq?
76 */
77#define in_irq() (hardirq_count())
78#define in_softirq() (softirq_count())
79#define in_interrupt() (irq_count())
80#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
81
82/*
83 * Are we in NMI context?
84 */
85#define in_nmi() (preempt_count() & NMI_MASK)
86
87#if defined(CONFIG_PREEMPT_COUNT)
90b62b51 88# define PREEMPT_DISABLE_OFFSET 1
92cf2118 89#else
90b62b51 90# define PREEMPT_DISABLE_OFFSET 0
92cf2118
FW
91#endif
92
93/*
94 * The preempt_count offset needed for things like:
95 *
96 * spin_lock_bh()
97 *
98 * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
99 * softirqs, such that unlock sequences of:
100 *
101 * spin_unlock();
102 * local_bh_enable();
103 *
104 * Work as expected.
105 */
90b62b51 106#define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_DISABLE_OFFSET)
92cf2118
FW
107
108/*
109 * Are we running in atomic context? WARNING: this macro cannot
110 * always detect atomic context; in particular, it cannot know about
111 * held spinlocks in non-preemptible kernels. Thus it should not be
112 * used in the general case to determine whether sleeping is possible.
113 * Do not use in_atomic() in driver code.
114 */
115#define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != 0)
116
117/*
118 * Check whether we were atomic before we did preempt_disable():
e017cf21 119 * (used by the scheduler)
92cf2118
FW
120 */
121#define in_atomic_preempt_off() \
90b62b51 122 ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_DISABLE_OFFSET)
92cf2118 123
6cd8a4bb 124#if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
bdb43806
PZ
125extern void preempt_count_add(int val);
126extern void preempt_count_sub(int val);
127#define preempt_count_dec_and_test() ({ preempt_count_sub(1); should_resched(); })
1da177e4 128#else
bdb43806
PZ
129#define preempt_count_add(val) __preempt_count_add(val)
130#define preempt_count_sub(val) __preempt_count_sub(val)
131#define preempt_count_dec_and_test() __preempt_count_dec_and_test()
1da177e4
LT
132#endif
133
bdb43806
PZ
134#define __preempt_count_inc() __preempt_count_add(1)
135#define __preempt_count_dec() __preempt_count_sub(1)
bdd4e85d 136
bdb43806
PZ
137#define preempt_count_inc() preempt_count_add(1)
138#define preempt_count_dec() preempt_count_sub(1)
bdd4e85d 139
b30f0e3f
FW
140#define preempt_active_enter() \
141do { \
142 preempt_count_add(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET); \
143 barrier(); \
144} while (0)
145
146#define preempt_active_exit() \
147do { \
148 barrier(); \
149 preempt_count_sub(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET); \
150} while (0)
151
bdd4e85d
FW
152#ifdef CONFIG_PREEMPT_COUNT
153
1da177e4
LT
154#define preempt_disable() \
155do { \
bdb43806 156 preempt_count_inc(); \
1da177e4
LT
157 barrier(); \
158} while (0)
159
ba74c144 160#define sched_preempt_enable_no_resched() \
1da177e4
LT
161do { \
162 barrier(); \
bdb43806 163 preempt_count_dec(); \
1da177e4
LT
164} while (0)
165
bdb43806 166#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
ba74c144 167
2e10e71c
FW
168#define preemptible() (preempt_count() == 0 && !irqs_disabled())
169
bdb43806 170#ifdef CONFIG_PREEMPT
1da177e4
LT
171#define preempt_enable() \
172do { \
bdb43806
PZ
173 barrier(); \
174 if (unlikely(preempt_count_dec_and_test())) \
1a338ac3 175 __preempt_schedule(); \
1da177e4
LT
176} while (0)
177
bdb43806
PZ
178#define preempt_check_resched() \
179do { \
180 if (should_resched()) \
1a338ac3 181 __preempt_schedule(); \
bdb43806
PZ
182} while (0)
183
184#else
62b94a08
PZ
185#define preempt_enable() \
186do { \
187 barrier(); \
188 preempt_count_dec(); \
189} while (0)
bdb43806
PZ
190#define preempt_check_resched() do { } while (0)
191#endif
50282528
SR
192
193#define preempt_disable_notrace() \
194do { \
bdb43806 195 __preempt_count_inc(); \
50282528
SR
196 barrier(); \
197} while (0)
198
199#define preempt_enable_no_resched_notrace() \
200do { \
201 barrier(); \
bdb43806 202 __preempt_count_dec(); \
50282528
SR
203} while (0)
204
bdb43806
PZ
205#ifdef CONFIG_PREEMPT
206
1a338ac3
PZ
207#ifndef CONFIG_CONTEXT_TRACKING
208#define __preempt_schedule_context() __preempt_schedule()
bdb43806
PZ
209#endif
210
50282528
SR
211#define preempt_enable_notrace() \
212do { \
bdb43806
PZ
213 barrier(); \
214 if (unlikely(__preempt_count_dec_and_test())) \
1a338ac3 215 __preempt_schedule_context(); \
50282528 216} while (0)
bdb43806 217#else
62b94a08
PZ
218#define preempt_enable_notrace() \
219do { \
220 barrier(); \
221 __preempt_count_dec(); \
222} while (0)
bdb43806 223#endif
50282528 224
bdd4e85d 225#else /* !CONFIG_PREEMPT_COUNT */
1da177e4 226
386afc91
LT
227/*
228 * Even if we don't have any preemption, we need preempt disable/enable
229 * to be barriers, so that we don't have things like get_user/put_user
230 * that can cause faults and scheduling migrate into our preempt-protected
231 * region.
232 */
bdb43806 233#define preempt_disable() barrier()
386afc91 234#define sched_preempt_enable_no_resched() barrier()
bdb43806
PZ
235#define preempt_enable_no_resched() barrier()
236#define preempt_enable() barrier()
237#define preempt_check_resched() do { } while (0)
386afc91
LT
238
239#define preempt_disable_notrace() barrier()
240#define preempt_enable_no_resched_notrace() barrier()
241#define preempt_enable_notrace() barrier()
2e10e71c 242#define preemptible() 0
50282528 243
bdd4e85d 244#endif /* CONFIG_PREEMPT_COUNT */
1da177e4 245
62b94a08
PZ
246#ifdef MODULE
247/*
248 * Modules have no business playing preemption tricks.
249 */
250#undef sched_preempt_enable_no_resched
251#undef preempt_enable_no_resched
252#undef preempt_enable_no_resched_notrace
253#undef preempt_check_resched
254#endif
255
8cb75e0c
PZ
256#define preempt_set_need_resched() \
257do { \
258 set_preempt_need_resched(); \
259} while (0)
260#define preempt_fold_need_resched() \
261do { \
262 if (tif_need_resched()) \
263 set_preempt_need_resched(); \
264} while (0)
8cb75e0c 265
e107be36
AK
266#ifdef CONFIG_PREEMPT_NOTIFIERS
267
268struct preempt_notifier;
269
270/**
271 * preempt_ops - notifiers called when a task is preempted and rescheduled
272 * @sched_in: we're about to be rescheduled:
273 * notifier: struct preempt_notifier for the task being scheduled
274 * cpu: cpu we're scheduled on
275 * @sched_out: we've just been preempted
276 * notifier: struct preempt_notifier for the task being preempted
277 * next: the task that's kicking us out
8592e648
TH
278 *
279 * Please note that sched_in and out are called under different
280 * contexts. sched_out is called with rq lock held and irq disabled
281 * while sched_in is called without rq lock and irq enabled. This
282 * difference is intentional and depended upon by its users.
e107be36
AK
283 */
284struct preempt_ops {
285 void (*sched_in)(struct preempt_notifier *notifier, int cpu);
286 void (*sched_out)(struct preempt_notifier *notifier,
287 struct task_struct *next);
288};
289
290/**
291 * preempt_notifier - key for installing preemption notifiers
292 * @link: internal use
293 * @ops: defines the notifier functions to be called
294 *
295 * Usually used in conjunction with container_of().
296 */
297struct preempt_notifier {
298 struct hlist_node link;
299 struct preempt_ops *ops;
300};
301
302void preempt_notifier_register(struct preempt_notifier *notifier);
303void preempt_notifier_unregister(struct preempt_notifier *notifier);
304
305static inline void preempt_notifier_init(struct preempt_notifier *notifier,
306 struct preempt_ops *ops)
307{
308 INIT_HLIST_NODE(&notifier->link);
309 notifier->ops = ops;
310}
311
312#endif
313
1da177e4 314#endif /* __LINUX_PREEMPT_H */